As reported: NetApp round 1 (2025): a design-based DSA problem — design a chess game with classes of your own, implement
nextPossibleMoves()andmakeNextMove(), and save all moves in the board class so that the possible moves never clash with the moves already played.
Implement a class ChessGame that starts from the standard chess position with white to move:
possibleMoves(square)— the squares the piece onsquarecan move to, sorted. Only pieces of the side to move have moves; an empty square or an opponent's piece gives an empty list.makeMove(from, to)— iftois one ofpossibleMoves(from), play the move (capturing whatever stands onto), record it, pass the turn to the other side and returntrue; otherwise change nothing and returnfalse.pieceAt(square)— the piece on a square:K Q R B N Pfor white,k q r b n pfor black,.for empty.history()— all moves played so far, in order, each written asfrom + to(e.g."e2e4").
Squares use algebraic names: files a–h from white's left, ranks 1–8 from white's side.
Movement rules
- King: one square in any direction. Knight: an L-shaped jump (2 + 1), over other pieces. Rook: any distance along a rank or file. Bishop: any distance diagonally. Queen: rook or bishop moves.
- Sliding pieces stop at the first occupied square; they may capture it if it holds an opponent's piece.
- Pawn: one square forward onto an empty square; two squares forward from its starting rank if both squares are empty; captures one square diagonally forward. A pawn reaching the last rank becomes a queen.
- A piece never moves onto a square occupied by its own side.
Clarifications
- Check, checkmate, castling and en passant are not modelled: every move that follows the movement rules is allowed (pseudo-legal moves).
- Moves alternate strictly between white and black.
Example
ChessGame()
possibleMoves("e2") -> ["e3","e4"]
possibleMoves("g1") -> ["f3","h3"]
makeMove("e2","e4") -> true
makeMove("e2","e4") -> false e2 is empty now
possibleMoves("d8") -> [] black queen is blocked
makeMove("e7","e5") -> true
history() -> ["e2e4","e7e5"]
Constraints: at most 500 calls; every square argument is a valid square name.
Code runner format: two lines in LeetCode's design format: the list of operations (["ChessGame","possibleMoves","makeMove",…]) and the list of argument lists. The harness prints the list of results, with null for the constructor. Go uses NewChessGame, PossibleMoves, MakeMove, PieceAt, History.