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
You can check out the original chess.js
code here.
You can install this package by running this command assuming you have composer installed.
composer require ryanhs/chess.php
If you don't have composer installed you can install it directly from their website.
chess.php
follows PSR-2 coding standards. For example a function name like do_something()
gets transformed into doSomething()
.
Be sure to correctly indent your code and add comments. Adding comments will help other developers know what a block of code does.
Other then that all contributions are welcomed.
Here is a sample code that plays a random game of chess.
<?php
require __DIR__ . '/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;
You can check out the full documentation at https://ryanhs.github.io/chess.php.