PHP Classes

PHP Chess Engine: Manage the moves of a chess game

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 59 All time: 10,513 This week: 206Up
Version License PHP version Categories
chess 1.0.0Custom (specified...5PHP 5, Graphics, Text processing, Games
Description 

Authors

ryan hs
Arkadiusz Kondas
Massimiliano Arione


Contributor

This package can manage the moves of a chess game.

It can take keep track of the places of the pieces in chess board and process several types operations to manage the game. Currently it can:

- Check if the game is over
- Retrieve the list of moves of the pieces
- Process a move of a piece
- Render a chess board either in a text format or as an image

Picture of Massimiliano Arione
  Performance   Level  
Name: Massimiliano Arione <contact>
Classes: 2 packages by
Country: Italy Italy
Age: 50
All time rank: 4418173 in Italy Italy
Week rank: 170 Up7 in Italy Italy Up
Innovation award
Innovation award
Nominee: 1x

Example

Chess

Chess 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 started as a port of chess.js for php, froked from ryanhs/chess.php

Latest Stable Version buddy branch MIT License

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

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

Supported output formats

ASCII

Pieces are displayed with corresponding codes (e.g. "p" for pawn, "q" for queen, etc.).

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

Unicode

Pieces are displayed like in the example above.

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

PNG Image

Pieces are displayed inside a png image.

<?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);

<img src="https://user-images.githubusercontent.com/179866/112304837-411be280-8c9e-11eb-8333-c2489f9bef05.png">

See dedicated documentation for a 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


  Files folder image Files (34)  
File Role Description
Files folder imagebenchmarks (1 file)
Files folder imagedocs (2 files)
Files folder imagesrc (6 files, 1 directory)
Files folder imagetests (11 files)
Accessible without login Plain text file .php_cs Example Example script
Accessible without login Plain text file buddy.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpbench.json Data Auxiliary data
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Example Example script

  Files folder image Files (34)  /  benchmarks  
File Role Description
  Plain text file MoveBench.php Class Class source

  Files folder image Files (34)  /  docs  
File Role Description
  Accessible without login Plain text file get-resources.sh Data Auxiliary data
  Accessible without login Plain text file output_imagine.md Example Example script

  Files folder image Files (34)  /  src  
File Role Description
Files folder imageOutput (6 files)
  Plain text file Board.php Class Class source
  Plain text file Chess.php Class Class source
  Plain text file History.php Class Class source
  Plain text file Move.php Class Class source
  Plain text file Piece.php Class Class source
  Plain text file Validation.php Class Class source

  Files folder image Files (34)  /  src  /  Output  
File Role Description
  Plain text file AsciiOutput.php Class Class source
  Plain text file BasicOutput.php Class Class source
  Plain text file ImagineOutput.php Class Class source
  Plain text file OutputInterface.php Class Class source
  Plain text file PgnOutput.php Class Class source
  Plain text file UnicodeOutput.php Class Class source

  Files folder image Files (34)  /  tests  
File Role Description
  Plain text file AttackTest.php Class Class source
  Plain text file ChessPublicator.php Class Class source
  Plain text file ConstructorTest.php Class Class source
  Plain text file FenTest.php Class Class source
  Plain text file GameTest.php Class Class source
  Plain text file MiscTest.php Class Class source
  Plain text file MoveTest.php Class Class source
  Plain text file PerftTest.php Class Class source
  Plain text file PgnTest.php Class Class source
  Plain text file PiecePlacementTest.php Class Class source
  Plain text file PieceTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:59
This week:0
All time:10,513
This week:206Up