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;
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 {

View file

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

View file

@ -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 {

View file

@ -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 {

View file

@ -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() : "");
}
}

View file

@ -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 {

View file

@ -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;
}
}

View file

@ -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",
};
}
}

View file

@ -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 {

View file

@ -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 {

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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