Part 6 of 6 · 3 min read
Exercises
Easy
Easy 1: Write the rules (20 minutes)
Write six numbered requirements for the game and three questions you would ask about house rules.
Done when: six rules and three questions exist, and one rule is marked out of scope.
Hint
Ask about the finish, the extra turns and the number of dice.
Easy 2: Run the four turns (20 minutes)
Type the code and reproduce the four-turn table with the scripted die.
Done when: A is on 5 and B is on 9.
Hint
new ScriptedDice(4, 3, 2, 6).
Easy 3: Spot the bad boards (20 minutes)
Decide which of these are refused: 4 to 25 and 25 to 3, 100 to 3, 12 to 12, 1 to 30, 60 to 120 on a 100 board.
Done when: five answers each give the reason.
Hint
A jump may not start at 1 or on the last square, and may not end on another start.
Medium
Medium 1: Extra turn on a six (45 minutes)
Add a TurnRule interface with passesTurn(roll). Make Game use it, with one class that keeps the turn on a six.
Done when: a scripted run with a six shows the same player rolling twice, and the default still passes the turn.
Hint
The game asks the rule after each roll.
Medium 2: Bounce back (45 minutes)
Add a finish rule where an overshoot bounces back by the extra amount. Make both rules selectable.
Done when: a roll of 5 from square 97 on a 100-square board leaves the player on 97 under exact finish and on 98 under bounce back.
Hint
From 97 with a roll of 5, the target is 102, which is 2 past the end of 100.
Medium 3: Event log (45 minutes)
Replace the log list with listeners. Write a console listener and a counting listener.
Done when: both listeners receive every move and Game does not name either class.
Hint
Keep a list of listeners and call each after the move.
Hard
Hard 1: Chains allowed (90 minutes)
Allow a jump to end on another jump's start. Resolve chains, and detect loops when the board is built.
Done when: chains work, a loop is refused at construction, and a test covers both.
Hint
Follow the chain from each start, counting steps, and stop on a repeat.
Hard 2: Simulation (90 minutes)
Play 10,000 seeded games on a board with no jumps and on a board with six snakes and six ladders. Report the average length of each.
Done when: two averages are printed and a sentence explains why they differ.
Hint
Use one seed per game, so the run is repeatable.
Hard 3: Board from a file (90 minutes)
Read a board from a text file of lines like "4 25". Report errors with the line number. Keep the board class unchanged.
Done when: a good file loads, three bad files each give a clear message with a line number.
Hint
Read lines, split on spaces, and build a map before calling the constructor.