Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎨 various improvements #64

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -27,7 +27,7 @@ jobs:

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: vendor
key: ${{ matrix.php }}-php-${{ hashFiles('**/composer.lock') }}
Expand Down
5 changes: 1 addition & 4 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

return (new PhpCsFixer\Config())
->setRules([
'@PSR2' => true,
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => ['operators' => ['=>' => 'single_space', '=' => 'single_space']],
'blank_line_after_opening_tag' => true,
'blank_line_before_statement' => ['statements' => ['return']],
'cast_spaces' => true,
'concat_space' => ['spacing' => 'none'],
Expand All @@ -20,12 +19,10 @@
'no_unneeded_control_parentheses' => true,
'no_unused_imports' => true,
'no_whitespace_in_blank_line' => true,
'ordered_imports' => true,
'phpdoc_align' => true,
'phpdoc_no_access' => true,
'php_unit_fqcn_annotation' => true,
'self_accessor' => true,
'single_blank_line_before_namespace' => true,
'single_quote' => true,
'return_type_declaration' => true,
'trailing_comma_in_multiline' => true,
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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](https://github.com/jhlywa/chess.js) for php, froked from [ryanhs/chess.php](https://github.com/ryanhs/chess.php)
NOTE: this started as a port of [chess.js](https://github.com/jhlywa/chess.js) for php, forked from [ryanhs/chess.php](https://github.com/ryanhs/chess.php)

[![Latest Stable Version](https://poser.pugx.org/p-chess/chess/v/stable)](https://packagist.org/p-chess/chess)
[![buddy branch](https://app.buddy.works/akondas/chess/repository/branch/master/badge.svg?token=bfd952ec0cee0cb4db84dbd50ded487354ee6c9f37a7034f7c46425fed70dea7 "buddy branch")](https://app.buddy.works/akondas/chess/repository/branch/master)
Expand Down Expand Up @@ -117,13 +117,13 @@ There is still a lot to do in this topic.
### 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
| 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 [docs directory](docs/index.md).
All classes are documented in the [docs directory](docs/index.md).
7 changes: 3 additions & 4 deletions benchmarks/MoveBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
*/
final class MoveBench
{
/**
* @var Chess
*/
private $chess;
private Chess $chess;

public function init(): void
{
Expand All @@ -26,6 +23,7 @@ public function init(): void

/**
* @Revs(1000)
*
* @Iterations(5)
*/
public function benchMove(): void
Expand All @@ -35,6 +33,7 @@ public function benchMove(): void

/**
* @Revs(1000)
*
* @Iterations(5)
*/
public function benchMoves(): void
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
"ext-json": "*"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.13",
"friendsofphp/php-cs-fixer": "^3.37",
"imagine/imagine": "^1.3",
"johnkary/phpunit-speedtrap": "^4.0",
"phpbench/phpbench": "^1.2",
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": "^9.5"
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.6"
},
"suggest": {
"imagine/imagine": "To generate board images."
Expand Down
2 changes: 1 addition & 1 deletion docs/board.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ with [SAN](https://en.wikipedia.org/wiki/Algebraic_notation_(chess)):
* `algebraic()`: get the SAN of a square, as string (e.g. "e2").
This should be the same as calling `Board::SQUARES[$square]`

Such methods accept a integer square (see `SQUARES` constant).
Such methods accept an integer square (see `SQUARES` constant).

Useful constants represent starting board (`Board::DEFAULT_POSITION`) and empty board (`Board::EMPTY`)
in [FEN](https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation) notation.
4 changes: 2 additions & 2 deletions docs/output_imagine.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ and one extension between GD and Imagick.
You need to provide an instance of Imagine, and a path of images for pieces.
Names of such images need to match a color/name code.
For example, a black pawn image must be named `bp.png` (_b_ for black and _p_ for pawn), a
white queen image must me named `wq.png` (_w_ for white and _q_ for queen).
white queen image must be named `wq.png` (_w_ for white and _q_ for queen).
Refer to `Piece` class source code to get all abbreviations.
A good source for free images is [Wikimedia](https://commons.wikimedia.org/wiki/Category:SVG_chess_pieces).

Expand Down Expand Up @@ -52,5 +52,5 @@ public function __construct(
);
```

In this directory, you can find an utility script, named [get-resources.sh](get-resources.sh), that downloads
In this directory, you can find a utility script, named [get-resources.sh](get-resources.sh), that downloads
all pieces images from Wikimedia, plus a free font to use.
2 changes: 1 addition & 1 deletion src/Board.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function offsetUnset($offset): void

public function current(): ?Piece
{
return \current($this->squares) !== false ? \current($this->squares): null;
return \current($this->squares) !== false ? \current($this->squares) : null;
}

public function next(): void
Expand Down
2 changes: 1 addition & 1 deletion src/Chess.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ public function undo(): ?Move
*/
protected function generateMoves(int $square = null, bool $legal = true): array
{
$cacheKey = $this->boardHash.\json_encode($square.($legal ? '1' : '0'));
$cacheKey = $this->boardHash.\json_encode($square.($legal ? '1' : '0'), JSON_THROW_ON_ERROR);

// check cache first
if (isset($this->generateMovesCache[$cacheKey])) {
Expand Down
22 changes: 8 additions & 14 deletions src/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,23 @@

class Entry
{
/** @var Move */
public $move;
public Move $move;

/** @var string|null */
public $position;
public ?string $position;

/** @var array<string, ?int> */
public $kings;
public array $kings;

/** @var string */
public $turn;
public string $turn;

/** @var array<string, ?int> */
public $castling;
public array $castling;

/** @var int|null */
public $epSquare;
public ?int $epSquare;

/** @var int */
public $halfMoves;
public int $halfMoves;

/** @var int */
public $moveNumber;
public int $moveNumber;

/**
* @param array<string, ?int> $kings
Expand Down
2 changes: 1 addition & 1 deletion src/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
final class History
{
/** @var array<int, Entry> */
private $entries;
private array $entries;

/** @param array<int, Entry>|null $entries */
public function __construct(array $entries = null)
Expand Down
30 changes: 10 additions & 20 deletions src/Move.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,25 @@ final class Move implements \JsonSerializable
'QSIDE_CASTLE' => 64,
];

/** @var string */
public $turn;
public string $turn;

/** @var int */
public $flags;
public int $flags;

/** @var Piece */
public $piece;
public Piece $piece;

/** @var string */
public $from;
public string $from;

/** @var string */
public $to;
public string $to;

/** @var int */
public $fromSquare;
public int $fromSquare;

/** @var int */
public $toSquare;
public int $toSquare;

/** @var string|null */
public $captured;
public ?string $captured;

/** @var string|null */
public $promotion;
public ?string $promotion;

/** @var string|null */
public $san;
public ?string $san;

public function __construct(string $turn, int $flags, Piece $piece, int $from, int $to, ?string $captured = null, ?string $promotion = null)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Output/BasicOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

abstract class BasicOutput
{
/** @var string */
protected static $line = ' +---+---+---+---+---+---+---+---+'.PHP_EOL;
protected static string $line = ' +---+---+---+---+---+---+---+---+'.PHP_EOL;

protected static function getLines(bool $reversed, int $position): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Output/HtmlOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function render(Chess $chess, ?string $from = null, $identifier = null):
}

/**
* Generete an array of 64 Link objects to use in render() method.
* Generate an array of 64 Link objects to use in render() method.
* Basically, you should cycle the board, assigning values to Link for pieces that can actually move.
* There are two possible situations: 1) $from is null (move has to start); 2) $from is not null (move started).
* In first case, you should assign a "start move" link to every piece that is allowed to move
Expand Down
6 changes: 3 additions & 3 deletions src/Output/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

/**
* This is a simple value object used by HtmlOutput to represent
* a CSS class and an URL (both can be null).
* a CSS class and a URL (both can be null).
*/
final class Link
{
private ?string $class = null;
private ?string $class;

private ?string $url = null;
private ?string $url;

public function __construct(?string $class, ?string $url)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Piece.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ final class Piece implements \JsonSerializable
public const BLACK = 'b';
public const WHITE = 'w';

/**
* @var array<string, int[]>
*/
public const PAWN_OFFSETS = [
self::BLACK => [16, 32, 17, 15],
self::WHITE => [-16, -32, -17, -15],
Expand Down
2 changes: 1 addition & 1 deletion tests/FenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testGetErrorString(): void
/**
* @return array<int|string, array<int, int|string>>
*/
public function provider(): array
public static function provider(): array
{
return [
'default position' => [Board::DEFAULT_POSITION, 0],
Expand Down
4 changes: 2 additions & 2 deletions tests/MoveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public function testMoveToSAN(): void
\array_walk($moves, static function (Move $move) use ($chess): void {
$chess->moveToSANPublic($move);
});
$sans = \array_map(static function (Move $move): string {
$sans = \array_map(static function (Move $move): ?string {
return $move->san;
}, $moves);
self::assertContains('f8=Q', $sans);
Expand Down Expand Up @@ -461,7 +461,7 @@ public function testArrayMove(): void
/**
* @dataProvider gameProvider
*/
public function testSANMoveFromRealGame(string $match, string $finalFen): void
public static function testSANMoveFromRealGame(string $match, string $finalFen): void
{
$chess = new ChessPublicator();
$moves = \explode(' ', $match);
Expand Down
2 changes: 1 addition & 1 deletion tests/PerftTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PerftTest extends TestCase
/**
* @dataProvider provider
*/
public function testPerft(string $fen, int $expectedDeep1, int $expectedDeep2): void
public static function testPerft(string $fen, int $expectedDeep1, int $expectedDeep2): void
{
$chess = new ChessPublicator($fen);
self::assertEquals($expectedDeep1, $chess->perft(1));
Expand Down