p-chess / chess
这是一个用于棋子走法生成/验证、棋子放置/移动以及检查/将军/逼和检测的PHP棋类库
0.5.0
2023-12-28 09:29 UTC
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.37
- imagine/imagine: ^1.3
- johnkary/phpunit-speedtrap: ^4.0
- phpbench/phpbench: ^1.2
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.6
Suggests
- imagine/imagine: To generate board images.
README
Chess是一个PHP棋类库,用于棋子走法生成/验证、棋子放置/移动以及检查/将军/逼和检测,基本上涵盖了除AI以外的所有功能。
注意:这个项目最初是将chess.js移植到PHP,并从ryanhs/chess.php分支出来的。
安装
使用composer命令composer require p-chess/chess
或在您的composer.json文件中添加
"require": {
"p-chess/chess": "^1.0"
}
示例代码
以下代码随机玩一局完整的象棋游戏...
<?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); } echo (new UnicodeOutput())->render($chess) . PHP_EOL;
+---+---+---+---+---+---+---+---+
8 | | ♜ | ♘ | | | | | |
+---+---+---+---+---+---+---+---+
7 | ♞ | | | | | | | |
+---+---+---+---+---+---+---+---+
6 | | | | | | | | |
+---+---+---+---+---+---+---+---+
5 | | | | | | | | |
+---+---+---+---+---+---+---+---+
4 | | | | | | ♚ | ♟ | |
+---+---+---+---+---+---+---+---+
3 | ♜ | | | | | | | |
+---+---+---+---+---+---+---+---+
2 | | | | | | | | |
+---+---+---+---+---+---+---+---+
1 | ♔ | | | | ♞ | | | |
+---+---+---+---+---+---+---+---+
a b c d e f g h
支持的输出格式
ASCII
棋子用相应的代码显示(例如,“p”代表兵,“q”代表后等)。
<?php // use... $chess = new Chess(); echo (new AsciiOutput())->render($chess);
Unicode
棋子显示方式与上述示例相同。
<?php // use... $chess = new Chess(); echo (new UnicodeOutput())->render($chess);
PNG图像
棋子在png图像中显示。
<?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);
有关详细说明,请参阅专门文档。
HTML
棋子显示在HTML表格中。
有关详细说明,请参阅专门文档。
性能
在此主题上还有很多工作要做。
akondas/php-grandmaster是一个开始实验的好地方 ;)
Chess::move()
其他文档
所有类都在docs目录中有文档说明。