mirror of
https://github.com/sigmasternchen/php-chess
synced 2025-03-14 23:58:53 +00:00
feat: Add knights to game system
This commit is contained in:
parent
aeda1c2e8f
commit
2624fb191a
2 changed files with 126 additions and 0 deletions
33
src/Game/Knight.php
Normal file
33
src/Game/Knight.php
Normal file
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace Game;
|
||||
|
||||
class Knight extends Piece {
|
||||
|
||||
public function getName(): string {
|
||||
return "Knight";
|
||||
}
|
||||
|
||||
public function getShort(): string {
|
||||
return "N";
|
||||
}
|
||||
|
||||
public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap {
|
||||
$result = FieldBitMap::empty();
|
||||
|
||||
for ($i = 0; $i < 8; $i++) {
|
||||
$fileOffset = ($i % 2 + 1) * ($i < 4 ? 1 : -1);
|
||||
$rankOffset = (($i + 1) % 2 + 1) * (($i & 2) == 0 ? 1 : -1);
|
||||
|
||||
$candidate = new Position(
|
||||
$this->position->file + $fileOffset,
|
||||
$this->position->rank + $rankOffset
|
||||
);
|
||||
if ($candidate->isValid() && !$occupied->has($candidate)) {
|
||||
$result->add($candidate);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
93
tests/Game/KnightTest.php
Normal file
93
tests/Game/KnightTest.php
Normal file
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Game;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class KnightTest extends TestCase {
|
||||
|
||||
public function testMoves_unobstructed() {
|
||||
$subject = new Knight(new Position(
|
||||
3, 4
|
||||
), Side::WHITE);
|
||||
|
||||
$result = $subject->getMoveCandidateMap(FieldBitMap::empty(), FieldBitMap::empty(), FieldBitMap::empty());
|
||||
|
||||
$this->assertTrue(
|
||||
$result->equals(new FieldBitMap([
|
||||
new Position(4, 2),
|
||||
new Position(4, 6),
|
||||
new Position(2, 2),
|
||||
new Position(2, 6),
|
||||
new Position(5, 3),
|
||||
new Position(5, 5),
|
||||
new Position(1, 3),
|
||||
new Position(1, 5),
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
public function testMoves_obstructed() {
|
||||
$subject = new Knight(new Position(
|
||||
3, 1
|
||||
), Side::WHITE);
|
||||
|
||||
$result = $subject->getMoveCandidateMap(
|
||||
new FieldBitMap([
|
||||
new Position(2, 3),
|
||||
new Position(5, 2)
|
||||
]),
|
||||
new FieldBitMap([
|
||||
new Position(4, 3)
|
||||
]),
|
||||
FieldBitMap::empty()
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$result->equals(new FieldBitMap([
|
||||
new Position(4, 3),
|
||||
new Position(1, 2),
|
||||
new Position(1, 0),
|
||||
new Position(5, 0)
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
public function testCaptureable() {
|
||||
$subject = new Knight(
|
||||
new Position(5, 6),
|
||||
Side::WHITE,
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$subject->getCaptureableMap(true)->equals(new FieldBitMap([
|
||||
new Position(5, 6)
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
public function testCaptureMap() {
|
||||
$subject = new Knight(new Position(
|
||||
5, 2
|
||||
), Side::WHITE);
|
||||
|
||||
$result = $subject->getCaptureMap(
|
||||
new FieldBitMap([
|
||||
new Position(6, 4),
|
||||
new Position(4, 4),
|
||||
new Position(3, 1),
|
||||
new Position(7, 1),
|
||||
])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$result->equals(new FieldBitMap([
|
||||
new Position(3, 3),
|
||||
new Position(7, 3),
|
||||
new Position(6, 0),
|
||||
new Position(4, 0),
|
||||
]))
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue