Chess Game
hard · rules engine, move validation
Brief-reported interview attribution (unverified): Microsoft, Amazon, Google
How to approach an LLD roundRequirements
- Functional: standard setup, legal moves per piece (pawn single/double start, diagonal capture; knight jumps; sliders need a clear path; king one square), turn alternation, win by capturing the king, no moves after the game ends.
- Non-functional: each piece's movement rule must live in that piece (adding a piece must not touch the game); the board geometry must be separate from the rules.
- Out of scope: check/checkmate, castling, en passant, promotion.
Statement
Implement a simplified chess game on an 8×8 board. Rows 0..7
(top to bottom), columns 0..7 (left to right). Row 0 is Black's back rank, row 7
is White's. White pawns move up (decreasing row), Black pawns move down
(increasing row). Pieces are strings: "wP", "wN", "wB", "wQ", "wK",
"wR" for White, "b…" for Black, "" for an empty square. There is **no check
or checkmate** in this version: a player wins by capturing the opponent's king.
Castling, en passant, and promotion are not implemented (they are listed as
follow-ups on the page). The constructor accepts an optional starting board for
testing.
class ChessGame:
def __init__(self, board=None):
# board: optional 8x8 list of lists; default is the standard starting position
def move(self, from_sq, to_sq):
# from_sq, to_sq: (row, col) tuples
# -> "ok" | "invalid" | "white wins" | "black wins"
def board(self):
# -> 8x8 list of lists (a copy)
def current_player(self):
# -> "white" | "black"
def status(self):
# -> "in progress" | "white wins" | "black wins"Rules. A move is "invalid" if the game is over, the coordinates are out of
bounds, the from-square is empty, it is not that colour's turn, the destination
holds a friendly piece, or the piece cannot move that way. Pawn: one step forward
if empty; two steps from its starting row if both squares are empty; one diagonal
step forward only onto an enemy piece. Knight: an L (2+1), jumping over pieces.
Bishop: diagonal, path clear. Rook: straight, path clear. Queen: bishop or rook.
King: one square in any direction. Capturing the king wins immediately.
Coordinates are authoritative: (6, 4) → (5, 4) is a one-square pawn move. This exercise deliberately is not full legal chess.
Tests use explicit inputs and injected clocks/callbacks. No network, real threads or application-server execution is involved.
Class design (SOLID)
Piece (ABC interface; color + kind; SRP) with Pawn/Knight/Bishop/Rook/Queen/King subclasses — each implements legal_move (LSP: any piece can be asked; OCP: a new piece is a new subclass). Board (the grid + path clearance; SRP). ChessGame (turn + status; SRP) delegates movement to the piece. Pattern: Polymorphism (piece behaviour by subclass).
Public demo
# Public demo: run after implementing the class API above.
g = ChessGame()
print(g.board()[0])
print(g.board()[7])
print(g.board()[6])
print(g.board()[1])
print(g.current_player())Reference solution and hidden tests
Locked. Open the interactive editor and attempt the problem; the reference, hidden test descriptions, pattern explanation and complexity are available only after an explicit reveal.
Concurrency
One game, one move at a time — no shared state beyond the game object. In a server version, one game per match with a lock per game around move.
Trade-offs & extensions
Win-by-king-capture is a deliberate simplification (no check/checkmate). Extending: a MoveValidator for check/checkmate and a GameHistory for undo.
Starter code
from abc import ABC, abstractmethod
class Piece(ABC):
def __init__(self, color, kind):
"""board: optional 8x8 list of lists; default is the standard starting position"""
pass
def symbol(self):
"""Implement the return value and rules in the API contract."""
pass
@abstractmethod
def legal_move(self, board, fr, fc, tr, tc):
"""Implement the return value and rules in the API contract."""
pass
class Pawn(Piece):
def __init__(self, color):
"""board: optional 8x8 list of lists; default is the standard starting position"""
pass
def legal_move(self, board, fr, fc, tr, tc):
"""Implement the return value and rules in the API contract."""
pass
class Knight(Piece):
def __init__(self, color):
"""board: optional 8x8 list of lists; default is the standard starting position"""
pass
def legal_move(self, board, fr, fc, tr, tc):
"""Implement the return value and rules in the API contract."""
pass
class Bishop(Piece):
def __init__(self, color):
"""board: optional 8x8 list of lists; default is the standard starting position"""
pass
def legal_move(self, board, fr, fc, tr, tc):
"""Implement the return value and rules in the API contract."""
pass
class Rook(Piece):
def __init__(self, color):
"""board: optional 8x8 list of lists; default is the standard starting position"""
pass
def legal_move(self, board, fr, fc, tr, tc):
"""Implement the return value and rules in the API contract."""
pass
class Queen(Piece):
def __init__(self, color):
"""board: optional 8x8 list of lists; default is the standard starting position"""
pass
def legal_move(self, board, fr, fc, tr, tc):
"""Implement the return value and rules in the API contract."""
pass
class King(Piece):
def __init__(self, color):
"""board: optional 8x8 list of lists; default is the standard starting position"""
pass
def legal_move(self, board, fr, fc, tr, tc):
"""Implement the return value and rules in the API contract."""
pass
class Board:
def __init__(self, grid=None):
"""board: optional 8x8 list of lists; default is the standard starting position"""
pass
@staticmethod
def initial():
"""Implement the return value and rules in the API contract."""
pass
@staticmethod
def in_bounds(r, c):
"""Implement the return value and rules in the API contract."""
pass
def get(self, r, c):
"""Implement the return value and rules in the API contract."""
pass
def set(self, r, c, piece):
"""Implement the return value and rules in the API contract."""
pass
def path_clear(self, fr, fc, tr, tc):
"""Implement the return value and rules in the API contract."""
pass
def to_strings(self):
"""Implement the return value and rules in the API contract."""
pass
class ChessGame:
def __init__(self, board=None):
"""board: optional 8x8 list of lists; default is the standard starting position"""
pass
def board(self):
"""-> 8x8 list of lists (a copy)"""
pass
def current_player(self):
'''-> "white" | "black"'''
pass
def status(self):
'''-> "in progress" | "white wins" | "black wins"'''
pass
def move(self, from_sq, to_sq):
'''from_sq, to_sq: (row, col) tuples -> "ok" | "invalid" | "white wins" | "black wins"'''
pass
Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.
Return to the Python interpreter