Part 3 of 6 · 1 min read
Classes
Board, dice, players and game.
| Class | Kind | Job in one sentence |
|---|---|---|
Board |
immutable entity | Holds the size and the jumps, validates them, and resolves a square |
Dice |
interface | Supplies a roll |
RandomDice |
class | A real die with a seed |
ScriptedDice |
class | A test die that returns a fixed list of rolls |
Player |
value | A name |
Game |
entity | Holds positions and turn, applies a roll, and finds the winner |
Rules and their homes:
- "Is this a valid board" needs only the board, so the
Boardconstructor checks it and refuses to build a bad one. A built board is always valid, so no other class checks again. - "What square does landing here lead to" is
Board.resolve, a pure lookup. - "Whose turn, who won" needs the sequence of rolls, so
Gameowns it. - "What is a roll" will change, since the dice may be random, scripted, loaded or double, so it sits behind
Dice.
The board is immutable (Level 5, module 2). Its jumps are copied in with Map.copyOf, so the caller cannot alter them afterwards.
Game has-one Board
Game uses Dice (injected)
Game has-many Player, with a position for each
Board has-many jumps (square to square)
Check yourself:
1. Why does the board refuse a bad jump map?
2. Why is Dice an interface?
3. Why is the board immutable?