Loading...
Learn Design Chess Game — a key concept for LLD interviews.
Chess has 6 piece types: King, Queen, Rook, Bishop, Knight, Pawn. Each moves differently but shares common properties (color, position, alive/captured). You need a clean hierarchy that lets the board call move validation on any piece polymorphically.
enum Color { WHITE, BLACK }
abstract class Piece {
protected color: Color
protected position: Position
protected alive: boolean = true
abstract getValidMoves(board: Board): Position[]
abstract getSymbol(): string
canMoveTo(board: Board, target: Position): boolean {
return this.getValidMoves(board).some(p => p.equals(target))
}
}
class King extends Piece {
getValidMoves(board: Board): Position[] {
// One square in any direction + castling
}
}
class Knight extends Piece {
getValidMoves(board: Board): Position[] {
// L-shape moves
}
}How should you design the piece class hierarchy?