From e10776dc0bb6b4edcaca60fefc09b639942628a6 Mon Sep 17 00:00:00 2001 From: overflowerror Date: Sat, 30 Dec 2023 14:34:54 +0100 Subject: [PATCH] feat: Add kings to game system --- src/Game/King.php | 35 ++++++++++++++++ tests/Game/KingTest.php | 93 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 src/Game/King.php create mode 100644 tests/Game/KingTest.php diff --git a/src/Game/King.php b/src/Game/King.php new file mode 100644 index 0000000..4a6f342 --- /dev/null +++ b/src/Game/King.php @@ -0,0 +1,35 @@ +isValid() && !$occupied->has($position) && !$threatened->has($position)) { + $result->add($position); + } + }; + + $checkCandidate(new Position($this->position->file - 1, $this->position->rank - 1)); + $checkCandidate(new Position($this->position->file - 1, $this->position->rank + 0)); + $checkCandidate(new Position($this->position->file - 1, $this->position->rank + 1)); + $checkCandidate(new Position($this->position->file + 0, $this->position->rank - 1)); + $checkCandidate(new Position($this->position->file + 0, $this->position->rank + 1)); + $checkCandidate(new Position($this->position->file + 1, $this->position->rank - 1)); + $checkCandidate(new Position($this->position->file + 1, $this->position->rank + 0)); + $checkCandidate(new Position($this->position->file + 1, $this->position->rank + 1)); + + return $result; + } +} \ No newline at end of file diff --git a/tests/Game/KingTest.php b/tests/Game/KingTest.php new file mode 100644 index 0000000..953bc36 --- /dev/null +++ b/tests/Game/KingTest.php @@ -0,0 +1,93 @@ +getMoveCandidateMap(FieldBitMap::empty(), FieldBitMap::empty(), FieldBitMap::empty()); + + $this->assertTrue( + $result->equals(new FieldBitMap([ + new Position(2, 3), + new Position(2, 4), + new Position(2, 5), + new Position(3, 3), + new Position(3, 5), + new Position(4, 3), + new Position(4, 4), + new Position(4, 5), + ])) + ); + } + + public function testMoves_obstructed() { + $subject = new King(new Position( + 3, 0 + ), Side::WHITE); + + $result = $subject->getMoveCandidateMap( + new FieldBitMap([ + new Position(3, 1), + ]), + new FieldBitMap([ + new Position(2, 1), + ]), + new FieldBitMap([ + new Position(2, 0), + ]), + ); + + $this->assertTrue( + $result->equals(new FieldBitMap([ + new Position(2, 1), + new Position(4, 1), + new Position(4, 0), + ])) + ); + } + + public function testCaptureable() { + $subject = new King( + new Position(5, 6), + Side::WHITE, + ); + + $this->assertTrue( + $subject->getCaptureableMap(true)->equals(new FieldBitMap([ + new Position(5, 6) + ])) + ); + } + + public function testCaptureMap() { + $subject = new King(new Position( + 5, 2 + ), Side::WHITE); + + $result = $subject->getCaptureMap( + new FieldBitMap([ + new Position(4, 3), + new Position(4, 1), + new Position(6, 3), + new Position(6, 1), + ]) + ); + + $this->assertTrue( + $result->equals(new FieldBitMap([ + new Position(5, 1), + new Position(5, 3), + new Position(4, 2), + new Position(6, 2), + ])) + ); + } +} \ No newline at end of file