smartcoder01 / chess.php
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
Requires
- php: ^7.1|^8.0
Requires (Dev)
- dms/phpunit-arraysubset-asserts: ^0.3.0
- friendsofphp/php-cs-fixer: ^2.14
- johnkary/phpunit-speedtrap: ^3.0
- phpunit/phpunit: ^7|^8|^9
This package is auto-updated.
Last update: 2025-03-10 09:57:15 UTC
README
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.
Installation
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.
Coding Style
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.
Basic Usage
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;
Documentation
You can check out the full documentation at https://ryanhs.github.io/chess.php.