mirror of
https://github.com/sigmasternchen/php-chess
synced 2025-03-16 00:18:55 +00:00
feat: Add bishops to game system
This commit is contained in:
parent
26f1a295a7
commit
aeda1c2e8f
2 changed files with 141 additions and 0 deletions
43
src/Game/Bishop.php
Normal file
43
src/Game/Bishop.php
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace Game;
|
||||
|
||||
class Bishop extends Piece {
|
||||
public function getName(): string {
|
||||
return "Bishop";
|
||||
}
|
||||
|
||||
public function getShort(): string {
|
||||
return "B";
|
||||
}
|
||||
|
||||
public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap {
|
||||
$result = FieldBitMap::empty();
|
||||
|
||||
$directions = [true, true, true, true];
|
||||
for ($i = 1; $i < 8; $i++) {
|
||||
for ($d = 0; $d < 4; $d++) {
|
||||
if ($directions[$d]) {
|
||||
$candidate = new Position(
|
||||
$this->position->file + $i * ($d % 2 == 0 ? 1 : -1),
|
||||
$this->position->rank + $i * ($d < 2 ? 1 : -1)
|
||||
);
|
||||
if ($candidate->isValid()) {
|
||||
if ($captureable->has($candidate)) {
|
||||
$result->add($candidate);
|
||||
$directions[$d] = false;
|
||||
} else if ($occupied->has($candidate)) {
|
||||
$directions[$d] = false;
|
||||
} else {
|
||||
$result->add($candidate);
|
||||
}
|
||||
} else {
|
||||
$directions[$d] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
98
tests/Game/BishopTest.php
Normal file
98
tests/Game/BishopTest.php
Normal file
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Game;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class BishopTest extends TestCase {
|
||||
|
||||
public function testMoves_unobstructed() {
|
||||
$subject = new Bishop(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(1, 2),
|
||||
new Position(0, 1),
|
||||
new Position(4, 5),
|
||||
new Position(5, 6),
|
||||
new Position(6, 7),
|
||||
new Position(4, 3),
|
||||
new Position(5, 2),
|
||||
new Position(6, 1),
|
||||
new Position(7, 0),
|
||||
new Position(2, 5),
|
||||
new Position(1, 6),
|
||||
new Position(0, 7),
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
public function testMoves_obstructed() {
|
||||
$subject = new Bishop(new Position(
|
||||
3, 2
|
||||
), Side::WHITE);
|
||||
|
||||
$result = $subject->getMoveCandidateMap(
|
||||
new FieldBitMap([
|
||||
new Position(4, 3),
|
||||
new Position(5, 0)
|
||||
]),
|
||||
new FieldBitMap([
|
||||
new Position(2, 1)
|
||||
]),
|
||||
FieldBitMap::empty()
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$result->equals(new FieldBitMap([
|
||||
new Position(4, 1),
|
||||
new Position(2, 1),
|
||||
new Position(2, 3),
|
||||
new Position(1, 4),
|
||||
new Position(0, 5)
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
public function testCaptureable() {
|
||||
$subject = new Bishop(
|
||||
new Position(5, 6),
|
||||
Side::WHITE,
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$subject->getCaptureableMap(true)->equals(new FieldBitMap([
|
||||
new Position(5, 6)
|
||||
]))
|
||||
);
|
||||
}
|
||||
|
||||
public function testCaptureMap() {
|
||||
$subject = new Bishop(new Position(
|
||||
5, 2
|
||||
), Side::WHITE);
|
||||
|
||||
$result = $subject->getCaptureMap(
|
||||
new FieldBitMap([
|
||||
new Position(6, 3),
|
||||
new Position(3, 4)
|
||||
])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$result->equals(new FieldBitMap([
|
||||
new Position(6, 1),
|
||||
new Position(7, 0),
|
||||
new Position(4, 3),
|
||||
new Position(4, 1),
|
||||
new Position(3, 0),
|
||||
]))
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue