A PHP port of draughts.js
A PHP checkers library for move generation/validation, piece placement/movement and draw detection. Useful for writing the server side implementation of a multi-player checkers game. It has been ported from a JavaScript implementation by @shubhendusaurabh. You can see a list of the contributors to this project here.
Install using composer: composer require photogabble/draughts
The code below will play a complete game of draughts, outputting the result of each move with each move being picked randomly:
$draughts = new Draughts();
echo $draughts->ascii();
while (!$draughts->gameOver())
{
$moves = $draughts->generateMoves();
$move = $moves[array_rand($moves, 1)];
$draughts->move($move);
echo $draughts->ascii();
}
During my time developing this port I have found this PDN Viewer to be invaluable at helping me understand the PDN structure and as a visual debugger of the PDN that this library generates.
The Draughts
class __construct
method takes an optional string
parameter that defines the initial board configuration in Forsyth-Edwards Notation.
// Board defaults to the starting position when call with no parameter
$draughts = new Draughts;
// Pass in a FEN string to load a particular position
$draughts = new Draughts('W:W31-50:B1-20');
Returns a string containing an ASCII diagram of the current position.
Reset the board to the initial starting position.
Returns the Forsyth-Edwards Notation (FEN) string for the current position.
Returns true
if the game has ended via no moves left, or no pieces rule. Otherwise, returns false
.
Under development, see issue #4
Under development, see issue #5
Attempts to make a move on the board, returning a Move
object if the move was legal, otherwise null
.
Returns a list of legals moves from the current position. The function takes an optional parameter which controls the single-square move generation.
Returns the current side to move.
Takeback the last half-move, returning a Move
object if successful, otherwise null
.
Returns the piece on the square.
Removes the piece on the square.
Puts the piece on the square and returns true
if valid placement. Otherwise, returns false
.
Returns a list containing the moves of the current game.
Update the header properties.
Load the moves of a game stored in Portable Draughts Notation. Options is a optional parameter that contains a 'newline_char' which is a string representation of a RegExp (and should not be pre-escaped) and defaults to '\r?\n'). Returns true
if the pdn was parsed successfully, otherwise false
.
This started as a PHP port of shubhendusaurabh/draughts.js.