Problem Statement
You are writing software that calculates a bowler's score based on the number of pins knocked down over a sequence of attempts. Because you are only building a prototype, you may assume the bowling game only has one player and that the game is completed.
Scoring in bowling works as follows. A game consists of 10 frames. For each frame, a bowler, who we'll assume is male, has up to two attempts to try and knock down a total of 10 pins. If he knocks down 10 pins on the first attempt of a frame, he gets a STRIKE and that frame is over. If he knocks down 10 pins after two attempts, he gets a SPARE. The pins do not reset between attempts in the same frame, but they do reset back to 10 at the end of each frame.
- If a bowler does not get a STRIKE or SPARE, the score for that frame is equal to the total number of pins knocked down.
- If a bowler gets a SPARE, the score for that frame is 10 plus the number of pins knocked down on the next attempt
- If a bowler gets a STRIKE, the score for that frame is 10 plus the total number of pins knocked down on the next two attempts
If a bowler gets a SPARE on the last frame, then he gets to take another attempt on a fresh set of 10 pins solely for the purpose of calculating the value of the SPARE (see examples). If a bowler gets a STRIKE on the last frame, then he gets to take two more attempts on a fresh set of 10 pins solely for the purpose of calculating the value of the STRIKE. If on the first of those two "bonus" attempts he knocks down all 10, then the second of the two bonus attempts will be on a fresh set of 10 pins (see examples).
You are given a
Definition
- Class:
- BowlSim
- Method:
- calcScore
- Parameters:
- int[]
- Returns:
- int
- Method signature:
- int calcScore(int[] pinsDown)
- (be sure your method is public)
Notes
- The highest possible score is a 300 from getting 10 STRIKES and 10 pins knocked down on each bonus atempt on the last frame.
- A frame is over after getting a STRIKE (unless it is the 10th frame, in which case you try for bonuses).
Constraints
- Each element of pinsDown is between 0 and 10, inclusive.
- pinsDown contains between 12 and 20 elements.
- pinsDown will represent a valid, complete game. That is:Each element will be between 0 and 10, inclusiveEach frame will have between 0 and 10 pins (inclusive) knocked down, total.The number of elements in pinsDown will be consistent with the rules of the game.
Examples
{10,5,4,3,7,10,10,10,5,4,3,7,10,10,4,3}
Returns: 192
Here are the scores for each frame: Frame 1: 10 points for knocking down 10 pins + 9 bonus points for the STRIKE Frame 2: 9 pins for a total of 9 points Frame 3: 10 pins + 10 bonus points for the SPARE Frame 4: 10 pins + 20 bonus points for the STRIKE Frame 5: 10 pins + 15 bonus points for the STRIKE Frame 6: 10 pins + 9 bonus points for the STRIKE Frame 7: 9 pins for a total of 9 points Frame 8: 10 points + 10 bonus points for a SPARE Frame 9: 10 pins + 14 bonus points for a STRIKE Frame 10: 10 pins plus 7 bonus points for a STRIKE The last two elements of pinsDown were bonus attempts from the STRIKE on frame 10
{10,5,4,3,7,10,10,10,5,4,3,7,10,10,10,10}
Returns: 211
Same as previous example except the last two bonus frames each had 10 pins knocked down. Note that you do not tally up bonus points for the last two attempts, even though they may appear to be STRIKES. The last two attempts are only for calculating the bonus of the STRIKE on frame 10.
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
Returns: 0
A terrible bowler has just had a huge streak of gutter balls (the ball rolled into the gutter and knocked down zero pins).
{10,10,10,10,10,10,10,10,10,10,10,10}
Returns: 300
The perfect bowling score
{0,5,6,3,4,5,6,4,7,3,8,2,9,1,10,0,5,0,9}
Returns: 126
{5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,4}
Returns: 144
{0,10,5,4,0,10,5,4,0,10,5,4,0,10,5,4,0,10,5,4}
Returns: 120
Note that if the first attempt of a frame is a 0, and the second is a 10, this counts as a SPARE, not a STRIKE.
{1,0,0,0,0,0,0,0,0,1,5,1,0,1,0,1,3,1,3,3}
Returns: 20
{10,10,10,10,10,10,10,7,3,10,10,9,1}
Returns: 266
{10,10,10,10,10,10,10,4,2,9,1,10,8,2}
Returns: 236
{10,10,10,10,10,10,10,10,10,10,10,7}
Returns: 297
{2,0,1,2,1,2,1,3,4,0,0,0,0,3,6,1,5,2,1,1}
Returns: 35
{10,10,2,8,10,10,10,10,5,5,10,10,10,5}
Returns: 242
{10,10,10,10,10,10,10,10,10,10,6,4}
Returns: 286
{10,10,4,6,2,8,10,10,10,10,10,10,10,10}
Returns: 256
{1,1,4,2,2,3,5,1,0,0,4,0,0,1,2,1,3,3,0,1}
Returns: 34
{6,4,10,2,7,7,3,10,10,6,0,8,2,6,2,9,1,8}
Returns: 158
{10,5,5,10,10,4,5,10,10,10,10,9,1,10}
Returns: 221
{4,0,8,2,6,4,10,10,10,9,1,10,10,10,10,6}
Returns: 225
{10,10,10,10,9,1,9,1,10,5,4,10,8,2,10}
Returns: 216
{10,10,9,1,10,6,0,10,10,4,6,10,10,9,1}
Returns: 204
{10,9,1,5,5,4,6,9,1,7,2,10,1,9,9,1,10,10,10}
Returns: 183
{5,1,2,8,10,1,9,10,10,9,1,3,7,9,1,10,8,1}
Returns: 186
{10,10,9,1,10,10,10,3,7,6,4,10,10,10,10}
Returns: 238
{10,10,2,8,4,6,10,10,10,10,10,6,4,10}
Returns: 232
{1,4,6,4,10,10,10,8,2,8,1,10,10,10,10,10}
Returns: 220
{10,10,8,2,10,10,10,10,10,9,1,2,8,9}
Returns: 238
{10,10,10,10,10,10,10,10,10,10,0,10}
Returns: 280
{ 10, 5, 4, 3, 7, 10, 10, 10, 5, 4, 3, 7, 10, 10, 4, 3 }
Returns: 192
{ 7, 3, 5, 1, 9, 1, 10, 10, 7, 1, 9, 1, 10, 4, 5, 10, 9, 1 }
Returns: 162
{ 0, 10, 4, 5, 0, 10, 4, 5, 0, 10, 4, 5, 0, 10, 4, 5, 0, 10, 4, 5 }
Returns: 115
{ 0, 10, 5, 4, 0, 10, 5, 4, 0, 10, 5, 4, 0, 10, 5, 4, 0, 10, 5, 4 }
Returns: 120
{ 5, 5, 6, 4, 10, 10, 10, 10, 10, 10, 10, 3, 7, 6 }
Returns: 245
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }
Returns: 300
{ 10, 10, 10, 10, 10, 10, 10, 8, 1, 7, 1, 10, 1, 1 }
Returns: 226
{ 10, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
Returns: 30