https://github.com/p-chess/chess
PHP Chess engine
https://github.com/p-chess/chess
chess chess-engine php
Last synced: 16 days ago
JSON representation
PHP Chess engine
- Host: GitHub
- URL: https://github.com/p-chess/chess
- Owner: p-chess
- License: mit
- Created: 2020-04-08T16:02:58.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2025-01-04T09:34:31.000Z (4 months ago)
- Last Synced: 2025-03-30T09:06:07.270Z (23 days ago)
- Topics: chess, chess-engine, php
- Language: PHP
- Size: 167 KB
- Stars: 43
- Watchers: 4
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Chess
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](https://github.com/jhlywa/chess.js) for PHP, forked from [ryanhs/chess.php](https://github.com/ryanhs/chess.php)
[](https://packagist.org/p-chess/chess)
[](https://packagist.org/packages/p-chess/chess)## 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
gameOver()) {
$moves = $chess->moves();
$move = $moves[random_int(0, count($moves) - 1)];
$chess->move($move);
}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
render($chess);
```### Unicode
Pieces are displayed like in the example above.
```php
render($chess);
```### PNG Image
Pieces are displayed inside a png image.
```php
render($chess);
```
![]()
See [dedicated documentation](docs/output_imagine.md) for detailed instructions.
### HTML
Pieces are displayed inside an HTML table.
See [dedicated documentation](docs/output_html.md) for detailed instructions.
## Performance
There is still a lot to do in this topic.
[akondas/php-grandmaster](https://github.com/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 | :fire: add cache for moveToSAN method |## Other documentation
All classes are documented in the [docs directory](docs/index.md).