Cricinfo
A live scorecard updates one ball at a time, and "one ball" already has enough state to trip you up: it can be a run, a wicket, a wide that doesn't count toward the over, or all three format's worth of rules about when an innings simply ends. The design keeps the ball-by-ball plumbing identical across formats and isolates only the part that actually differs.
Requirements
Functional
- A match is created between two teams in a format: T20, ODI, or Test.
- Deliveries are recorded one at a time; each updates the current over, the batting side's score, and the striker's/bowler's individual stats.
- An over completes after six legal deliveries (wides and no-balls don't count toward the six); an innings ends per the format's own rule (overs limit, all out, or a declaration for Test).
- A scorecard reports the live score, overs bowled, and current batters/bowler at any point without replaying the innings.
Non-functional
- Recording a ball must update the scorecard incrementally - it must not recompute totals by replaying every prior delivery.
- The rule for when an innings ends (fixed overs vs. all-out-or-declare) differs by format
and must be swappable without editing
Match,Innings, orOver.
Design
Match records one Ball at a time and never asks "what format are we in" to decide
anything - every format-specific question (how many overs, when does this innings end) is
answered by a MatchFormat strategy handed in at match creation. Over and Innings only
know about balls and totals; they'd behave identically whether the strategy said "20 overs"
or "unlimited, until ten wickets or a declaration."
- 1The scorer only ever talks to Match - never to an Over or Innings directly.
- 2Match forwards to whichever innings is currently live.
- 3The over updates its own ball count and the innings’ running score.
- 4After every ball, the innings asks the format strategy - not itself - whether it should end.
- 5If the format says the innings is done, the match moves on - the rule that triggered it never leaks past this line.
Scorecard is a read-only view built once and mutated ball-by-ball alongside the innings -
never rebuilt - which is what keeps "get me the live score" a field read instead of a
replay.
Class diagram
Code
Design decisions
- Format rules live entirely in
MatchFormat, not inif format == TESTchecks.T20Format/OdiFormatcap overs at a fixed number;TestFormatallows a declaration and no overs cap.Innings.isCompleteasks the strategy one question - "should this innings end now" - instead of encoding three formats' worth of logic inline. Overcounts only legal deliveries toward its limit of six. A wide or no-ball updates the score and is appended to the ball list, but doesn't advance the over's ball counter - modeling that distinction insideOver.recordBallis what makes "six balls" mean the cricket definition instead of "six list entries."Scorecardis updated incrementally byInnings, never recomputed. Every ball updates running totals directly on the scorecard object; there is norebuildFromBalls()method, because a live scorecard that recomputes from history on every ball is the exact performance problem the non-functional requirement rules out.Playerstats (runs, balls faced, wickets) live on the player, keyed per match. A player who's out doesn't stop existing -Inningsjust stops passing them deliveries - so there's no special "retired" subclass, just a state the innings tracks about who's currently at the crease.- What's missing for a real system: partnerships, DLS-method rain calculations, and
ball-by-ball commentary text are all real Cricinfo features that sit on top of the model
above (mostly as read-only views over
Scorecard/Ballhistory) rather than requiring changes to how a ball is recorded.