Games_Chess
[ class tree: Games_Chess ] [ index: Games_Chess ] [ all elements ]

Class: Games_Chess

Source Location: /Games_Chess-1.0.1/Chess.php

Class Overview


ABSTRACT parent class - use Games_Chess_Standard for a typical chess game


Methods


Child classes:

Games_Chess_Standard
Standard chess game

Inherited Variables

Inherited Methods


Class Details

[line 270]
ABSTRACT parent class - use Games_Chess_Standard for a typical chess game

Error Constants This class contains a few public methods that are the only thing most users of the package will ever need. Protected methods are available for usage by child classes, and it is expected that all child classes will implement certain protected methods used by the utility methods in this class.

Public API methods used are:

Game-related methods

  • resetGame(): in order to start a new game (pass a FEN for a starting position)
  • blankBoard(): in order to start with an empty chessboard
  • addPiece(): Use to add pieces one at a time to the board
  • moveSAN(): Use to move pieces based on their SAN (Qa3, exd5, etc.)
  • moveSquare(): Use to move pieces based on their square (a2 -> a3 for Qa3, e4 -> d5 for exd5, etc.)
Game state methods:

  • inCheck(): Use to determine the presence of check
  • inCheckMate(): Use to determine a won game
  • inStaleMate(): Use to determine presence of stalemate draw
  • in50MoveDraw(): Use to determine presence of 50-move rule draw
  • inRepetitionDraw(): Use to determine presence of a draw by repetition
  • inStaleMate(): Use to determine presence of stalemate draw
  • inDraw(): Use to determine if any forced draw condition exists
Game data methods:

  • renderFen(): Use to retrieve a FEN representation of the current chessboard position, in order to transfer to another chess program
  • toArray(): Use to retrieve a literal representation of the current chessboard position, in order to display as HTML or some other format for the user
  • getMoveList(): Use to retrieve the list of SAN moves for this game



[ Top ]


Method Detail

addPiece   [line 819]

void addPiece( W|B $color, P|N|K|Q|R|B $type, string $square)

Add a piece to the chessboard

Must be overridden in child classes

  • Abstract:

Overridden in child classes as:

Games_Chess_Standard::addPiece()
Add a piece to the chessboard
Games_Chess_Crazyhouse::addPiece()
Add a piece to the chessboard

Parameters:

W|B   $color   —  Color of piece
P|N|K|Q|R|B   $type   —  Piece type
string   $square   —  algebraic location of piece

[ Top ]

blankBoard   [line 376]

void blankBoard( )

Create a blank chessboard with no pieces on it

Overridden in child classes as:

Games_Chess_Standard::blankBoard()
Set up a blank chess board
Games_Chess_Crazyhouse::blankBoard()
Set up a blank chess board

[ Top ]

canCastleKingside   [line 869]

boolean canCastleKingside( )

Determine legality of kingside castling

[ Top ]

canCastleQueenside   [line 879]

boolean canCastleQueenside( )

Determine legality of queenside castling

[ Top ]

commitTransaction   [line 2345]

void commitTransaction( )

Remove any possibility of undo.

[ Top ]

factory   [line 358]

void &factory( [ $type = 'Standard'])


Parameters:

   $type   — 

[ Top ]

gameOver   [line 535]

W|B|D|false gameOver( )

  • Return: winner of game, or draw, or false if still going

Overridden in child classes as:

Games_Chess_Losers::gameOver()

[ Top ]

getDiagonalColor   [line 2113]

void getDiagonalColor( $square)


Parameters:

   $square   — 

[ Top ]

getMessage   [line 2174]

string getMessage( integer $code, array $extra)

Get an error message from the code

Future versions of this method will be multi-language


Parameters:

integer   $code   —  Error code
array   $extra   —  extra information to pass for error message creation

[ Top ]

getMoveList   [line 524]

array getMoveList( [boolean $withChecks = false])

Get the list of moves in Standard Algebraic Notation

Can be used to populate a PGN file.


Parameters:

boolean   $withChecks   —  If true, then moves that check will be postfixed with "+" and checkmate with "#" as in Nf3+ or Qxg7#

[ Top ]

getPieceLocations   [line 1763]

array|PEAR_Error getPieceLocations( [W|B $color = null])

Get the location of all pieces on the board of a certain color

Default is the color that is about to move

  • Throws: GAMES_CHESS_ERROR_INVALID_COLOR

Parameters:

W|B   $color   — 

[ Top ]

getPossibleBishopMoves   [line 1819]

array getPossibleBishopMoves( string $square, [W|B $color = null])

Get all legal Bishop moves (checking of the king is not taken into account)

Parameters:

string   $square   —  [a-h][1-8] Location of piece
W|B   $color   —  color of piece, or null to use current piece to move

[ Top ]

getPossibleKingMoves   [line 2069]

array getPossibleKingMoves( string $square, [W|B $color = null], [ $returnCastleMoves = true])

Get all legal King moves (checking of the king is not taken into account)
  • Since: 0.7alpha castling is possible by moving the king to the destination square

Parameters:

string   $square   —  [a-h][1-8] Location of piece
W|B   $color   —  color of piece, or null to use current piece to move
   $returnCastleMoves   — 

[ Top ]

getPossibleKnightMoves   [line 1794]

array getPossibleKnightMoves( string $square, [W|B $color = null])

Get all legal Knight moves (checking of the king is not taken into account)

Parameters:

string   $square   —  [a-h][1-8] Location of piece
W|B   $color   —  color of piece, or null to use current piece to move

[ Top ]

getPossibleMoves   [line 1418]

array|PEAR_Error getPossibleMoves( K|P|Q|R|B|N $piece, string $square, [B|W $color = null], [boolean $returnCastleMoves = true])

Get a list of all possible theoretical squares a piece of this nature and color could move to with the current board and game setup.

This method will return all valid moves without determining the presence of check

  • Throws: GAMES_CHESS_ERROR_INVALID_COLOR
  • Throws: GAMES_CHESS_ERROR_INVALID_SQUARE
  • Throws: GAMES_CHESS_ERROR_INVALID_PIECE

Parameters:

K|P|Q|R|B|N   $piece   —  Piece name
string   $square   —  [a-h][1-8] algebraic location of the piece
B|W   $color   —  color of the piece
boolean   $returnCastleMoves   —  Whether to return shortcut king moves for castling

[ Top ]

getPossiblePawnMoves   [line 1988]

array getPossiblePawnMoves( string $square, [W|B $color = null], [ $enpassant = null])

Get all legal Pawn moves (checking of the king is not taken into account)

Parameters:

string   $square   —  [a-h][1-8] Location of piece
W|B   $color   —  color of piece, or null to use current piece to move
   $enpassant   — 

[ Top ]

getPossibleQueenMoves   [line 1969]

array getPossibleQueenMoves( string $square, [W|B $color = null])

Get all legal Queen moves (checking of the king is not taken into account)

Parameters:

string   $square   —  [a-h][1-8] Location of piece
W|B   $color   —  color of piece, or null to use current piece to move

[ Top ]

getPossibleRookMoves   [line 1894]

array getPossibleRookMoves( string $square, [W|B $color = null])

Get all legal Rook moves (checking of the king is not taken into account)

Parameters:

string   $square   —  [a-h][1-8] Location of piece
W|B   $color   —  color of piece, or null to use current piece to move

[ Top ]

getState   [line 2337]

void getState( array 0)

Get the current state of the chess game

Use this in conjunction with setState


Parameters:

array   0   — 

[ Top ]

in50MoveDraw   [line 685]

boolean in50MoveDraw( )

Determine whether any pawn move or capture has occurred in the past 50 moves

Overridden in child classes as:

Games_Chess_Crazyhouse::in50MoveDraw()
50 move draw is not allowed in crazyhouse

[ Top ]

inBasicDraw   [line 705]

boolean inBasicDraw( )

Determine the presence of a basic draw as defined by FIDE rules

The rule states:

 10.4

 The game is drawn when one of the following endings arises:
 (a) king against king;
 (b) king against king with only bishop or knight;
 (c) king and bishop against king and bishop, with both bishops
     on diagonals of the same colour.


Overridden in child classes as:

Games_Chess_Crazyhouse::inBasicDraw()
Basic draw is impossible in crazyhouse, because it is always possible

[ Top ]

inCheckMate   [line 553]

boolean inCheckMate( [W|B $color = null])

Determine whether a side is in checkmate
  • Throws: GAMES_CHESS_ERROR_INVALID_COLOR

Parameters:

W|B   $color   —  color of side to check, defaults to the current side

[ Top ]

inDraw   [line 641]

boolean inDraw( [W|B $color = null])

Determines the presence of a forced draw

Parameters:

W|B   $color   — 

[ Top ]

inRepetitionDraw   [line 672]

boolean inRepetitionDraw( )

Determine whether draw by repetition has happened

From FIDE rules:

 10.10

 The game is drawn, upon a claim by the player having the move, when the
 same position, for the third time:
 (a) is about to appear, if he first writes the move on his
     scoresheet and declares to the arbiter his intention of making
     this move; or
 (b) has just appeared, the same player having the move each time.

 The position is considered the same if pieces of the same kind and
 colour occupy the same squares, and if all the possible moves of
 all the pieces are the same, including the rights to castle [at
 some future time] or to capture a pawn "en passant".

This class determines draw by comparing FENs rendered after every move


Overridden in child classes as:

Games_Chess_Crazyhouse::inRepetitionDraw()
Repetition draw is not allowed in crazyhouse

[ Top ]

inStaleMate   [line 600]

boolean inStaleMate( [W|B $color = null])

Determine whether a side is in stalemate
  • Throws: GAMES_CHESS_ERROR_INVALID_COLOR

Parameters:

W|B   $color   —  color of the side to look at, defaults to the current side

[ Top ]

isError   [line 2297]

boolean isError( mixed $err)

Determines whether the data returned from a method is a PEAR-related

error class


Parameters:

mixed   $err   — 

[ Top ]

isPromoteMove   [line 845]

boolean isPromoteMove( string $from, string $to)

Determine whether moving a piece from one square to another requires

a pawn promotion

  • Return: true if the move represented by moving from $from to $to is a pawn promotion move

Parameters:

string   $from   —  [a-h][1-8] location of the piece to move
string   $to   —  [a-h][1-8] place to move the piece to

[ Top ]

moveSAN   [line 412]

true|PEAR_Error moveSAN( string $move)

Make a move from a Standard Algebraic Notation (SAN) format

SAN is just a normal chess move like Na4, instead of the English Notation, like NR4


Overridden in child classes as:

Games_Chess_Crazyhouse::moveSAN()
Make a move from a Standard Algebraic Notation (SAN) format

Parameters:

string   $move   — 

[ Top ]

moveSquare   [line 506]

true|PEAR_Error moveSquare( string $from, string $to, [string $promote = ''])

Move a piece from one square to another, and mark the old square as empty

Parameters:

string   $from   —  [a-h][1-8] square to move from
string   $to   —  [a-h][1-8] square to move to
string   $promote   —  piece to promote to, if this is a promotion move

[ Top ]

raiseError   [line 2159]

PEAR_Error raiseError( integer $code, [array $extra = array()])

  • Uses: PEAR::raiseError()

Parameters:

integer   $code   —  error code from Chess.php
array   $extra   —  associative array of additional error message data

[ Top ]

renderFen   [line 768]

string renderFen( [boolean $include_moves = true])

render the FEN notation for the current board

Parameters:

boolean   $include_moves   —  private parameter, used to determine whether to include move number/ply count - this is used to keep track of positions for draw detection

[ Top ]

resetGame   [line 393]

PEAR_Error|true resetGame( [false|string $fen = false])

Create a new game with the starting position, or from the position specified by $fen
  • Return: returns any errors thrown by _parseFen()

Parameters:

false|string   $fen   — 

[ Top ]

rollbackTransaction   [line 2353]

void rollbackTransaction( )

Undo any changes to state since startTransaction() was first used

[ Top ]

setState   [line 2324]

void setState( array $state)

Set the state of the chess game

WARNING: this resets the state without any validation.


Parameters:

array   $state   — 

[ Top ]

startTransaction   [line 2308]

void startTransaction( )

Begin a chess piece transaction

Transactions are used to attempt moves that may be revoked later, especially in methods like inCheckMate()


[ Top ]

toArray   [line 832]

array toArray( )

Generate a representation of the chess board and pieces for use as a direct translation to a visual chess board

Must be overridden in child classes

  • Abstract:

Overridden in child classes as:

Games_Chess_Standard::toArray()
Generate a representation of the chess board and pieces for use as a
Games_Chess_Crazyhouse::toArray()

[ Top ]

toMove   [line 860]

W|B toMove( )

  • Return: return the color of the side to move (white or black)

[ Top ]

_convertSquareToSAN   [line 1313]

string|PEAR_Error _convertSquareToSAN( string $from, string $to, [string $promote = ''])

Convert a starting and ending algebraic square into SAN
  • Throws: GAMES_CHESS_ERROR_INVALID_SQUARE
  • Throws: GAMES_CHESS_ERROR_INVALID_PROMOTE
  • Throws: GAMES_CHESS_ERROR_NO_PIECE
  • Throws: GAMES_CHESS_ERROR_WRONG_COLOR
  • Throws: GAMES_CHESS_ERROR_CANT_MOVE_THAT_WAY
  • Access: protected

Parameters:

string   $from   —  [a-h][1-8] square piece is on
string   $to   —  [a-h][1-8] square piece moves to
string   $promote   —  Q|R|B|N

[ Top ]

_getAllPieceLocations   [line 1783]

array _getAllPieceLocations( W|B $color)

Get the location of every piece on the board of color $color
  • Abstract:
  • Access: protected

Overridden in child classes as:

Games_Chess_Standard::_getAllPieceLocations()
Get the location of every piece on the board of color $color
Games_Chess_Crazyhouse::_getAllPieceLocations()
Get the location of every piece on the board of color $color

Parameters:

W|B   $color   —  color of pieces to check

[ Top ]

_getCastleSquares   [line 1695]

array _getCastleSquares( string $square)

Get a list of all the squares a king could castle to on an empty board

WARNING: assumes valid input

  • Since: 0.7alpha
  • Access: protected

Parameters:

string   $square   —  [a-h][1-8]

[ Top ]

_getDiagonalColor   [line 2103]

B|W _getDiagonalColor( string $square)

Return the color of a square (black or white)
  • Access: protected

Parameters:

string   $square   —  [a-h][1-8]

[ Top ]

_getKingSquares   [line 1725]

array _getKingSquares( string $square)

Get a list of all the squares a king could move to on an empty board

WARNING: assumes valid input

  • Access: protected

Parameters:

string   $square   —  [a-h][1-8]

[ Top ]

_getKnightSquares   [line 1645]

array _getKnightSquares( string $square, [boolean $returnFlatArray = false])

Get all the squares a knight could move to on an empty board

WARNING: assumes valid input

  • Return: Returns an array of all the squares organized by compass point, that a knight can go to. These squares may be indexed by any of WNW, NNW, NNE, ENE, ESE, SSE, SSW or WSW, unless $returnFlatArray is true, in which case an array of squares is returned
  • Access: protected

Parameters:

string   $square   —  [a-h][1-8]
boolean   $returnFlatArray   —  if true, simply returns an array of all squares

[ Top ]

_getQueenSquares   [line 1626]

array _getQueenSquares( string $square, [boolean $returnFlatArray = false])

Get all the squares a queen could go to on a blank board

WARNING: assumes valid input

  • Return: combines contents of _getRookSquares() and _getDiagonals()
  • Access: protected

Parameters:

string   $square   —  [a-h][1-8]
boolean   $returnFlatArray   —  if true, simply returns an array of all squares

[ Top ]

_getRookSquares   [line 1560]

array _getRookSquares( string $square, [boolean $returnFlatArray = false])

Get the set of squares that are diagonals from this square on an empty board.

WARNING: assumes valid input

  • Return:

    Format:

     array(
       'N' => array(square, square),
       'E' => array(square, square),
       'S' => array(square, square),
       'W' => array(square, square)
     )

    Think of the horizontal directions as on a map. squares are listed with closer squares first

  • Access: protected

Parameters:

string   $square   —  [a-h][1-8]
boolean   $returnFlatArray   —  if true, simply returns an array of all squares

[ Top ]

_moveAlgebraic   [line 894]

void _moveAlgebraic( string $from, string $to, [string $promote = ''])

Move a piece from one square to another, and mark the old square as empty

NO validation is performed, use moveSquare() for validation.

  • Access: protected

Parameters:

string   $from   —  [a-h][1-8] square to move from
string   $to   —  [a-h][1-8] square to move to
string   $promote   —  piece to promote to, if this is a promotion move

[ Top ]

_parseMove   [line 918]

array _parseMove( string $move)

Parse out the segments of a move (minus any annotations)
  • Access: protected

Parameters:

string   $move   — 

[ Top ]

_setupStartingPosition   [line 995]

void _setupStartingPosition( )

Set up the board with the starting position

Must be overridden in child classes

  • Abstract:
  • Access: protected

Overridden in child classes as:

Games_Chess_Standard::_setupStartingPosition()
Set up a starting position for a new chess game
Games_Chess_Crazyhouse::_setupStartingPosition()
Set up a starting position for a new chess game

[ Top ]

_validMove   [line 1191]

true|PEAR_Error _validMove( array $move)

Validate a move
  • Throws: GAMES_CHESS_ERROR_CK_PIECES_IN_WAY
  • Throws: GAMES_CHESS_ERROR_CANT_CK
  • Throws: GAMES_CHESS_ERROR_IN_CHECK
  • Throws: GAMES_CHESS_ERROR_CANT_CQ
  • Throws: GAMES_CHESS_ERROR_CQ_PIECES_IN_WAY
  • Throws: GAMES_CHESS_ERROR_STILL_IN_CHECK
  • Throws: GAMES_CHESS_ERROR_MOVE_WOULD_CHECK
  • Throws: GAMES_CHESS_ERROR_CANT_CAPTURE_OWN
  • Throws: GAMES_CHESS_ERROR_CASTLE_WOULD_CHECK
  • Access: protected

Overridden in child classes as:

Games_Chess_Losers::_validMove()
Validate a move

Parameters:

array   $move   —  parsed move array from _parsedMove()

[ Top ]


Documentation generated on Mon, 11 Mar 2019 15:05:01 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.