Implement a two-player chess game with move validation, check detection and undo

Quick Overview

Implement a two-player chess game class that validates moves in coordinate notation, rejects moves that leave the king in check, reports check, checkmate and stalemate, and keeps a move history with undo. It tests object-oriented modeling of pieces, legality checks and reversible game state.

Implement a two-player chess game with move validation, check detection and undo

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a multiplayer chess game for two players, `WHITE` and `BLACK`, who take alternating turns on a standard 8x8 board. The system must model the movement rules of each piece (pawn, rook, knight, bishop, queen and king), track the game state (ongoing, check, checkmate, stalemate), and keep the history of moves played so they can be inspected or undone. Moves are written in coordinate notation such as `"e2e4"`, meaning move the piece on square `e2` to square `e4`. Illegal moves must be rejected: moving out of turn, a move the piece cannot make, or a move that leaves the player's own king in check. Implement a `ChessGame` class with these methods: - `move(notation: str) -> bool` tries to play the move given in coordinate notation. It returns `True` if the move was legal and applied, `False` otherwise. - `get_state() -> str` returns the current game state: `"ONGOING"`, `"CHECK"`, `"CHECKMATE"` or `"STALEMATE"`. - `get_turn() -> str` returns whose turn it is: `"WHITE"` or `"BLACK"`. - `get_piece(square: str) -> str` returns a 2-character code for the piece on the square (`"WP"` for a white pawn, `"BK"` for the black king), or `".."` if the square is empty. - `history() -> List[str]` returns the moves played so far, in order, in coordinate notation. - `undo() -> bool` undoes the most recent move. It returns `True` if a move was undone and `False` if there is no move to undo. Example 1: ```python game = ChessGame() # new game, White to move game.get_turn() # "WHITE" game.get_piece("e2") # "WP" (white pawn on e2) game.move("e2e4") # True (double pawn push from the start rank) game.get_turn() # "BLACK" game.move("e7e5") # True game.get_piece("e4") # "WP" game.history() # ["e2e4", "e7e5"] game.undo() # True (takes back e7e5) game.get_turn() # "BLACK" (Black is to move again) ``` Example 2 (fool's mate): ```python game = ChessGame() game.move("f2f3") # True game.move("e7e5") # True game.move("g2g4") # True game.move("d8h4") # True (the black queen gives checkmate) game.move("a2a3") # False (White is checkmated, so no move is legal) game.move("e2e4") # False game.move("b7b6") # False (it is not Black's turn) game.get_state() # "CHECKMATE" ``` ```hint King safety Think about how you would know that a move exposes your own king without writing a special rule for pins, discovered checks and the king stepping into an attacked square. ``` ```hint What undo needs List everything a move changes on the board. A history entry has to hold enough to reverse all of it, including a capture. ``` ### Constraints & Assumptions - The board is an 8 x 8 grid. - Squares use files `a` to `h` and ranks `1` to `8` (for example `"e2"`). - A move is a 4-character string `"<from><to>"`, such as `"e2e4"`. - Players alternate turns, starting with `WHITE`. - At most 10,000 moves are played per game. - Castling, en passant and promotion are out of scope. ### Clarifying Questions to Ask - Does `get_state` describe the player whose turn it is, so that `"CHECK"` after White's move means Black is in check? - With promotion out of scope, does a pawn that reaches the last rank stay there as a pawn with no moves? - Are draws other than stalemate (threefold repetition, the fifty-move rule, insufficient material) out of scope? - What should `get_piece` do with an invalid square such as `"z9"`? ### What a Strong Answer Covers - A class breakdown that keeps each piece's movement rule with the piece and keeps the turn, history and game status in the game object. - Move validation in order: parsing, piece ownership and turn, the piece's movement rule including blocked paths, and rejection of moves that leave the mover's own king in check. - How check, checkmate and stalemate are told apart using two facts: whether the king is attacked and whether any legal move exists. - What each history entry stores so that undo restores the board exactly, captured pieces included, along with the turn. - The cost of state detection and where it is computed. ### Follow-up Questions - How would you add castling, en passant and promotion, and what would each history entry need to store for undo? - How would you detect draws by threefold repetition and by the fifty-move rule? - How would you make `get_state` cheap enough to call after every move? - How would the design change if the two players were on different machines and a server had to accept their moves over the network?

Overview: Implement a two-player chess game class that validates moves in coordinate notation, rejects moves that leave the king in check, reports check, checkmate and stalemate, and keeps a move history with undo. It tests object-oriented modeling of pieces, legality checks and reversible game state.

Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a multiplayer chess game for two players, WHITE and BLACK, who take alternating turns on a standard 8x8 board.

The system must model the movement rules of each piece (pawn, rook, knight, bishop, queen and king), track the game state (ongoing, check, checkmate, stalemate), and keep the history of moves played so they can be inspected or undone.

Moves are written in coordinate notation such as "e2e4", meaning move the piece on square e2 to square e4. Illegal moves must be rejected: moving out of turn, a move the piece cannot make, or a move that leaves the player's own king in check.

Implement a ChessGame class with these methods:

  • move(notation: str) -> bool tries to play the move given in coordinate notation. It returns True if the move was legal and applied, False otherwise.
  • get_state() -> str returns the current game state: "ONGOING" , "CHECK" , "CHECKMATE" or "STALEMATE" .
  • get_turn() -> str returns whose turn it is: "WHITE" or "BLACK" .
  • get_piece(square: str) -> str returns a 2-character code for the piece on the square ( "WP" for a white pawn, "BK" for the black king), or ".." if the square is empty.
  • history() -> List[str] returns the moves played so far, in order, in coordinate notation.
  • undo() -> bool undoes the most recent move. It returns True if a move was undone and False if there is no move to undo.

Example 1:

game = ChessGame()       # new game, White to move
game.get_turn()          # "WHITE"
game.get_piece("e2")     # "WP"  (white pawn on e2)
game.move("e2e4")        # True  (double pawn push from the start rank)
game.get_turn()          # "BLACK"
game.move("e7e5")        # True
game.get_piece("e4")     # "WP"
game.history()           # ["e2e4", "e7e5"]
game.undo()              # True  (takes back e7e5)
game.get_turn()          # "BLACK" (Black is to move again)

Example 2 (fool's mate):

game = ChessGame()
game.move("f2f3")        # True
game.move("e7e5")        # True
game.move("g2g4")        # True
game.move("d8h4")        # True  (the black queen gives checkmate)
game.move("a2a3")        # False (White is checkmated, so no move is legal)
game.move("e2e4")        # False
game.move("b7b6")        # False (it is not Black's turn)
game.get_state()         # "CHECKMATE"

Constraints & Assumptions

  • The board is an 8 x 8 grid.
  • Squares use files a to h and ranks 1 to 8 (for example "e2" ).
  • A move is a 4-character string "<from><to>" , such as "e2e4" .
  • Players alternate turns, starting with WHITE .
  • At most 10,000 moves are played per game.
  • Castling, en passant and promotion are out of scope.

Clarifying Questions to Ask Guidance

  • Does get_state describe the player whose turn it is, so that "CHECK" after White's move means Black is in check?
  • With promotion out of scope, does a pawn that reaches the last rank stay there as a pawn with no moves?
  • Are draws other than stalemate (threefold repetition, the fifty-move rule, insufficient material) out of scope?
  • What should get_piece do with an invalid square such as "z9" ?

What a Strong Answer Covers Guidance

  • A class breakdown that keeps each piece's movement rule with the piece and keeps the turn, history and game status in the game object.
  • Move validation in order: parsing, piece ownership and turn, the piece's movement rule including blocked paths, and rejection of moves that leave the mover's own king in check.
  • How check, checkmate and stalemate are told apart using two facts: whether the king is attacked and whether any legal move exists.
  • What each history entry stores so that undo restores the board exactly, captured pieces included, along with the turn.
  • The cost of state detection and where it is computed.

Follow-up Questions Guidance

  • How would you add castling, en passant and promotion, and what would each history entry need to store for undo?
  • How would you detect draws by threefold repetition and by the fifty-move rule?
  • How would you make get_state cheap enough to call after every move?
  • How would the design change if the two players were on different machines and a server had to accept their moves over the network?