CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 5 of 6 · 3 min read

Edge cases and changes

Bad moves, last move wins, and extensions.

Step 5: edge cases, tests and change requests

Edge cases and what the code does:

Case Behaviour
The ninth move also completes a line The win check runs before the full-board check, so it is a win, not a draw
A move after the game ended Refused with "game is over"
A move on a taken cell Refused, and the turn does not change
Board size 2 Rejected by the constructor
Negative or too large row or column Refused as off the board
A null mark Cannot occur, since the game picks the mark

The first row of the table is the one that is most often wrong. Writing the draw check first would report a draw for a game that was won on the last move.

Tests to write first: each of the three kinds of win, a draw, each bad move, the last-move win, and a 4 by 4 game. The bad-move tests should also check that nothing changed, such as the same turn after a refusal.

Change requests:

  • "Make it N by N." Already supported. It is worth saying so.
  • "Win needs K in a row, not a full line." The counter trick no longer works, since a line of K can sit anywhere inside a longer line. After each move, scan outward from the new cell in four directions and count marks. Only Board.place changes.
  • "Add undo." Make each move a command (Level 4, module 4) with an undo step that clears the cell and reverses the counters. Game keeps a stack of moves.
  • "A smarter computer player." A new MoveSource. The classic choice is a search that tries each move and the replies to it.
  • "Three or more players." The mark becomes a player id, and the counters are kept per player. The turn rotates through a list.
  • "Show the board on screen." A separate BoardView that reads cells from the board (observer, Level 4, module 2). The model has no print code.

Mention one design decision aloud: the board is not told who is playing. It only knows marks. That keeps it reusable.

Common mistake

Checking a draw before checking a win, or checking all lines on every move without saying why. State the cost, and offer the counter version as the faster option.

Interview angle

Answer in your head first, then open it.

How do you check for a win?

Scan the lines through the new cell, or keep a count per row, column and diagonal for each mark and compare to n. The counters are constant time.

How would you add undo?

Store moves as commands, each able to reverse itself, on a stack.

How would you add a computer player?

A move-source interface with a human version and a computer version, chosen per player.

Why is the board separate from the game?

The board holds cells and lines. The game holds turn and status. Each can change without the other.

Check yourself: 1. Why must the win check come before the draw check? 2. What changes in the code for a K-in-a-row game? 3. Which pattern gives undo?

Words you will meet

Word Meaning
Cell One square of the board
Line A full row, column or diagonal
Draw A full board with no winning line
Move source An object that supplies the next move for a player
Constant time Cost that does not grow with the input size