Part 6 of 6 · 2 min read
Exercises
Easy
Easy 1: Write the rules (20 minutes)
Write six numbered requirements for tic-tac-toe and two questions you would ask.
Done when: six rules exist and the questions cover size and ending.
Hint
Think about what a player can do wrong.
Easy 2: Run the game (20 minutes)
Type the three classes and play the row win from the table.
Done when: it prints the status WON and the winner X.
Hint
game.play(0, 0) and so on, inside a try.
Easy 3: Trace the counters (20 minutes)
On paper, trace the counters for the moves X(0,0), O(1,0), X(1,1), O(0,1), X(2,2).
Done when: you show the diagonal count reaching 3 at the last move.
Hint
Row 0, column 0 and the main diagonal all change on (0,0).
Medium
Medium 1: Test the bad moves (45 minutes)
Write tests for taken cell, off the board and a move after the end. Each test must also check that the turn did not change.
Done when: three tests pass and each asserts the same turn afterward.
Hint
Read turn() before and after.
Medium 2: A text view (45 minutes)
Write a BoardView that prints the grid with X, O and dots. Do not add print code to Board.
Done when: a mid-game board prints three rows of three characters.
Hint
board.at(r, c) returns null for an empty cell.
Medium 3: K in a row (45 minutes)
Change place so that a win needs k marks in a row on a board of n. Scan outward from the new cell in four directions.
Done when: a 5 by 5 board with k = 4 reports a win on four in a row and not on three.
Hint
Count marks in both directions along each axis, then add one.
Hard
Hard 1: Undo and redo (90 minutes)
Add undo() and redo() using a stack of moves. Fix the counters on undo.
Done when: after four moves and two undos, the board and turn match the state after two moves.
Hint
Each move remembers its cell and mark.
Hard 2: A smart bot (90 minutes)
Write a MoveSource that wins when it can, blocks the opponent when it must, and otherwise takes the centre or a corner.
Done when: the bot never loses against FirstFree in 100 games, and a test shows a block.
Hint
Check each empty cell for a winning move for you, then for the opponent.
Hard 3: Three players (90 minutes)
Generalise to three players on a 5 by 5 board with 4 in a row. Replace the Mark enum with a player id and rotate turns.
Done when: a three-player game runs to a win, and a note lists which classes changed.
Hint
Counters become a map from player to count.