mirror of
https://github.com/sigmasternchen/php-chess
synced 2025-03-14 23:58:53 +00:00
feat: Add kings to game system
This commit is contained in:
parent
c74f53fce4
commit
e10776dc0b
2 changed files with 128 additions and 0 deletions
35
src/Game/King.php
Normal file
35
src/Game/King.php
Normal file
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Game;
|
||||
|
||||
class King extends Piece {
|
||||
|
||||
public function getName(): string {
|
||||
return "King";
|
||||
}
|
||||
|
||||
public function getShort(): string {
|
||||
return "K";
|
||||
}
|
||||
|
||||
public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap {
|
||||
$result = FieldBitMap::empty();
|
||||
|
||||
$checkCandidate = function(Position $position) use (&$result, $occupied, $captureable, $threatened) {
|
||||
if ($position->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;
|
||||
}
|
||||
}
|
93
tests/Game/KingTest.php
Normal file
93
tests/Game/KingTest.php
Normal file
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Game;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class KingTest extends TestCase {
|
||||
|
||||
public function testMoves_unobstructed() {
|
||||
$subject = new King(new Position(
|
||||
3, 4
|
||||
), Side::WHITE);
|
||||
|
||||
$result = $subject->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),
|
||||
]))
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue