akondas / chess.php
chess.php is a PHP chess library that is used for chess move generation/validation, piece placement/movement, and check/checkmate/stalemate detection
Installs: 494
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 1
Forks: 18
pkg:composer/akondas/chess.php
Requires
- php: ^7.2
- ext-json: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.14
- johnkary/phpunit-speedtrap: ^3.0
- phpbench/phpbench: ^0.15.0
- phpstan/phpstan: ^0.11.1
- phpunit/phpunit: ^7.5
This package is auto-updated.
Last update: 2025-10-29 02:56:16 UTC
README
chess.php 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 is a port of chess.js for php, froked from ryanhs/chess.php
Installation
use composer with composer require akondas/chess.php
or put in your composer.json
"require": {
	"ryanhs/chess.php": "^1.0"
}
Example Code
The code below plays a complete game of chess ... randomly.
<?php require 'vendor/autoload.php'; use \Ryanhs\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 | .  .  .  .  r  .  .  . |
 6 | .  .  .  .  .  .  .  . |
 5 | .  .  .  .  .  .  .  . |
 4 | k  .  .  .  .  .  .  . |
 3 | .  .  .  .  .  .  K  . |
 2 | .  .  .  n  .  .  .  . |
 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 |