refactor: Move type strings to type enum

This commit is contained in:
overflowerror 2024-01-07 12:02:58 +01:00
parent c43de25f9e
commit 8051485b65
16 changed files with 82 additions and 45 deletions

View file

@ -3,12 +3,8 @@
namespace Game; namespace Game;
class Bishop extends Piece { class Bishop extends Piece {
public function getName(): string { public function getType(): PieceType {
return "Bishop"; return PieceType::BISHOP;
}
public function getShort(): string {
return "B";
} }
public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap { public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap {

View file

@ -299,7 +299,7 @@ class Game {
$result .= "\033[30m"; $result .= "\033[30m";
} }
$short = $piece->getShort(); $short = $piece->getType()->getShort();
$result .= ($short ?: "p") . " "; $result .= ($short ?: "p") . " ";
} else { } else {
$result .= " "; $result .= " ";

View file

@ -4,12 +4,8 @@ namespace Game;
class King extends Piece { class King extends Piece {
public function getName(): string { public function getType(): PieceType {
return "King"; return PieceType::KING;
}
public function getShort(): string {
return "K";
} }
public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap { public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap {

View file

@ -4,12 +4,8 @@ namespace Game;
class Knight extends Piece { class Knight extends Piece {
public function getName(): string { public function getType(): PieceType {
return "Knight"; return PieceType::KNIGHT;
}
public function getShort(): string {
return "N";
} }
public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap { public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap {

View file

@ -27,7 +27,7 @@ class Move {
public function __toString(): string { public function __toString(): string {
return $this->piece . " " . 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() : ""); ($this->promoteTo ? $this->promoteTo->getShort() : "");
} }
} }

View file

@ -3,12 +3,9 @@
namespace Game; namespace Game;
class Pawn extends Piece { class Pawn extends Piece {
public function getName(): string {
return "Pawn";
}
public function getShort(): string { public function getType(): PieceType {
return ""; return PieceType::PAWN;
} }
public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap { public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap {

View file

@ -34,8 +34,7 @@ abstract class Piece {
return $this->position; return $this->position;
} }
abstract public function getName(): string; abstract public function getType(): PieceType;
abstract public function getShort(): string;
abstract public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap; abstract public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap;
public function getCaptureMap(FieldBitMap $occupied): FieldBitMap { public function getCaptureMap(FieldBitMap $occupied): FieldBitMap {
return $this->getMoveCandidateMap($occupied, FieldBitMap::empty(), FieldBitMap::empty()); return $this->getMoveCandidateMap($occupied, FieldBitMap::empty(), FieldBitMap::empty());
@ -45,7 +44,7 @@ abstract class Piece {
} }
public function __toString() { public function __toString() {
return $this->getShort() . $this->getPosition(); return $this->getType()->getShort() . $this->getPosition();
} }
private static function getClassForType(PieceType $type): string { private static function getClassForType(PieceType $type): string {
@ -64,7 +63,7 @@ abstract class Piece {
return King::class; 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 { 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) && return get_class($this) == get_class($piece) &&
$this->position->equals($piece->position); $this->position->equals($piece->position);
} }
public function canPromote(Position $position): bool {
return false;
}
} }

View file

@ -24,4 +24,15 @@ enum PieceType {
self::KING => "K", 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",
};
}
} }

View file

@ -4,12 +4,8 @@ namespace Game;
class Queen extends Piece { class Queen extends Piece {
public function getName(): string { public function getType(): PieceType {
return "Queen"; return PieceType::QUEEN;
}
public function getShort(): string {
return "Q";
} }
public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap { public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap {

View file

@ -4,12 +4,8 @@ namespace Game;
class Rook extends Piece { class Rook extends Piece {
public function getName(): string { public function getType(): PieceType {
return "Rook"; return PieceType::ROOK;
}
public function getShort(): string {
return "R";
} }
public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap { public function getMoveCandidateMap(FieldBitMap $occupied, FieldBitMap $captureable, FieldBitMap $threatened): FieldBitMap {

View file

@ -7,6 +7,15 @@ use PHPUnit\Framework\TestCase;
final class BishopTest extends 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() { public function testMoves_unobstructed() {
$subject = new Bishop(new Position( $subject = new Bishop(new Position(
3, 4 3, 4

View file

@ -7,6 +7,15 @@ use PHPUnit\Framework\TestCase;
final class KingTest extends 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() { public function testMoves_unobstructed() {
$subject = new King(new Position( $subject = new King(new Position(
3, 4 3, 4

View file

@ -7,6 +7,15 @@ use PHPUnit\Framework\TestCase;
final class KnightTest extends 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() { public function testMoves_unobstructed() {
$subject = new Knight(new Position( $subject = new Knight(new Position(
3, 4 3, 4

View file

@ -7,6 +7,15 @@ use PHPUnit\Framework\TestCase;
final class PawnTest extends 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() { public function testMoves_initialWhiteUnobstructed() {
$subject = new Pawn(new Position( $subject = new Pawn(new Position(
3, 1 3, 1

View file

@ -7,6 +7,15 @@ use PHPUnit\Framework\TestCase;
final class QueenTest extends 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() { public function testMoves_unobstructed() {
$subject = new Queen(new Position( $subject = new Queen(new Position(
3, 4 3, 4

View file

@ -7,6 +7,15 @@ use PHPUnit\Framework\TestCase;
final class RookTest extends 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() { public function testMoves_unobstructed() {
$subject = new Rook(new Position( $subject = new Rook(new Position(
3, 4 3, 4