Skip to content

Commit

Permalink
🎨 improve code and analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
garak committed Jan 22, 2024
1 parent d20b11a commit cc61e9d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 31 deletions.
9 changes: 2 additions & 7 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,25 @@

return (new PhpCsFixer\Config())
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'@PSR12' => true,
'@PHP74Migration' => true,
'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'],
'declare_strict_types' => true,
'fully_qualified_strict_types' => true,
'phpdoc_separation' => true,
'native_function_invocation' => ['include' => ['@all']],
'new_with_braces' => true,
'no_blank_lines_after_class_opening' => true,
'no_extra_blank_lines' => true,
'no_spaces_around_offset' => ['positions' => ['inside', 'outside']],
'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
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ 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)
[![MIT License](https://poser.pugx.org/p-chess/chess/license)](https://packagist.org/packages/p-chess/chess)

## Installation
Expand Down Expand Up @@ -116,13 +115,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
| 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

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.48",
"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 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
20 changes: 10 additions & 10 deletions tests/ChessPublicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function undoMovePublic(): ?Move
return $this->undoMove();
}

public function moveToSANPublic(?Move $move): void
public function moveToSANPublic(Move $move): void
{
$this->moveToSAN($move);
}
Expand All @@ -81,17 +81,17 @@ public function perft(int $depth, bool $full = false)

if (!$this->kingAttacked($color)) {
if ($depth - 1 > 0) {
$childs = $this->perft($depth - 1, true);
if (!\is_array($childs)) {
$children = $this->perft($depth - 1, true);
if (!\is_array($children)) {
continue;
}
$nodes += $childs['nodes'];
$captures += $childs['captures'];
$enPassants += $childs['enPassants'];
$castles += $childs['castles'];
$promotions += $childs['promotions'];
$checks += $childs['checks'];
$checkmates += $childs['checkmates'];
$nodes += $children['nodes'];
$captures += $children['captures'];
$enPassants += $children['enPassants'];
$castles += $children['castles'];
$promotions += $children['promotions'];
$checks += $children['checks'];
$checkmates += $children['checkmates'];
} else {
++$nodes;
}
Expand Down
16 changes: 15 additions & 1 deletion tests/MoveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ public function testMoveToSAN(): void
));
$chess->makeMovePublic($move);
$undo = $chess->undo();
self::assertNotNull($undo);
self::assertEquals('e4', $undo->san);
self::assertEquals('e4', (string) $undo);

Expand All @@ -251,6 +252,7 @@ public function testMoveToSAN(): void
));
$chess->makeMovePublic($move);
$undo = $chess->undo();
self::assertNotNull($undo);
self::assertEquals('Nf6', $undo->san);

// normal pawn capture
Expand All @@ -264,6 +266,7 @@ public function testMoveToSAN(): void
));
$chess->makeMovePublic($move);
$undo = $chess->undo();
self::assertNotNull($undo);
self::assertEquals('exd5', $undo->san);

// en passant capture
Expand All @@ -277,6 +280,7 @@ public function testMoveToSAN(): void
));
$chess->makeMovePublic($move);
$undo = $chess->undo();
self::assertNotNull($undo);
self::assertEquals('dxe6', $undo->san);

// normal knight capture
Expand All @@ -290,6 +294,7 @@ public function testMoveToSAN(): void
));
$chess->makeMovePublic($move);
$undo = $chess->undo();
self::assertNotNull($undo);
self::assertEquals('Nxd5', $undo->san);

// promotion
Expand All @@ -304,6 +309,7 @@ public function testMoveToSAN(): void
));
$chess->makeMovePublic($move);
$undo = $chess->undo();
self::assertNotNull($undo);
self::assertEquals('d8=R', $undo->san);

// check
Expand All @@ -317,6 +323,7 @@ public function testMoveToSAN(): void
));
$chess->makeMovePublic($move);
$undo = $chess->undo();
self::assertNotNull($undo);
self::assertEquals('Rf8+', $undo->san);

// checkmate
Expand All @@ -330,6 +337,7 @@ public function testMoveToSAN(): void
));
$chess->makeMovePublic($move);
$undo = $chess->undo();
self::assertNotNull($undo);
self::assertEquals('Rb8#', $undo->san);

// ambiguous moves: row
Expand All @@ -343,6 +351,7 @@ public function testMoveToSAN(): void
));
$chess->makeMovePublic($move);
$undo = $chess->undo();
self::assertNotNull($undo);
self::assertEquals('N4xd6', $undo->san);

// ambiguous moves: rank > 0 & file > 0
Expand All @@ -356,6 +365,7 @@ public function testMoveToSAN(): void
));
$chess->makeMovePublic($move);
$undo = $chess->undo();
self::assertNotNull($undo);
self::assertEquals('Qd5xd4', $undo->san);

// ambiguous moves: col
Expand All @@ -369,6 +379,7 @@ public function testMoveToSAN(): void
));
$chess->makeMovePublic($move);
$undo = $chess->undo();
self::assertNotNull($undo);
self::assertEquals('Nexd6', $undo->san);

// ambiguous moves: col
Expand All @@ -382,6 +393,7 @@ public function testMoveToSAN(): void
));
$chess->makeMovePublic($move);
$undo = $chess->undo();
self::assertNotNull($undo);
self::assertEquals('Ncxd6', $undo->san);

// ambiguous moves: normal capture
Expand All @@ -395,6 +407,7 @@ public function testMoveToSAN(): void
));
$chess->makeMovePublic($move);
$undo = $chess->undo();
self::assertNotNull($undo);
self::assertEquals('Nxd6', $undo->san);

// ambiguous moves: normal capture
Expand All @@ -408,6 +421,7 @@ public function testMoveToSAN(): void
));
$chess->makeMovePublic($move);
$undo = $chess->undo();
self::assertNotNull($undo);
self::assertEquals('Rxd6', $undo->san);

// generate moves test
Expand All @@ -416,7 +430,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

0 comments on commit cc61e9d

Please sign in to comment.