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

🎨 improve code and analysis #65

Merged
merged 2 commits into from
Sep 15, 2024
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: 1 addition & 3 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
return (new PhpCsFixer\Config())
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'@PHP74Migration' => true,
'binary_operator_spaces' => ['operators' => ['=>' => 'single_space', '=' => 'single_space']],
'blank_line_before_statement' => ['statements' => ['return']],
'cast_spaces' => true,
Expand All @@ -12,8 +12,6 @@
'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,
Expand Down
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ detection - basically everything but the AI.
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"ext-json": "*"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.37",
"friendsofphp/php-cs-fixer": "^3.48",
"imagine/imagine": "^1.3",
"johnkary/phpunit-speedtrap": "^4.0",
"phpbench/phpbench": "^1.2",
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
14 changes: 14 additions & 0 deletions 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 Down