Skip to content

Commit

Permalink
Refactored Chess\Variant\Classical\Board
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed May 6, 2024
1 parent cb453f5 commit 73ec8b2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 92 deletions.
23 changes: 4 additions & 19 deletions src/Variant/Classical/Board.php
Original file line number Diff line number Diff line change
Expand Up @@ -1092,29 +1092,14 @@ public function isFiftyMoveDraw(): bool
}

/**
* Returns the legal FEN positions of a piece.
* Returns the legal squares of a piece.
*
* @param string $sq
* @return object|null
* @return array
*/
public function legal(string $sq): ?object
public function legal(string $sq): array
{
if ($piece = $this->getPieceBySq($sq)) {
$fen = [];
foreach ($piece->sqs() as $sq) {
if ($res = $piece->fen($piece->getColor(), $sq)) {
$fen[$sq] = $res;
}
}

return (object) [
'color' => $piece->getColor(),
'id' => $piece->getId(),
'fen' => (object) $fen,
];
}

return null;
return array_values($this->getPieceBySq($sq)->sqs());
}

/**
Expand Down
63 changes: 9 additions & 54 deletions tests/unit/Variant/Classical/BoardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2413,14 +2413,7 @@ public function undo_e4_e5_Nf3_Nf6_Be2_Be7_O_O_legal()

$board = $board->undo();

$expected = (object) [
'color' => 'w',
'id' => 'K',
'fen' => (object) [
'f1' => 'rnbqk2r/ppppbppp/5n2/4p3/4P3/5N2/PPPPBPPP/RNBQ1K1R b kq -',
'g1' => 'rnbqk2r/ppppbppp/5n2/4p3/4P3/5N2/PPPPBPPP/RNBQ1RK1 b kq -',
],
];
$expected = ['f1', 'g1'];

$this->assertEquals($expected, $board->legal('e1'));
}
Expand Down Expand Up @@ -2548,15 +2541,7 @@ public function legal_e4_e5_Nf3_Nf6_Bc4_Be7()
$board->play('w', 'Bc4');
$board->play('b', 'Be7');

$expected = (object) [
'color' => 'w',
'id' => 'K',
'fen' => (object) [
'e2' => 'rnbqk2r/ppppbppp/5n2/4p3/2B1P3/5N2/PPPPKPPP/RNBQ3R b kq -',
'f1' => 'rnbqk2r/ppppbppp/5n2/4p3/2B1P3/5N2/PPPP1PPP/RNBQ1K1R b kq -',
'g1' => 'rnbqk2r/ppppbppp/5n2/4p3/2B1P3/5N2/PPPP1PPP/RNBQ1RK1 b kq -',
],
];
$expected = ['e2', 'f1', 'g1'];

$this->assertEquals($expected, $board->legal('e1'));
}
Expand Down Expand Up @@ -2823,14 +2808,7 @@ public function play_h4_a5_h5_g5()
$board->play('w', 'h5');
$board->play('b', 'g5');

$expected = (object) [
'color' => 'w',
'id' => 'P',
'fen' => (object) [
'h6' => 'rnbqkbnr/1ppppp1p/7P/p5p1/8/8/PPPPPPP1/RNBQKBNR b KQkq -',
'g6' => 'rnbqkbnr/1ppppp1p/6P1/p7/8/8/PPPPPPP1/RNBQKBNR b KQkq -',
],
];
$expected = ['h6', 'g6'];

$this->assertEquals($expected, $board->legal('h5'));
}
Expand All @@ -2856,6 +2834,8 @@ public function play_lan_h2h4_a7a5_h4h5_g7g5()
],
];

$expected = ['h6', 'g6'];

$this->assertEquals($expected, $board->legal('h5'));
}

Expand All @@ -2866,11 +2846,7 @@ public function legal_a1()
{
$board = new Board();

$expected = (object) [
'color' => 'w',
'id' => 'R',
'fen' => (object) [],
];
$expected = [];

$this->assertEquals($expected, $board->legal('a1'));
}
Expand All @@ -2882,14 +2858,7 @@ public function legal_e2()
{
$board = new Board();

$expected = (object) [
'color' => 'w',
'id' => 'P',
'fen' => (object) [
'e3' => 'rnbqkbnr/pppppppp/8/8/8/4P3/PPPP1PPP/RNBQKBNR b KQkq -',
'e4' => 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3',
],
];
$expected = ['e3', 'e4'];

$this->assertEquals($expected, $board->legal('e2'));
}
Expand All @@ -2903,14 +2872,7 @@ public function legal_e7()

$board->playLan('w', 'e2e4');

$expected = (object) [
'color' => 'b',
'id' => 'P',
'fen' => (object) [
'e6' => 'rnbqkbnr/pppp1ppp/4p3/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq -',
'e5' => 'rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6',
],
];
$expected = ['e6', 'e5'];

$this->assertEquals($expected, $board->legal('e7'));
}
Expand All @@ -2922,14 +2884,7 @@ public function legal_e1()
{
$board = (new SanPlay('1.f4 e5 2.e4 a6 3.Bc4 a5 4.Nh3 Qh4+'))->validate()->getBoard();

$expected = (object) [
'color' => 'w',
'id' => 'K',
'fen' => (object) [
'e2' => 'rnb1kbnr/1ppp1ppp/8/p3p3/2B1PP1q/7N/PPPPK1PP/RNBQ3R b kq -',
'f1' => 'rnb1kbnr/1ppp1ppp/8/p3p3/2B1PP1q/7N/PPPP2PP/RNBQ1K1R b kq -',
],
];
$expected = ['e2', 'f1'];

$this->assertEquals($expected, $board->legal('e1'));
}
Expand Down
23 changes: 4 additions & 19 deletions tests/unit/Variant/Classical/FEN/StrToBoardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,15 +595,7 @@ public function disambiguate_f8_rook_legal()
$board = (new StrToBoard('r1Q2rk1/p2p1pp1/1ppq1n1p/4p3/1PP1P3/P4N2/1B3PPP/R4RK1 b - -'))
->create();

$expected = (object) [
'color' => 'b',
'id' => 'R',
'fen' => (object) [
'e8' => 'r1Q1r1k1/p2p1pp1/1ppq1n1p/4p3/1PP1P3/P4N2/1B3PPP/R4RK1 w - -',
'd8' => 'r1Qr2k1/p2p1pp1/1ppq1n1p/4p3/1PP1P3/P4N2/1B3PPP/R4RK1 w - -',
'c8' => 'r1r3k1/p2p1pp1/1ppq1n1p/4p3/1PP1P3/P4N2/1B3PPP/R4RK1 w - -',
],
];
$expected = ['e8', 'd8', 'c8'];

$this->assertEquals($expected, $board->legal('f8'));
}
Expand All @@ -616,16 +608,7 @@ public function disambiguate_c8_knight_legal()
$board = (new StrToBoard('k1N3N1/8/2K5/8/8/8/8/8 w - - 0 1'))
->create();

$expected = (object) [
'color' => 'w',
'id' => 'N',
'fen' => (object) [
'a7' => 'k5N1/N7/2K5/8/8/8/8/8 b - -',
'b6' => 'k5N1/8/1NK5/8/8/8/8/8 b - -',
'd6' => 'k5N1/8/2KN4/8/8/8/8/8 b - -',
'e7' => 'k5N1/4N3/2K5/8/8/8/8/8 b - -',
],
];
$expected = ['a7', 'b6', 'd6', 'e7'];

$this->assertEquals($expected, $board->legal('c8'));
}
Expand All @@ -648,6 +631,8 @@ public function disambiguate_g8_knight_legal()
],
];

$expected = ['e7', 'f6', 'h6'];

$this->assertEquals($expected, $board->legal('g8'));
}

Expand Down

0 comments on commit 73ec8b2

Please sign in to comment.