| 1 | 1 | class BowlingGame: |
| 2 | 2 | def __init__(self): |
| 3 | | pass |
| 3 | self.rolls = [] |
| 4 | self.current_frame = 1 |
| 5 | self.frame_rolls = 0 # Number of rolls in the current frame |
| 6 | self.is_finished = False |
| 4 | 7 | |
| 5 | 8 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 9 | # Edge Case: Game is already finished |
| 10 | if self.is_finished: |
| 11 | raise ValueError("Cannot roll after game is over") |
| 12 | |
| 13 | # Edge Case: Invalid pin count (negative or more than 10) |
| 14 | if pins < 0 or pins > 10: |
| 15 | raise ValueError("Pins must be between 0 and 10") |
| 16 | |
| 17 | # Edge Case: Too many pins in a frame (except for special 10th frame cases) |
| 18 | if self.frame_rolls == 1 and self.current_frame < 10: |
| 19 | # For frames 1-9, check if the sum exceeds 10 |
| 20 | if self.rolls[-1] + pins > 10: |
| 21 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 22 | |
| 23 | self.rolls.append(pins) |
| 24 | |
| 25 | # Handle frame progression |
| 26 | if self.current_frame < 10: |
| 27 | # For frames 1-9 |
| 28 | if self.frame_rolls == 0: |
| 29 | if pins == 10: # Strike |
| 30 | self.current_frame += 1 |
| 31 | self.frame_rolls = 0 |
| 32 | else: |
| 33 | self.frame_rolls = 1 |
| 34 | else: # Second roll in frame |
| 35 | self.current_frame += 1 |
| 36 | self.frame_rolls = 0 |
| 37 | else: |
| 38 | # For frame 10 |
| 39 | if self.frame_rolls == 0: |
| 40 | if pins == 10: # First roll is a strike |
| 41 | self.frame_rolls = 1 |
| 42 | else: |
| 43 | self.frame_rolls = 1 |
| 44 | elif self.frame_rolls == 1: |
| 45 | # Second roll in frame 10 |
| 46 | # If first was strike or second completes spare |
| 47 | if self.rolls[-2] == 10 or (self.rolls[-2] + pins == 10): |
| 48 | self.frame_rolls = 2 # Need one more roll |
| 49 | else: |
| 50 | # Frame 10 is complete with just two rolls |
| 51 | self.is_finished = True |
| 52 | else: # Third roll in frame 10 |
| 53 | self.is_finished = True |
| 54 | |
| 8 | 55 | def score(self): |
| 9 | | pass |
| 56 | # Edge Case: Game is not yet finished |
| 57 | if not self.is_finished: |
| 58 | raise ValueError("Game is not yet complete") |
| 59 | |
| 60 | total_score = 0 |
| 61 | roll_index = 0 |
| 62 | |
| 63 | # Score first 9 frames |
| 64 | for frame in range(9): |
| 65 | # Edge Case: Not enough rolls to calculate score |
| 66 | if roll_index >= len(self.rolls): |
| 67 | raise ValueError("Not enough rolls to calculate score") |
| 68 | |
| 69 | if self.rolls[roll_index] == 10: # Strike |
| 70 | # Edge Case: Not enough subsequent rolls to calculate strike bonus |
| 71 | if roll_index + 2 >= len(self.rolls): |
| 72 | raise ValueError("Not enough rolls to calculate score") |
| 73 | total_score += 10 + self.rolls[roll_index + 1] + self.rolls[roll_index + 2] |
| 74 | roll_index += 1 |
| 75 | else: |
| 76 | frame_score = self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 77 | if frame_score == 10: # Spare |
| 78 | # Edge Case: Not enough subsequent rolls to calculate spare bonus |
| 79 | if roll_index + 2 >= len(self.rolls): |
| 80 | raise ValueError("Not enough rolls to calculate score") |
| 81 | total_score += 10 + self.rolls[roll_index + 2] |
| 82 | else: # Open frame |
| 83 | total_score += frame_score |
| 84 | roll_index += 2 |
| 85 | |
| 86 | # Score 10th frame |
| 87 | # Edge Case: Not enough rolls for 10th frame |
| 88 | if roll_index >= len(self.rolls): |
| 89 | raise ValueError("Not enough rolls to calculate score") |
| 90 | |
| 91 | # Add the remaining rolls (could be 2 or 3 depending on strikes/spares) |
| 92 | while roll_index < len(self.rolls): |
| 93 | total_score += self.rolls[roll_index] |
| 94 | roll_index += 1 |
| 95 | |
| 96 | return total_score |
| 97 | |
| 98 | # Handled Edge Cases: Game already finished, invalid pin count, too many pins in a frame, game not yet finished when scoring, not enough rolls to calculate score, not enough subsequent rolls to calculate strike/spare bonus |