-3

Cricket scoring is complex and I want to build an app in part to practice good design principles/patterns and develop a clean solution.

A few high level classes I have in mind are:

Match | Innings | Player | BallScore | TotalScore

There are states, some noticeable are following:

  • CurrentInnings
  • CurrentBatter
  • NonStrikerBatter

Strategy

I am inclined for Match to contain two Innings and each innings are 11 Players but then how to keep track of current innings and current batter/non-striker in clean way?

Or should the app simply work with current batter/non-striker-batter and bowler add them to match object once a batter is out? I would like the app to show current view of innings as show in picture

enter image description here

Any ideas welcome.

The main thing I am looking for is clean solution for keeping track of current innings and current batter (switching batter with scoring) and how to progress the match to 2nd innings.

6
  • whats the app got to do exactly and whats lacking with your current solution? whats wrong with just having a list of events from which you compute the score/display?
    – Ewan
    Commented Feb 12 at 19:05
  • @Ewan The app will just record progress of the match (operated by a user). I know all the logic that will go into scoring but trying to organize the objects in best way and how to main state of the match that should support do/undo functionality as well (Command Pattern).
    – zadane
    Commented Feb 12 at 19:17
  • This seems to be a data modelling problem -- the main issue I see is that the focus on objects and architecture look largely irrelevant and unhelpful given the problem you're trying to solve is focused on how to organise and structure the data. Almost everything mentioned here sounds like it relates to either an entity or attribute of an entity somehow. Commented Feb 12 at 21:36
  • What have you tried to keep track of the current inning/batter/non-striker batter/bowler? What cleanliness problems do you have with your attempts? Commented Feb 13 at 8:03
  • "Help analyze the cricket scoring system for me" is not a question, it's a request for work. Voting to close as this needs refinement towards a concrete software engineering question, not a request for people to go and look up the rules/scoring of cricket.
    – Flater
    Commented Feb 14 at 0:28

1 Answer 1

3

With rules based games I always find it best to AVOID any objects relating to human concepts in the game, ie match, player, wicket etc. At least as a first pass. Rules for games are not written in an Object Orientated fashion so its hard or impossible to reverse engineer them to fit that pattern and end up with those kind of objects.

The best approach is to record the events that happen in the game with a single event type

eg.

id, time, type, data
1, 10:00, playerWalkOnPitch, "Ian Botham"
1, 10:01, playerWalkOnPitch, "Bob McCricketFace"
2, 10:30, umpireStartsGame,
3, 10:31, bowlerBowls, "Bob mcCricketFace"
4, 10:31, batterHits, "Ian Botham", 123m
5, 10:31, batterRuns, "Ian Botham", south

etc etc

Then you can make any calculation on the game at any point via objects

ScoreCalculator()
{
    string ScoreIs(List<Event> eventsSoFar) {...
}

TimeLeftInCalc()
{
    int SecondsInMatch(List<Event> eventsSoFar) {...
    int SecondsInOver(List<Event> eventsSoFar) {...
} 

etc etc

It also allows for things you didnt think of

int SecondsReviewingUmpireDecision(List<Event> eventsSoFar) {...

and new rules

int SecondsInMatch50-50(List<Event> eventsSoFar) {...

etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.