CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 5 of 6 · 3 min read

Edge cases and changes

Variants of the rules.

Step 5: edge cases, tests and change requests

Edge cases:

Case Behaviour
Roll takes the player past the end Player stays, turn passes
Roll lands exactly on the end Player wins
Landing on a snake head Moves down, and stays there until the next turn
Jump map with a chain or a loop The board cannot be built
One player The game constructor refuses
step after the game ended IllegalStateException

Tests to write first: a ladder, a snake, an overshoot, an exact win, two refused boards, and a game with a scripted die from start to finish. The win test should check that step throws afterwards.

Change requests, each with the part that changes:

  • "A six gives another turn." The simplest answer is a rule object, TurnRule, asked after each roll whether to pass the turn. Game calls it, and the default rule always passes. A new class and a one-line change to Game.
  • "Use two dice." A Dice that rolls twice and adds. Nothing else changes.
  • "Loaded dice for tests or for a harder level." A new Dice.
  • "Win with an overshoot allowed (bounce back)." A change to the overshoot branch, ideally behind a FinishRule with two classes: exact and bounce.
  • "Show a log of each move." Game already builds one. Better, publish a move event to listeners (observer, Level 4, module 2), so a console and a web page can both listen.
  • "Many boards, each with a theme." A BoardFactory that builds a board from a name or a file (Level 3, module 1). The validation runs in every case, since it lives in the constructor.
  • "Let a player choose between two dice." A MoveSource as in tic-tac-toe.

These answers show the shape of a good design: each new rule is a new small class, and Game stays small.

Common mistake

Making Snake and Ladder two classes that differ only in a comment, or calling the random generator inside Game. The first doubles the code for no gain. The second makes the game impossible to test.

Interview angle

Answer in your head first, then open it.

How do you model snakes and ladders?

As one concept, a jump from one square to another. Direction tells which is which.

How do you test a random game?

Put the dice behind an interface and use a scripted die in tests, or a seeded random one.

What stops an invalid board?

The constructor validates and the board is immutable, so a bad board never exists.

How do you add a rule such as an extra turn on a six?

A small rule object that the game consults, so the game's loop does not grow.

Check yourself: 1. Which change request needs a rule object, and why? 2. Which classes stay the same when two dice are used? 3. Where does the board validation run, and why there?

Words you will meet

Word Meaning
Jump A pair of squares: landing on one moves you to the other
Exact finish The rule that the last roll must land exactly on the final square
Scripted dice A test die that gives a fixed list of rolls
Seed A starting number that makes a random generator repeat itself
Rule object A small class that holds one variable rule of the game