From 73b090e9b7a578083dddb0547b00fdbab6a8a834 Mon Sep 17 00:00:00 2001 From: overflowerror Date: Sat, 6 Jan 2024 21:24:46 +0100 Subject: [PATCH] fix: Pawn can not capture forward --- src/Game/Pawn.php | 6 ++++-- tests/Game/PawnTest.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Game/Pawn.php b/src/Game/Pawn.php index 5efc752..bc83e9c 100644 --- a/src/Game/Pawn.php +++ b/src/Game/Pawn.php @@ -25,10 +25,12 @@ class Pawn extends Piece { $initial = new Position($file, $rank + 2 * $direction); - if (!$occupied->has($regular)) { + // checking for collisions in the captureable map works because there is always + // a pawn before the en passant square. + if (!$occupied->has($regular) && !$captureable->has($regular)) { $result->add($regular); - if (!$this->hasMoved && !$occupied->has($initial)) { + if (!$this->hasMoved && !$occupied->has($initial) && !$captureable->has($initial)) { $result->add($initial); } } diff --git a/tests/Game/PawnTest.php b/tests/Game/PawnTest.php index c7d27a1..e3c2454 100644 --- a/tests/Game/PawnTest.php +++ b/tests/Game/PawnTest.php @@ -155,6 +155,22 @@ final class PawnTest extends TestCase { ); } + public function testMoves_bug_captureForwardNotAllowed() { + $subject = new Pawn( + new Position(4, 3), + Side::BLACK, + true + ); + + $result = $subject->getMoveCandidateMap( + FieldBitMap::empty(), + new FieldBitMap([new Position(4, 2)]), + FieldBitMap::empty()); + $this->assertTrue( + $result->isEmpty() + ); + } + public function testCaptureable_default() { $subject = new Pawn( new Position(3, 4),