p-chess/chess
A PHP chess library that is used for chess move generation/validation, piece placement/movement, and check/checkmate/stalemate detection
Maintainers
Requires
- php: ^8.1
- ext-ctype: *
Requires (Dev)
- ergebnis/phpunit-slow-test-detector: ^2.20
- friendsofphp/php-cs-fixer: ^3.92
- imagine/imagine: ^1.5
- phpbench/phpbench: ^1.4
- phpstan/phpstan: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^10.5 || ^11.5 || ^12.5
Suggests
- imagine/imagine: To generate board images.
Provides
None
Conflicts
None
Replaces
None
MIT 256f6dcfb9b637ff1dce9b5b5e83a4397ac6375e
- ryan hs <mr.ryansilalahi.woop@gmail.com>
- Arkadiusz Kondas <arkadiusz.kondas.woop@gmail.com>
- Massimiliano Arione <garakkio.woop@gmail.com>
README
Chess is a PHP chess library 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, forked from ryanhs/chess.php
ð Latest Stable Version
ð MIT License
ð Build Status
Installation
use composer with composer require p-chess/chess
or put in your composer.json
"require": {
"p-chess/chess": "^1.0"
}
Local development
This project is designed to run inside Docker via docker compose, with helper targets in Makefile.
Prerequisites
- Docker + Docker Compose
- GNU Make
First-time setup
make build make start make install
Common development commands
make test
make stan
make cs
make coverage
make stop
You can list all available targets with:
make help
Example Code
The code below plays a complete game of chess... randomly.
<?php require 'vendor/autoload.php'; use \PChess\Chess\Chess; use \PChess\Chess\Output\UnicodeOutput; $chess = new Chess(); while (!$chess->gameOver()) { $moves = $chess->moves(); $move = $moves[random_int(0, count($moves) - 1)]; $chess->move($move->san); } echo (new UnicodeOutput())->render($chess) . PHP_EOL;
+---+---+---+---+---+---+---+---+
8 | | â | â | | | | | |
+---+---+---+---+---+---+---+---+
7 | â | | | | | | | |
+---+---+---+---+---+---+---+---+
6 | | | | | | | | |
+---+---+---+---+---+---+---+---+
5 | | | | | | | | |
+---+---+---+---+---+---+---+---+
4 | | | | | | â | â | |
+---+---+---+---+---+---+---+---+
3 | â | | | | | | | |
+---+---+---+---+---+---+---+---+
2 | | | | | | | | |
+---+---+---+---+---+---+---+---+
1 | â | | | | â | | | |
+---+---+---+---+---+---+---+---+
a b c d e f g h
Supported output formats
ASCII
Pieces are displayed with corresponding codes (e.g. "p" for pawn, "q" for queen, etc.).
<?php // use... $chess = new Chess(); echo (new AsciiOutput())->render($chess);
Unicode
Pieces are displayed like in the example above.
<?php // use... $chess = new Chess(); echo (new UnicodeOutput())->render($chess);
PNG Image
Pieces are displayed inside a png image.
<?php // use... $chess = new Chess(); $imagine = new \Imagine\Gd\Imagine(); // or \Imagine\Imagick\Imagine() $output = new ImageOutput($imagine, '/your/path/to/images', 480); header('Content-Type: image/png'); echo $output->render($chess);
See dedicated documentation for detailed instructions.
SVG Image
Pieces are displayed inside an SVG image.
<?php // use... $chess = new Chess(); echo (new SvgOutput())->render($chess);ð Image
HTML
Pieces are displayed inside an HTML table.
See dedicated documentation for detailed instructions.
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 |
Other documentation
All classes are documented in the docs directory.
