Bowling Game

Level: Advanced 60–90 min

Concepts: Edge CasesState


Create a program to score a game of ten-pin bowling.

Requirements

  1. Calculate the score for a game of ten-pin bowling
  2. Handle all valid bowling scenarios:
    • Regular frames (sum of pins knocked down)
    • Spares (10 pins in two rolls, bonus is next roll)
    • Strikes (10 pins in one roll, bonus is next two rolls)
    • 10th frame special cases (up to three rolls)
  3. Validate input:
    • Number of pins must be between 0 and 10
    • Sum of pins in a frame (except last) must not exceed 10
    • Proper number of rolls based on game progress

Test Cases

ScenarioRollsScoreNotes
All Gutter Balls0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,00No pins knocked down
All Ones1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,120One pin per roll
One Spare5,5, 3,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,016Spare + bonus
One Strike10, 3,4, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,024Strike + bonus
Perfect Game10, 10, 10, 10, 10, 10, 10, 10, 10, 10,10,10300All strikes
All Spares5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5,5,5150All spares

Edge Cases to Consider

  • Invalid number of pins (>10 or <0)
  • Invalid frame totals (>10 in a frame)
  • Wrong number of rolls for the game state
  • Invalid bonus rolls in 10th frame
  • Empty or null input

Tips

  • Start with the simplest case (all zeros)
  • Add support for regular frames first
  • Then add spares, then strikes
  • Handle the 10th frame as a special case
  • Consider using a Frame class to encapsulate frame logic