Source for file Standard.php
Documentation is available at Standard.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at |
// | http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Gregory Beaver <cellog@php.net> |
// +----------------------------------------------------------------------+
// $Id: Standard.php,v 1.8 2006/11/18 00:09:20 cellog Exp $
* A standard chess game representation
* @author Gregory Beaver <cellog@php.net>
require_once 'Games/Chess.php';
* @author Gregory Beaver <cellog@php.net>
* Set up a blank chess board
* Set up a starting position for a new chess game
'a8' => 'BR1', 'b8' => 'BN1', 'c8' => 'BB1', 'd8' => 'BQ', 'e8' => 'BK', 'f8' => 'BB2', 'g8' => 'BN2', 'h8' => 'BR2',
'a7' => 'BP1', 'b7' => 'BP2', 'c7' => 'BP3', 'd7' => 'BP4', 'e7' => 'BP5', 'f7' => 'BP6', 'g7' => 'BP7', 'h7' => 'BP8',
'a6' => 'a6', 'b6' => 'b6', 'c6' => 'c6', 'd6' => 'd6', 'e6' => 'e6', 'f6' => 'f6', 'g6' => 'g6', 'h6' => 'h6',
'a5' => 'a5', 'b5' => 'b5', 'c5' => 'c5', 'd5' => 'd5', 'e5' => 'e5', 'f5' => 'f5', 'g5' => 'g5', 'h5' => 'h5',
'a4' => 'a4', 'b4' => 'b4', 'c4' => 'c4', 'd4' => 'd4', 'e4' => 'e4', 'f4' => 'f4', 'g4' => 'g4', 'h4' => 'h4',
'a3' => 'a3', 'b3' => 'b3', 'c3' => 'c3', 'd3' => 'd3', 'e3' => 'e3', 'f3' => 'f3', 'g3' => 'g3', 'h3' => 'h3',
'a2' => 'WP1', 'b2' => 'WP2', 'c2' => 'WP3', 'd2' => 'WP4', 'e2' => 'WP5', 'f2' => 'WP6', 'g2' => 'WP7', 'h2' => 'WP8',
'a1' => 'WR1', 'b1' => 'WN1', 'c1' => 'WB1', 'd1' => 'WQ', 'e1' => 'WK', 'f1' => 'WB2', 'g1' => 'WN2', 'h1' => 'WR2',
$this->_enPassantSquare = '-';
'WP1' => array ('a2', 'P'),
'WP2' => array ('b2', 'P'),
'WP3' => array ('c2', 'P'),
'WP4' => array ('d2', 'P'),
'WP5' => array ('e2', 'P'),
'WP6' => array ('f2', 'P'),
'WP7' => array ('g2', 'P'),
'WP8' => array ('h2', 'P'),
'BP1' => array ('a7', 'P'),
'BP2' => array ('b7', 'P'),
'BP3' => array ('c7', 'P'),
'BP4' => array ('d7', 'P'),
'BP5' => array ('e7', 'P'),
'BP6' => array ('f7', 'P'),
'BP7' => array ('g7', 'P'),
'BP8' => array ('h7', 'P'),
* Add a piece to the chessboard
* @param K|Q|R|N|P|BPiece type
* @param string [a-h][1-8] algebraic location of piece
* @return true|PEAR_Error
* @throws GAMES_CHESS_ERROR_INVALIDSQUARE
* @throws GAMES_CHESS_ERROR_DUPESQUARE
* @throws GAMES_CHESS_ERROR_MULTIPIECE
function addPiece($color, $type, $square)
if (!isset ($this->_board[$square])) {
return $this->raiseError(GAMES_CHESS_ERROR_INVALIDSQUARE ,
array ('square' => $square));
if ($this->_board[$square] != $square) {
$dpiece = $this->_board[$square];
$dpiece = $this->_pieces[$dpiece][1 ];
array ('piece' => $type, 'dpiece' => $dpiece, 'square' => $square));
$piece_name = $color . $type;
if (!$this->_pieces[$piece_name . '1']) {
$this->_board[$square] = $piece_name . '1';
$this->_pieces[$piece_name . '1'] = $square;
} elseif (!$this->_pieces[$piece_name . '2']) {
$this->_board[$square] = $piece_name . '2';
$this->_pieces[$piece_name . '2'] = $square;
for ($col = 1; $col <= 8; $col++ ) {
if (!$this->_pieces[$color . 'P' . $col]) {
$this->_pieces[$color . 'P' . $col] =
$this->_board[$square] = $color . 'P' . $col;
array ('color' => $color, 'piece' => $type));
$piece_name = $color . 'Q';
if (!$this->_pieces[$piece_name]) {
$this->_board[$square] = $piece_name;
$this->_pieces[$piece_name] = $square;
for ($col = 1; $col <= 8; $col++ ) {
if (!$this->_pieces[$color . 'P' . $col]) {
$this->_pieces[$color . 'P' . $col] =
$this->_board[$square] = $color . 'P' . $col;
array ('color' => $color, 'piece' => $type));
for ($col = 1; $col <= 8; $col++ ) {
if (!$this->_pieces[$color . 'P' . $col]) {
$this->_pieces[$color . 'P' . $col] =
$this->_board[$square] = $color . 'P' . $col;
array ('color' => $color, 'piece' => $type));
if (!$this->_pieces[$color . 'K']) {
$this->_pieces[$color . 'K'] = $square;
$this->_board[$square] = $color . 'K';
array ('color' => $color, 'piece' => $type));
* Generate a representation of the chess board and pieces for use as a
* direct translation to a visual chess board
foreach ($this->_board as $square => $piece) {
$piece = $this->_pieces[$piece][1 ];
uksort($ret, array ($this, '_sortToArray'));
* Sort two algebraic coordinates for easy display by foreach() iteration
function _sortToArray ($a, $b)
* Render the current board position into Forsyth-Edwards Notation
* This method only renders the board contents, not the castling and other
foreach ($this->_board as $square => $piece) {
if ($square{1 } != $saverow) {
// if we have just moved to the next rank,
// output any whitespace, and a '/'
// increment whitespace - no piece on this square
// add any whitespace and reset
// add pawns/promoted pawns
$p = ($piece{0 } == 'W') ? $this->_pieces[$piece][1 ] :
$p = ($piece{0 } == 'W') ? $piece{1 } : strtolower($piece{1 });
// add any trailing whitespace
* Get the location of every piece on the board of color $color
* @param W|Bcolor of pieces to check
foreach ($this->_pieces as $name => $loc) {
if ($name{0 } == $color) {
$where = (is_array($loc) ? $loc[0 ] : $loc);
* Used to determine check
* Retrieve all of the moves of the pieces matching the color passed in.
foreach ($this->_pieces as $name => $loc) {
if ($name{0 } == $color) {
* Determine whether one side's king is in check by the other side's pieces
* @param W|Bcolor of pieces to determine enemy check
* @return string|array|falsesquare of checking piece(s) or false
$king = $this->_pieces[$color . 'K'];
foreach ($possible as $piece => $squares) {
$loc = $this->_pieces[$piece];
$ret[] = is_array($loc) ? $loc[0 ] : $loc;
* Mark a piece as having been taken. No validation is performed
* @param string [a-h][1-8]
if (isset ($this->_pieces[$this->_board[$piece]])) {
$this->_pieces[$this->_board[$piece]] = false;
* Move a piece from one square to another, disregarding any existing pieces
* {@link _takePiece()} should always be used prior to this method. No
* validation is performed
* @param string [a-h][1-8] square the piece resides on
* @param string [a-h][1-8] square the piece moves to
* @param string Piece to promote to if this is a promotion move
function _movePiece ($from, $to, $promote = '')
if (isset ($this->_pieces[$this->_board[$from]])) {
$newto = $this->_pieces[$this->_board[$from]];
if ($promote && ($to{1 } == '8' || $to{1 } == '1')) {
$this->_pieces[$this->_board[$from]] = $newto;
* Translate an algebraic coordinate into the color and name of a piece,
* or false if no piece is on that square
* @return false|arrayFormat array('color' => B|W, 'piece' => P|R|Q|N|K|B)
* @param string [a-h][1-8]
if ($this->_board[$square] != $square) {
$piece = $this->_board[$square];
$piece = $this->_pieces[$piece][1 ];
return array ('color' => $color, 'piece' => $piece);
* Retrieve the locations of all pieces of the same type as $piece
* @param string [a-h][1-8] optional square of piece to exclude from the listing
foreach ($this->_pieces as $name => $loc) {
if ($name{0 } != $color) {
if ($loc[1 ] != $piece || $loc[0 ] == $exclude) {
if ($name{1 } != $piece) {
* @return string|PEAR_Error
* @param array contents returned from {@link parent::_parseMove()}
* in other words, not array(GAMES_CHESS_PIECEMOVE =>
* array('piece' => 'K', ...)), but array('piece' => 'K', ...)
* @param W|Bcurrent side moving
function _getSquareFromParsedMove ($parsedmove, $color = null )
switch ($parsedmove['piece']) {
return $this->_pieces[$color . 'K'];
if ($parsedmove['disambiguate']) {
if (strlen($parsedmove['disambiguate']) == 2 ) {
$square = $parsedmove['disambiguate'];
} elseif (is_numeric($parsedmove['disambiguate'])) {
$row = $parsedmove['disambiguate'];
$col = $parsedmove['disambiguate'];
foreach ($others as $square) {
// other pieces can move to this square - need to disambiguate
if (count($ambiguous) > 1 ) {
$pieces = implode($ambiguous, ' ');
array ('san' => $parsedmove['piece'] .
$parsedmove['disambiguate'] . $parsedmove['takes']
'piece' => $parsedmove['piece']));
$square = $col = $row = null;
foreach ($this->_pieces as $name => $value) {
if ($name{0 } != $color) {
if ($name{1 } == $parsedmove['piece'] &&
if ($name{1 } == 'P' && $value[0 ] == $square &&
$value[1 ] == $parsedmove['piece']) {
if ($name{1 } == $parsedmove['piece'] &&
if ($name{1 } == 'P' && $value[0 ]{0 } == $col &&
$value[1 ] == $parsedmove['piece']) {
$potentials[] = $value[0 ];
if ($name{1 } == $parsedmove['piece'] &&
if ($name{1 } == 'P' && $value[0 ]{1 } == $row &&
$value[1 ] == $parsedmove['piece']) {
$potentials[] = $value[0 ];
if ($name{1 } == $parsedmove['piece']) {
} elseif ($name{1 } == 'P' &&
$value[1 ] == $parsedmove['piece']) {
$potentials[] = $value[0 ];
if (count($potentials) == 1 ) {
if ($parsedmove['disambiguate']) {
$square = $parsedmove['disambiguate'] . $parsedmove['takesfrom'];
if ($parsedmove['takesfrom']) {
$col = $parsedmove['takesfrom'];
foreach ($this->_pieces as $name => $value) {
if ($name{0 } != $color) {
if ($name{1 } == 'P' && $value[0 ] == $square && $value[1 ] == 'P') {
if ($name{1 } == 'P' && $value[0 ]{0 } == $col && $value[1 ] == 'P') {
$potentials[] = $value[0 ];
if ($name{1 } == 'P' && $value[1 ] == 'P') {
$potentials[] = $value[0 ];
if (count($potentials) == 1 ) {
if ($parsedmove['piece'] == 'P') {
$san = $parsedmove['takesfrom'] . $parsedmove['takes'] . $parsedmove['square'];
$san = $parsedmove['piece'] .
$parsedmove['disambiguate'] . $parsedmove['takes'] .
* Get the location of the king
* assumes valid color input
return $this->_pieces[$color . 'K'];
return $this->_pieces[$this->_move . 'K'];
* Get the location of a piece
* This does NOT take an algebraic square as the argument, but the contents
* of _board[algebraic square]
return is_array($this->_pieces[$piecename]) ?
$this->_pieces[$piecename][0 ] :
$this->_pieces[$piecename];
* Determine whether a piece name is a knight
* This does NOT take an algebraic square as the argument, but the contents
* of _board[algebraic square]
return $piecename{1 } == 'N' ||
$this->_pieces[$piecename][1 ] == 'N');
* Determine whether a piece name is a queen
* This does NOT take an algebraic square as the argument, but the contents
* of _board[algebraic square]
return $piecename{1 } == 'Q' ||
$this->_pieces[$piecename][1 ] == 'Q');
* Determine whether a piece name is a bishop
* This does NOT take an algebraic square as the argument, but the contents
* of _board[algebraic square]
return $piecename{1 } == 'B' ||
$this->_pieces[$piecename][1 ] == 'B');
* Determine whether a piece name is a rook
* This does NOT take an algebraic square as the argument, but the contents
* of _board[algebraic square]
return $piecename{1 } == 'R' ||
$this->_pieces[$piecename][1 ] == 'R');
* Determine whether a piece name is a pawn
* This does NOT take an algebraic square as the argument, but the contents
* of _board[algebraic square]
return $piecename{1 } == 'P' &&
$this->_pieces[$piecename][1 ] == 'P';
* Determine whether a piece name is a king
* This does NOT take an algebraic square as the argument, but the contents
* of _board[algebraic square]
return $piecename{1 } == 'K';
* Determine whether it is possible to capture the piece delivering check,
* or to interpose a piece in between the checking piece and the king
* @param array squares that will block a checkmate
* @param W|Bcolor of the side attempting to prevent checkmate
* @return boolean true if it is possible to remove check
function _interposeOrCapture ($squares, $color)
foreach ($this->_pieces as $name => $value) {
if ($name{0 } != $color) {
foreach($squares as $square) {
// try the move, see if we're still in check
// if so, then the piece is pinned and cannot move
PEAR ::pushErrorHandling (PEAR_ERROR_RETURN );
PEAR ::popErrorHandling (PEAR_ERROR_RETURN );
$stillchecked = $this->inCheck($color);
* Retrieve the color of a piece from its name
* Game-specific method of retrieving the color of a piece
* Get a list of all pieces on the board organized by the type of piece,
* and the color of the square the piece is on.
* Used to determine basic draw conditions
* 'W' => array('B' => array('W', 'B'), // all bishops
* 'B' => array('Q' => array('B'), // all queens
* 'K' => array('W'),... // king is on white square
$ret = array ('W' => array (), 'B' => array ());
foreach($this->_pieces as $name => $loc) {
Documentation generated on Sun, 17 Jun 2007 02:01:00 -0400 by phpDocumentor 1.3.2. PEAR Logo Copyright © PHP Group 2004.
|