Skip to content

Commit

Permalink
TDDed the Chess\Variant\Chess960\K class
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Jan 20, 2025
1 parent 6404c73 commit a6818ed
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
19 changes: 19 additions & 0 deletions src/Variant/Chess960/K.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,23 @@

class K extends ClassicalK
{
/**
* Returns the piece's moves.
*
* Unlike in classical chess, in Chess960 there are positions where the
* king can be moved to the same square where it is located in order to
* castle. Castling is thus possible by double-clicking on the square
* where the king is currently located.
*
* @return array
*/
public function moveSqs(): array
{
$sqs = [
...$this->moveSqs(),
...[$this->sq],
];

return array_filter(array_unique($sqs));
}
}
30 changes: 19 additions & 11 deletions tests/unit/Variant/Chess960/BoardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -803,17 +803,12 @@ public function play_RNQNBBKR_castle_short()
*/
public function play_lan_RNQNBBKR_castle_short()
{
$startPos = ['R', 'N', 'Q', 'N', 'B', 'B', 'K', 'R'];

$board = new Board($startPos);

$board->playLan('w', 'g2g3');
$board->playLan('b', 'g7g6');
$board->playLan('w', 'f1g2');
$board->playLan('b', 'f8g7');
$board->playLan('w', 'g1g1');
$expectedLegal = [
'f1',
'g1',
];

$expected = [
$expectedArray = [
7 => [ 'r', 'n', 'q', 'n', 'b', '.', 'k', 'r' ],
6 => [ 'p', 'p', 'p', 'p', 'p', 'p', 'b', 'p' ],
5 => [ '.', '.', '.', '.', '.', '.', 'p', '.' ],
Expand All @@ -824,6 +819,19 @@ public function play_lan_RNQNBBKR_castle_short()
0 => [ 'R', 'N', 'Q', 'N', 'B', 'R', 'K', '.' ],
];

$this->assertSame($expected, $board->toArray());
$startPos = ['R', 'N', 'Q', 'N', 'B', 'B', 'K', 'R'];

$board = new Board($startPos);

$board->playLan('w', 'g2g3');
$board->playLan('b', 'g7g6');
$board->playLan('w', 'f1g2');
$board->playLan('b', 'f8g7');

$this->assertSame($expectedLegal, $board->legal('g1'));

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

$this->assertSame($expectedArray, $board->toArray());
}
}

0 comments on commit a6818ed

Please sign in to comment.