CSE Roadmap

Common tier · about 5 hours · 6 parts

Part 5 of 6 · 4 min read

Edge cases and changes

Missing rules and how they would fit.

Step 5: edge cases, tests and change requests

Edge cases and the answer in the code:

Case Behaviour
Move from an empty square Refused with the square name
Move the opponent's piece "not your piece"
Move to the same square Refused (pseudo-legality rejects it)
Capture own piece Refused
Pawn double step through a piece Refused (the middle square must be empty)
Pawn forward onto an enemy piece Refused (pawns capture only diagonally)
Moving a pinned piece "your king would be in check"
Pawn reaching the last row Becomes a queen
Any move after mate or stalemate "game over"
Square text such as "z9" or "e" Rejected by Pos

Rules left out, and how each fits in:

  • Castling. The king and rook must not have moved, the squares between them must be empty, and the king may not pass through check. Add a hasMoved flag to king and rook, and a special move handled in Game, since it moves two pieces.
  • En passant. The game remembers the last move, and a pawn may capture a pawn that just made a two-step move beside it. Needs a record of the last move.
  • Promotion to other pieces. The move gains a fourth part, the chosen piece, instead of always taking a queen.
  • Draws by repetition and the fifty-move rule. The game keeps a history of positions and a counter.
  • Insufficient material. A check at the end of each move.

Tests to write first: each piece's movement on an empty board; blocked paths; each row of the run table; the scholar's mate; a pinned piece; and the stalemate game. Test Pos.of with bad input too.

Change requests and the part that changes:

  • "Undo a move." Record each move as a command (Level 4, module 4) holding the squares, the piece moved and any captured piece, and reverse it.
  • "Show the board." A BoardView that reads squares and prints a symbol for each piece, with no print code in Board (the symbol method on pieces helps).
  • "Play against a computer." A MoveSource as in tic-tac-toe (module 2). The simplest bot picks the first legal move. A better one scores positions.
  • "Save and load a game." Store the list of moves, and replay them on a new game. This is why a move history is worth keeping.
  • "Chess variants with different pieces." New subclasses and a different setup. The game's rules do not change.
  • "Many games at once." Each Game is independent. A GameManager holds them by id, and the games are not shared between threads without a lock.

Two honest limits. Game reaches into Board fields directly in this version, which a cleaner design would hide behind methods. And pieces are rebuilt on every game, which is fine, since they hold only a colour. Piece objects without state could even be shared (Level 3, module 5 on copies, and the flyweight note in Level 4, module 10).

Common mistake

Putting the movement of all pieces in one big method with a switch on the piece type. Each new piece edits the method, and the method grows. One class per piece keeps each rule in one place.

Interview angle

Answer in your head first, then open it.

How do you model the pieces?

An abstract Piece with a method for reachability and one subclass for each kind, so the game never switches on type.

How do you check whether a move is legal?

Reachability for the piece, the target not being the mover's own piece, and the king not being in check after the move. Test the last by playing the move and undoing it.

How do you detect checkmate?

The player to move has no legal move and is in check. With no legal move and no check, it is stalemate.

What did you leave out?

Castling, en passant, repetition and the fifty-move rule, with a sentence on how each would be added.

Check yourself: 1. Which rule needs a history of the last move? 2. Which change request reuses the idea of module 2? 3. Why is a pinned piece refused?

Words you will meet

Word Meaning
Pseudo-legal move A move the piece can make, before checking king safety
Legal move A pseudo-legal move that leaves the mover's king safe
Check The king is attacked by an enemy piece
Checkmate In check, with no legal move
Pin A piece that cannot move because it shields its king