From f36fba17d9fdc0d3e4068e573be53d373dd232f3 Mon Sep 17 00:00:00 2001 From: sigmasternchen Date: Sun, 27 Oct 2024 12:49:29 +0100 Subject: [PATCH] fix: Current side stays the same on apply in place --- src/Game/Game.php | 1 + tests/Game/GameTest.php | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Game/Game.php b/src/Game/Game.php index 285c0e7..ddb37b3 100644 --- a/src/Game/Game.php +++ b/src/Game/Game.php @@ -319,6 +319,7 @@ class Game { } $this->current = $this->current->getNext(); + $this->moveCache = null; $this->history->add($this); } diff --git a/tests/Game/GameTest.php b/tests/Game/GameTest.php index ff1241d..9f99dc5 100644 --- a/tests/Game/GameTest.php +++ b/tests/Game/GameTest.php @@ -875,4 +875,25 @@ final class GameTest extends TestCase { array_map(fn($p) => $p->getPosition()->file, $kings), ); } + + public function testBug_moveCacheNotClearedOnApplyInPlace() { + $subject = new Game( + [ + new King(new Position(0, 7), Side::BLACK), + new King(new Position(0, 0), Side::WHITE), + ], + Side::WHITE + ); + + $this->assertEquals(Side::WHITE, $subject->getCurrentSide()); + + $moves = $subject->getLegalMoves(); + $this->assertEquals(Side::WHITE, $moves[0]->getPiece()->getSide()); + $subject->applyInPlace($moves[0]); + + $this->assertEquals(Side::BLACK, $subject->getCurrentSide()); + + $moves = $subject->getLegalMoves(); + $this->assertEquals(Side::BLACK, $moves[0]->getPiece()->getSide()); + } } \ No newline at end of file