Bowling Game
Level: AdvancedConcepts: Edge Cases State
Create a program to score a game of ten-pin bowling.
Requirements
- Calculate the score for a game of ten-pin bowling
- 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)
- 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
Scenario | Rolls | Score | Notes |
---|---|---|---|
All Gutter Balls | 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 | 0 | No pins knocked down |
All Ones | 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1 | 20 | One pin per roll |
One Spare | 5,5, 3,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 | 16 | Spare + bonus |
One Strike | 10, 3,4, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 | 24 | Strike + bonus |
Perfect Game | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,10,10 | 300 | All strikes |
All Spares | 5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5,5,5 | 150 | All 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