diff --git a/src/Game/Rook.php b/src/Game/Rook.php new file mode 100644 index 0000000..396d0ee --- /dev/null +++ b/src/Game/Rook.php @@ -0,0 +1,88 @@ +position->file, $this->position->rank + $offset); + if (!$candidate->isValid()) { + break; + } + + if ($occupied->has($candidate)) { + break; + } + + $result->add($candidate); + + if ($captureable->has($candidate)) { + break; + } + } + + for ($offset = -1; $offset > -8; $offset--) { + $candidate = new Position($this->position->file, $this->position->rank + $offset); + if (!$candidate->isValid()) { + break; + } + + if ($occupied->has($candidate)) { + break; + } + + $result->add($candidate); + + if ($captureable->has($candidate)) { + break; + } + } + + for ($offset = 1; $offset < 8; $offset++) { + $candidate = new Position($this->position->file + $offset, $this->position->rank); + if (!$candidate->isValid()) { + break; + } + + if ($occupied->has($candidate)) { + break; + } + + $result->add($candidate); + + if ($captureable->has($candidate)) { + break; + } + } + + for ($offset = -1; $offset > -8; $offset--) { + $candidate = new Position($this->position->file + $offset, $this->position->rank); + if (!$candidate->isValid()) { + break; + } + + if ($occupied->has($candidate)) { + break; + } + + $result->add($candidate); + + if ($captureable->has($candidate)) { + break; + } + } + + return $result; + } +} \ No newline at end of file diff --git a/tests/Game/RookTest.php b/tests/Game/RookTest.php new file mode 100644 index 0000000..bd6d55e --- /dev/null +++ b/tests/Game/RookTest.php @@ -0,0 +1,99 @@ +getMoveCandidateMap(FieldBitMap::empty(), FieldBitMap::empty(), FieldBitMap::empty()); + + $this->assertTrue( + $result->equals(new FieldBitMap([ + new Position(3, 0), + new Position(3, 1), + new Position(3, 2), + new Position(3, 3), + new Position(3, 5), + new Position(3, 6), + new Position(3, 7), + new Position(0, 4), + new Position(1, 4), + new Position(2, 4), + new Position(4, 4), + new Position(5, 4), + new Position(6, 4), + new Position(7, 4), + + ])) + ); + } + + public function testMoves_obstructed() { + $subject = new Rook(new Position( + 3, 0 + ), Side::WHITE); + + $result = $subject->getMoveCandidateMap( + new FieldBitMap([ + new Position(3, 3), + new Position(5, 0) + ]), + new FieldBitMap([ + new Position(1, 0) + ]), + FieldBitMap::empty() + ); + + $this->assertTrue( + $result->equals(new FieldBitMap([ + new Position(3, 1), + new Position(3, 2), + new Position(1, 0), + new Position(2, 0), + new Position(4, 0), + ])) + ); + } + + public function testCaptureable() { + $subject = new Rook( + new Position(5, 6), + Side::WHITE, + ); + + $this->assertTrue( + $subject->getCaptureableMap(true)->equals(new FieldBitMap([ + new Position(5, 6) + ])) + ); + } + + public function testCaptureMap() { + $subject = new Rook(new Position( + 5, 2 + ), Side::WHITE); + + $result = $subject->getCaptureMap( + new FieldBitMap([ + new Position(4, 2), + new Position(6, 2), + new Position(5, 4), + ]) + ); + + $this->assertTrue( + $result->equals(new FieldBitMap([ + new Position(5, 0), + new Position(5, 1), + new Position(5, 3), + ])) + ); + } +} \ No newline at end of file