Skip to content

akondas/chess

 
 

Repository files navigation

Chess

Chess is a PHP chess library that is used for chess move generation/validation, piece placement/movement, and check/checkmate/stalemate detection - basically everything but the AI.

NOTE: this started as a port of chess.js for php, froked from ryanhs/chess.php

Latest Stable Version Build Status MIT License

Installation

use composer with composer require p-chess/chess or put in your composer.json

"require": {
    "p-chess/chess": "^1.0"
}

Example Code

The code below plays a complete game of chess ... randomly.

<?php

require 'vendor/autoload.php';
use \PChess\Chess\Chess;

$chess = new Chess();
while (!$chess->gameOver()) {
    $moves = $chess->moves();
    $move = $moves[mt_rand(0, count($moves) - 1)];
    $chess->move($move);
}

echo $chess->ascii() . PHP_EOL;
   +---+---+---+---+---+---+---+---+
 8 |   | ♜ | ♘ |   |   |   |   |   | 
   +---+---+---+---+---+---+---+---+
 7 | ♞ |   |   |   |   |   |   |   | 
   +---+---+---+---+---+---+---+---+
 6 |   |   |   |   |   |   |   |   | 
   +---+---+---+---+---+---+---+---+
 5 |   |   |   |   |   |   |   |   | 
   +---+---+---+---+---+---+---+---+
 4 |   |   |   |   |   | ♚ | ♟ |   | 
   +---+---+---+---+---+---+---+---+
 3 | ♜ |   |   |   |   |   |   |   | 
   +---+---+---+---+---+---+---+---+
 2 |   |   |   |   |   |   |   |   | 
   +---+---+---+---+---+---+---+---+
 1 | ♔ |   |   |   | ♞ |   |   |   | 
   +---+---+---+---+---+---+---+---+
     a   b   c   d   e   f   g   h

Performance

There is still a lot to do in this topic.
akondas/php-grandmaster is a good place to start experiment ;)

Chess::move()

iteration mean comment
1 548.819μs initial
2 447.973μs replace fen with json_encode in history position (inThreefoldRepetition cache)
3 340.375μs replace fen with json_encode in generateMoves
4 333.145μs add boardHash calculation on make/undo move
5 25.917μs 🔥 add cache for moveToSAN method

About

PHP Chess engine

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%