diff --git a/src/Game/Bishop.php b/src/Game/Bishop.php index 0bd5e80..d160b1a 100644 --- a/src/Game/Bishop.php +++ b/src/Game/Bishop.php @@ -3,12 +3,8 @@ namespace Game; class Bishop extends Piece { - public function getName(): string { - return "Bishop"; - } - - public function getShort(): string { - return "B"; + public function getType(): PieceType { + return PieceType::BISHOP; } public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap { diff --git a/src/Game/Game.php b/src/Game/Game.php index d3db711..c4eaa37 100644 --- a/src/Game/Game.php +++ b/src/Game/Game.php @@ -299,7 +299,7 @@ class Game { $result .= "\033[30m"; } - $short = $piece->getShort(); + $short = $piece->getType()->getShort(); $result .= ($short ?: "p") . " "; } else { $result .= " "; diff --git a/src/Game/King.php b/src/Game/King.php index 7a8d8ee..ba21a92 100644 --- a/src/Game/King.php +++ b/src/Game/King.php @@ -4,12 +4,8 @@ namespace Game; class King extends Piece { - public function getName(): string { - return "King"; - } - - public function getShort(): string { - return "K"; + public function getType(): PieceType { + return PieceType::KING; } public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap { diff --git a/src/Game/Knight.php b/src/Game/Knight.php index 11ea38d..7bdf625 100644 --- a/src/Game/Knight.php +++ b/src/Game/Knight.php @@ -4,12 +4,8 @@ namespace Game; class Knight extends Piece { - public function getName(): string { - return "Knight"; - } - - public function getShort(): string { - return "N"; + public function getType(): PieceType { + return PieceType::KNIGHT; } public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap { diff --git a/src/Game/Move.php b/src/Game/Move.php index d8af85b..0c4dd9e 100644 --- a/src/Game/Move.php +++ b/src/Game/Move.php @@ -27,7 +27,7 @@ class Move { public function __toString(): string { return $this->piece . " " . - $this->piece->getShort() . ($this->captures ? "x" : "") . $this->target . + $this->piece->getType()->getShort() . ($this->captures ? "x" : "") . $this->target . ($this->promoteTo ? $this->promoteTo->getShort() : ""); } } \ No newline at end of file diff --git a/src/Game/Pawn.php b/src/Game/Pawn.php index 0a8464a..7a1c206 100644 --- a/src/Game/Pawn.php +++ b/src/Game/Pawn.php @@ -3,12 +3,9 @@ namespace Game; class Pawn extends Piece { - public function getName(): string { - return "Pawn"; - } - public function getShort(): string { - return ""; + public function getType(): PieceType { + return PieceType::PAWN; } public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap { diff --git a/src/Game/Piece.php b/src/Game/Piece.php index c2a4ceb..6713d68 100644 --- a/src/Game/Piece.php +++ b/src/Game/Piece.php @@ -34,8 +34,7 @@ abstract class Piece { return $this->position; } - abstract public function getName(): string; - abstract public function getShort(): string; + abstract public function getType(): PieceType; abstract public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap; public function getCaptureMap(FieldBitMap $occupied): FieldBitMap { return $this->getMoveCandidateMap($occupied, FieldBitMap::empty(), FieldBitMap::empty()); @@ -45,7 +44,7 @@ abstract class Piece { } public function __toString() { - return $this->getShort() . $this->getPosition(); + return $this->getType()->getShort() . $this->getPosition(); } private static function getClassForType(PieceType $type): string { @@ -64,7 +63,7 @@ abstract class Piece { return King::class; } - throw new \RuntimeException("unknown piecetype " . $type); + throw new \RuntimeException("unknown piecetype " . $type->getLong()); } public static function ofType(PieceType $type, Position $position, Side $side): Piece { @@ -83,8 +82,4 @@ abstract class Piece { return get_class($this) == get_class($piece) && $this->position->equals($piece->position); } - - public function canPromote(Position $position): bool { - return false; - } } \ No newline at end of file diff --git a/src/Game/PieceType.php b/src/Game/PieceType.php index 73337f5..c449b2c 100644 --- a/src/Game/PieceType.php +++ b/src/Game/PieceType.php @@ -24,4 +24,15 @@ enum PieceType { self::KING => "K", }; } + + public function getLong(): string { + return match ($this) { + self::PAWN => "Pawn", + self::BISHOP => "Bishop", + self::KNIGHT => "Knight", + self::ROOK => "Rook", + self::QUEEN => "Queen", + self::KING => "King", + }; + } } \ No newline at end of file diff --git a/src/Game/Queen.php b/src/Game/Queen.php index a9cdd8e..a604243 100644 --- a/src/Game/Queen.php +++ b/src/Game/Queen.php @@ -4,12 +4,8 @@ namespace Game; class Queen extends Piece { - public function getName(): string { - return "Queen"; - } - - public function getShort(): string { - return "Q"; + public function getType(): PieceType { + return PieceType::QUEEN; } public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap { diff --git a/src/Game/Rook.php b/src/Game/Rook.php index 396d0ee..be6280d 100644 --- a/src/Game/Rook.php +++ b/src/Game/Rook.php @@ -4,12 +4,8 @@ namespace Game; class Rook extends Piece { - public function getName(): string { - return "Rook"; - } - - public function getShort(): string { - return "R"; + public function getType(): PieceType { + return PieceType::ROOK; } public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap { diff --git a/tests/Game/BishopTest.php b/tests/Game/BishopTest.php index 72f71fb..e972caa 100644 --- a/tests/Game/BishopTest.php +++ b/tests/Game/BishopTest.php @@ -7,6 +7,15 @@ use PHPUnit\Framework\TestCase; final class BishopTest extends TestCase { + public function testType() { + $subject = new Bishop( + new Position(0, 0), + Side::WHITE, + ); + + $this->assertEquals(PieceType::BISHOP, $subject->getType()); + } + public function testMoves_unobstructed() { $subject = new Bishop(new Position( 3, 4 diff --git a/tests/Game/KingTest.php b/tests/Game/KingTest.php index 953bc36..20c99e3 100644 --- a/tests/Game/KingTest.php +++ b/tests/Game/KingTest.php @@ -7,6 +7,15 @@ use PHPUnit\Framework\TestCase; final class KingTest extends TestCase { + public function testType() { + $subject = new King( + new Position(0, 0), + Side::WHITE, + ); + + $this->assertEquals(PieceType::KING, $subject->getType()); + } + public function testMoves_unobstructed() { $subject = new King(new Position( 3, 4 diff --git a/tests/Game/KnightTest.php b/tests/Game/KnightTest.php index 42913bf..f4a5a6c 100644 --- a/tests/Game/KnightTest.php +++ b/tests/Game/KnightTest.php @@ -7,6 +7,15 @@ use PHPUnit\Framework\TestCase; final class KnightTest extends TestCase { + public function testType() { + $subject = new Knight( + new Position(0, 0), + Side::WHITE, + ); + + $this->assertEquals(PieceType::KNIGHT, $subject->getType()); + } + public function testMoves_unobstructed() { $subject = new Knight(new Position( 3, 4 diff --git a/tests/Game/PawnTest.php b/tests/Game/PawnTest.php index e2065f1..c8d9e1a 100644 --- a/tests/Game/PawnTest.php +++ b/tests/Game/PawnTest.php @@ -7,6 +7,15 @@ use PHPUnit\Framework\TestCase; final class PawnTest extends TestCase { + public function testType() { + $subject = new Pawn( + new Position(0, 0), + Side::WHITE, + ); + + $this->assertEquals(PieceType::PAWN, $subject->getType()); + } + public function testMoves_initialWhiteUnobstructed() { $subject = new Pawn(new Position( 3, 1 diff --git a/tests/Game/QueenTest.php b/tests/Game/QueenTest.php index cad7af8..55aa5eb 100644 --- a/tests/Game/QueenTest.php +++ b/tests/Game/QueenTest.php @@ -7,6 +7,15 @@ use PHPUnit\Framework\TestCase; final class QueenTest extends TestCase { + public function testType() { + $subject = new Queen( + new Position(0, 0), + Side::WHITE, + ); + + $this->assertEquals(PieceType::QUEEN, $subject->getType()); + } + public function testMoves_unobstructed() { $subject = new Queen(new Position( 3, 4 diff --git a/tests/Game/RookTest.php b/tests/Game/RookTest.php index bd6d55e..49ff60b 100644 --- a/tests/Game/RookTest.php +++ b/tests/Game/RookTest.php @@ -7,6 +7,15 @@ use PHPUnit\Framework\TestCase; final class RookTest extends TestCase { + public function testType() { + $subject = new Rook( + new Position(0, 0), + Side::WHITE, + ); + + $this->assertEquals(PieceType::ROOK, $subject->getType()); + } + public function testMoves_unobstructed() { $subject = new Rook(new Position( 3, 4