项目作者: p-chess

项目描述 :
PHP Chess engine
高级语言: PHP
项目地址: git://github.com/p-chess/chess.git
创建时间: 2020-04-08T16:02:58Z
项目社区:https://github.com/p-chess/chess

开源协议:MIT License

下载


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 for PHP, forked from ryanhs/chess.php

Latest Stable Version
MIT License

Installation

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

  1. "require": {
  2. "p-chess/chess": "^1.0"
  3. }

Example Code

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

  1. <?php
  2. require 'vendor/autoload.php';
  3. use \PChess\Chess\Chess;
  4. use \PChess\Chess\Output\UnicodeOutput;
  5. $chess = new Chess();
  6. while (!$chess->gameOver()) {
  7. $moves = $chess->moves();
  8. $move = $moves[random_int(0, count($moves) - 1)];
  9. $chess->move($move);
  10. }
  11. echo (new UnicodeOutput())->render($chess) . PHP_EOL;
  1. +---+---+---+---+---+---+---+---+
  2. 8 | | | | | | | | |
  3. +---+---+---+---+---+---+---+---+
  4. 7 | | | | | | | | |
  5. +---+---+---+---+---+---+---+---+
  6. 6 | | | | | | | | |
  7. +---+---+---+---+---+---+---+---+
  8. 5 | | | | | | | | |
  9. +---+---+---+---+---+---+---+---+
  10. 4 | | | | | | | | |
  11. +---+---+---+---+---+---+---+---+
  12. 3 | | | | | | | | |
  13. +---+---+---+---+---+---+---+---+
  14. 2 | | | | | | | | |
  15. +---+---+---+---+---+---+---+---+
  16. 1 | | | | | | | | |
  17. +---+---+---+---+---+---+---+---+
  18. 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.).

  1. <?php
  2. // use...
  3. $chess = new Chess();
  4. echo (new AsciiOutput())->render($chess);

Unicode

Pieces are displayed like in the example above.

  1. <?php
  2. // use...
  3. $chess = new Chess();
  4. echo (new UnicodeOutput())->render($chess);

PNG Image

Pieces are displayed inside a png image.

  1. <?php
  2. // use...
  3. $chess = new Chess();
  4. $imagine = new \Imagine\Gd\Imagine(); // or \Imagine\Imagick\Imagine()
  5. $output = new ImageOutput($imagine, '/your/path/to/images', 480);
  6. header('Content-Type: image/png');
  7. echo $output->render($chess);

See dedicated documentation for detailed instructions.

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 :fire: add cache for moveToSAN method

Other documentation

All classes are documented in the docs directory.