| 1 | 1 | class BowlingGame: |
| 2 | 2 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 3 | """ |
| 4 | Initialize a new bowling game. |
| 5 | """ |
| 6 | self.rolls = [] |
| 7 | self.current_frame = 1 |
| 8 | self.frame_rolls = [] |
| 9 | self.frames = [] |
| 10 | |
| 5 | 11 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 12 | """ |
| 13 | Record a roll in the game. |
| 14 | |
| 15 | Args: |
| 16 | pins (int): Number of pins knocked down in this roll |
| 17 | |
| 18 | Raises: |
| 19 | ValueError: If pins is negative or greater than 10 |
| 20 | ValueError: If game is already over |
| 21 | """ |
| 22 | # Edge Case: Invalid pin count (negative or more than 10) |
| 23 | if pins < 0 or pins > 10: |
| 24 | raise ValueError("Pins must be between 0 and 10") |
| 25 | |
| 26 | # Edge Case: Game already completed |
| 27 | if len(self.frames) == 10 and not self._needs_fill_balls(): |
| 28 | raise IndexError("Game is already finished") |
| 29 | |
| 30 | # Edge Case: Tenth frame special handling |
| 31 | if self.current_frame == 10: |
| 32 | return self._handle_tenth_frame(pins) |
| 33 | |
| 34 | self.frame_rolls.append(pins) |
| 35 | |
| 36 | # Edge Case: Strike in regular frame (first roll) |
| 37 | if len(self.frame_rolls) == 1 and pins == 10: |
| 38 | self.frames.append(self.frame_rolls) |
| 39 | self.current_frame += 1 |
| 40 | self.frame_rolls = [] |
| 41 | # Edge Case: Two rolls completed in regular frame |
| 42 | elif len(self.frame_rolls) == 2: |
| 43 | # Edge Case: Invalid frame total (more than 10 pins) |
| 44 | if sum(self.frame_rolls) > 10: |
| 45 | raise ValueError("Invalid roll: pins down exceed 10 in a frame") |
| 46 | self.frames.append(self.frame_rolls) |
| 47 | self.current_frame += 1 |
| 48 | self.frame_rolls = [] |
| 49 | |
| 50 | def _handle_tenth_frame(self, pins): |
| 51 | """ |
| 52 | Handle rolls in the tenth frame with special rules. |
| 53 | |
| 54 | Args: |
| 55 | pins (int): Number of pins knocked down |
| 56 | """ |
| 57 | self.frame_rolls.append(pins) |
| 58 | |
| 59 | # Edge Case: First roll in tenth frame |
| 60 | if len(self.frame_rolls) == 1: |
| 61 | # If first roll is not a strike, continue normally |
| 62 | return |
| 63 | |
| 64 | # Edge Case: Second roll in tenth frame |
| 65 | if len(self.frame_rolls) == 2: |
| 66 | # If first was strike or second completed a spare, need one more roll |
| 67 | # Otherwise, frame is complete |
| 68 | frame_sum = sum(self.frame_rolls) |
| 69 | # Edge Case: Invalid frame total in tenth frame |
| 70 | if frame_sum > 10 and self.frame_rolls[0] != 10: |
| 71 | raise ValueError("Invalid roll: pins down exceed 10 in a frame") |
| 72 | if frame_sum < 10: |
| 73 | # Open frame, game over |
| 74 | self.frames.append(self.frame_rolls) |
| 75 | self.frame_rolls = [] |
| 76 | return |
| 77 | |
| 78 | # Edge Case: Third roll in tenth frame |
| 79 | if len(self.frame_rolls) == 3: |
| 80 | # Validate the rolls according to tenth frame rules |
| 81 | if self.frame_rolls[0] == 10: # First roll was a strike |
| 82 | # Second and third rolls are fill balls |
| 83 | if self.frame_rolls[1] == 10: # Second roll was also a strike |
| 84 | # Third roll is a fill ball, can be 0-10 |
| 85 | pass |
| 86 | else: # Second roll was not a strike |
| 87 | # Sum of second and third must be <= 10 |
| 88 | if self.frame_rolls[1] + self.frame_rolls[2] > 10: |
| 89 | raise ValueError("Invalid fill balls") |
| 90 | else: # First roll was not a strike, so second must complete a spare |
| 91 | # Third roll is a fill ball, can be 0-10 |
| 92 | if self.frame_rolls[0] + self.frame_rolls[1] != 10: |
| 93 | raise ValueError("Invalid fill balls") |
| 94 | self.frames.append(self.frame_rolls) |
| 95 | self.frame_rolls = [] |
| 96 | |
| 97 | def _needs_fill_balls(self): |
| 98 | """ |
| 99 | Determine if the tenth frame needs fill balls. |
| 100 | |
| 101 | Returns: |
| 102 | bool: True if fill balls are needed, False otherwise |
| 103 | """ |
| 104 | if len(self.frames) < 10: |
| 105 | return False |
| 106 | if len(self.frames) > 10: |
| 107 | return False |
| 108 | |
| 109 | tenth_frame = self.frames[9] |
| 110 | # Edge Case: Tenth frame is a strike or spare, needs fill balls |
| 111 | return (len(tenth_frame) == 1 and tenth_frame[0] == 10) or \ |
| 112 | (len(tenth_frame) == 2 and sum(tenth_frame) == 10) |
| 113 | |
| 8 | 114 | def score(self): |
| 9 | | pass |
| 115 | """ |
| 116 | Calculate the total score for the game. |
| 117 | |
| 118 | Returns: |
| 119 | int: Total score for the game |
| 120 | |
| 121 | Raises: |
| 122 | ValueError: If the game is not yet complete |
| 123 | """ |
| 124 | # Edge Case: Game not yet complete |
| 125 | if len(self.frames) < 10: |
| 126 | raise IndexError("Game is not yet finished") |
| 127 | if len(self.frames) == 10 and self._needs_fill_balls(): |
| 128 | raise IndexError("Game is not yet finished") |
| 129 | |
| 130 | total_score = 0 |
| 131 | |
| 132 | # Edge Case: Calculate score for first 9 frames |
| 133 | for i in range(min(9, len(self.frames))): |
| 134 | frame = self.frames[i] |
| 135 | frame_score = sum(frame) |
| 136 | |
| 137 | # Edge Case: Strike scoring |
| 138 | if len(frame) == 1 and frame[0] == 10: # Strike |
| 139 | # Add next two rolls as bonus |
| 140 | if i + 1 < len(self.frames): |
| 141 | next_frame = self.frames[i + 1] |
| 142 | if len(next_frame) >= 2: |
| 143 | frame_score += next_frame[0] + next_frame[1] |
| 144 | elif len(next_frame) == 1 and i + 2 < len(self.frames): |
| 145 | # Next frame is also a strike |
| 146 | frame_score += next_frame[0] + self.frames[i + 2][0] |
| 147 | elif len(next_frame) == 1 and i + 2 >= len(self.frames): |
| 148 | # This shouldn't happen in a valid game |
| 149 | pass |
| 150 | # Edge Case: Spare scoring |
| 151 | elif len(frame) == 2 and sum(frame) == 10: # Spare |
| 152 | # Add next roll as bonus |
| 153 | if i + 1 < len(self.frames): |
| 154 | frame_score += self.frames[i + 1][0] |
| 155 | |
| 156 | total_score += frame_score |
| 157 | |
| 158 | # Edge Case: Calculate score for tenth frame |
| 159 | if len(self.frames) >= 10: |
| 160 | tenth_frame = self.frames[9] |
| 161 | total_score += sum(tenth_frame) |
| 162 | |
| 163 | # Handle any additional frames (fill balls) |
| 164 | for i in range(10, len(self.frames)): |
| 165 | total_score += sum(self.frames[i]) |
| 166 | |
| 167 | return total_score |
| 168 | |
| 169 | # Handled Edge Cases: Invalid pin count, game already completed, strike in regular frame, |
| 170 | # two rolls completed in regular frame, invalid frame total, first/second/third roll in tenth frame, |
| 171 | # invalid frame total in tenth frame, tenth frame is a strike or spare needs fill balls, |
| 172 | # game not yet complete, strike scoring, spare scoring |