photogabble / draughts

A PHP checkers library for move generation/validation, piece placement/movement, and draw detection
MIT License
1 stars 1 forks source link
port wip

Draughts

A PHP port of draughts.js

Build Status License Gitmoji

About this package

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

Install using composer: composer require photogabble/draughts

Example Usage

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();
}

PDN Viewer

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.

Public API

Constructor

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');

ascii(bool $unicode = false): string

Returns a string containing an ASCII diagram of the current position.

reset(): void

Reset the board to the initial starting position.

generateFen(): string

Returns the Forsyth-Edwards Notation (FEN) string for the current position.

gameOver(): bool

Returns true if the game has ended via no moves left, or no pieces rule. Otherwise, returns false.

inDraw(): bool

Under development, see issue #4

inThreefoldRepetition()

Under development, see issue #5

move(Move $move): ?Move

Attempts to make a move on the board, returning a Move object if the move was legal, otherwise null.

generateMoves(int $square = null): array

Returns a list of legals moves from the current position. The function takes an optional parameter which controls the single-square move generation.

turn(): string

Returns the current side to move.

undo(): ?Move

Takeback the last half-move, returning a Move object if successful, otherwise null.

get($square): string

Returns the piece on the square.

remove(int $square): string

Removes the piece on the square.

put(string $piece, int $square): bool

Puts the piece on the square and returns true if valid placement. Otherwise, returns false.

getHistory(bool $verbose = false): array

Returns a list containing the moves of the current game.

setHeader(array $values = []): array

Update the header properties.

loadPDN(string $pdn, array $options = [])

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.

Not invented here

This started as a PHP port of shubhendusaurabh/draughts.js.