| 1 | 1 | class BowlingGame: |
| 2 | 2 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 3 | """ |
| 4 | Initialize a new bowling game. |
| 5 | """ |
| 6 | self.rolls = [] |
| 7 | |
| 5 | 8 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 9 | """ |
| 10 | Record a roll in the game. |
| 11 | |
| 12 | Args: |
| 13 | pins (int): Number of pins knocked down in this roll. |
| 14 | |
| 15 | Raises: |
| 16 | ValueError: If the number of pins is invalid. |
| 17 | """ |
| 18 | # Edge Case: Invalid pin count |
| 19 | if pins < 0 or pins > 10: |
| 20 | raise ValueError("Pins must be between 0 and 10") |
| 21 | |
| 22 | # Edge Case: Game already complete |
| 23 | if self._is_game_complete(): |
| 24 | raise ValueError("Game is already complete") |
| 25 | |
| 26 | # Edge Case: Too many pins in a frame (except in special 10th frame cases) |
| 27 | if not self._is_valid_roll(pins): |
| 28 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 29 | |
| 30 | self.rolls.append(pins) |
| 31 | |
| 32 | def _is_valid_roll(self, pins): |
| 33 | """ |
| 34 | Check if a roll is valid given the current game state. |
| 35 | |
| 36 | Args: |
| 37 | pins (int): Number of pins knocked down in this roll. |
| 38 | |
| 39 | Returns: |
| 40 | bool: True if the roll is valid, False otherwise. |
| 41 | """ |
| 42 | # First, determine which frame and roll we're currently in |
| 43 | frame_index, roll_in_frame = self._get_current_frame_and_roll() |
| 44 | |
| 45 | # For frames 1-9 (indices 0-8) |
| 46 | if frame_index < 9: |
| 47 | if roll_in_frame == 0: # First roll of frame |
| 48 | return True # First roll is always valid (0-10 pins) |
| 49 | else: # Second roll of frame |
| 50 | # Second roll is valid if sum doesn't exceed 10 |
| 51 | return self.rolls[-1] + pins <= 10 |
| 52 | |
| 53 | # For frame 10 (index 9) |
| 54 | if frame_index == 9: |
| 55 | if roll_in_frame == 0: # First roll of 10th frame |
| 56 | return True # First roll is always valid (0-10 pins) |
| 57 | elif roll_in_frame == 1: # Second roll of 10th frame |
| 58 | # Get the first roll of the 10th frame |
| 59 | first_roll_index = self._get_tenth_frame_start_index() |
| 60 | first_roll = self.rolls[first_roll_index] |
| 61 | if first_roll == 10: # First roll was a strike |
| 62 | return True # Second roll can be anything (0-10 pins) |
| 63 | else: |
| 64 | return first_roll + pins <= 10 # Sum must not exceed 10 |
| 65 | else: # Third roll of 10th frame |
| 66 | # This is a fill ball, so it's always valid (0-10 pins) |
| 67 | return True |
| 68 | |
| 69 | # For any frame beyond 10 (shouldn't happen in a valid game) |
| 70 | return True |
| 71 | |
| 72 | def _get_current_frame_and_roll(self): |
| 73 | """ |
| 74 | Determine which frame and which roll within that frame we're currently in. |
| 75 | |
| 76 | Returns: |
| 77 | tuple: (frame_index, roll_in_frame) where frame_index is 0-9 and roll_in_frame is 0-2 |
| 78 | """ |
| 79 | # For no rolls, we're in frame 0, roll 0 |
| 80 | if len(self.rolls) == 0: |
| 81 | return (0, 0) |
| 82 | |
| 83 | # First, check if we're in frames 1-9 |
| 84 | frame_index = 0 |
| 85 | roll_count_in_frames = 0 |
| 86 | |
| 87 | # Process frames 1-9 |
| 88 | while frame_index < 9 and roll_count_in_frames < len(self.rolls): |
| 89 | if self._is_strike(roll_count_in_frames): |
| 90 | # Strike - one roll for this frame |
| 91 | if roll_count_in_frames == len(self.rolls) - 1: |
| 92 | # This is the current roll |
| 93 | return (frame_index, 0) |
| 94 | roll_count_in_frames += 1 |
| 95 | else: |
| 96 | # Not a strike - two rolls for this frame |
| 97 | if roll_count_in_frames == len(self.rolls) - 1: |
| 98 | # This is the first roll of this frame |
| 99 | return (frame_index, 0) |
| 100 | elif roll_count_in_frames + 1 == len(self.rolls): |
| 101 | # This is the second roll of this frame |
| 102 | return (frame_index, 1) |
| 103 | roll_count_in_frames += 2 |
| 104 | frame_index += 1 |
| 105 | |
| 106 | # If we get here, we're in frame 10 (index 9) |
| 107 | # Find where frame 10 starts |
| 108 | roll_index = 0 |
| 109 | frames_completed = 0 |
| 110 | |
| 111 | # Process frames 1-9 |
| 112 | while frames_completed < 9 and roll_index < len(self.rolls): |
| 113 | if self._is_strike(roll_index): |
| 114 | roll_index += 1 |
| 115 | else: |
| 116 | roll_index += 2 |
| 117 | frames_completed += 1 |
| 118 | |
| 119 | # roll_index now points to the start of frame 10 |
| 120 | # Calculate which roll we are in frame 10 (0, 1, or 2) |
| 121 | roll_in_frame = len(self.rolls) - roll_index - 1 |
| 122 | return (9, roll_in_frame) |
| 123 | |
| 124 | def _get_tenth_frame_start_index(self): |
| 125 | """ |
| 126 | Get the index of the first roll in the 10th frame. |
| 127 | |
| 128 | Returns: |
| 129 | int: Index of the first roll in the 10th frame. |
| 130 | """ |
| 131 | roll_index = 0 |
| 132 | frames_completed = 0 |
| 133 | |
| 134 | # Process frames 1-9 |
| 135 | while frames_completed < 9 and roll_index < len(self.rolls): |
| 136 | if self._is_strike(roll_index): |
| 137 | roll_index += 1 |
| 138 | else: |
| 139 | roll_index += 2 |
| 140 | frames_completed += 1 |
| 141 | |
| 142 | return roll_index |
| 143 | |
| 8 | 144 | def score(self): |
| 9 | | pass |
| 145 | """ |
| 146 | Calculate the total score for the game. |
| 147 | |
| 148 | Returns: |
| 149 | int: The total score of the game. |
| 150 | |
| 151 | Raises: |
| 152 | ValueError: If the game is not complete or has too many rolls. |
| 153 | """ |
| 154 | # Edge Case: Game not complete |
| 155 | if not self._is_game_complete(): |
| 156 | raise ValueError("Score cannot be taken until the end of the game") |
| 157 | |
| 158 | score = 0 |
| 159 | roll_index = 0 |
| 160 | |
| 161 | # Score frames 1-9 |
| 162 | for frame in range(9): |
| 163 | if roll_index >= len(self.rolls): |
| 164 | raise ValueError("Score cannot be taken until the end of the game") |
| 165 | |
| 166 | if self._is_strike(roll_index): # Strike |
| 167 | # Edge Case: Not enough rolls for strike bonus |
| 168 | if roll_index + 2 >= len(self.rolls): |
| 169 | raise ValueError("Score cannot be taken until the end of the game") |
| 170 | |
| 171 | score += 10 + self.rolls[roll_index + 1] + self.rolls[roll_index + 2] |
| 172 | roll_index += 1 |
| 173 | elif self._is_spare(roll_index): # Spare |
| 174 | # Edge Case: Not enough rolls for spare bonus |
| 175 | if roll_index + 2 >= len(self.rolls): |
| 176 | raise ValueError("Score cannot be taken until the end of the game") |
| 177 | |
| 178 | score += 10 + self.rolls[roll_index + 2] |
| 179 | roll_index += 2 |
| 180 | else: # Open frame |
| 181 | score += self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 182 | roll_index += 2 |
| 183 | |
| 184 | # Score 10th frame |
| 185 | if roll_index >= len(self.rolls): |
| 186 | raise ValueError("Score cannot be taken until the end of the game") |
| 187 | |
| 188 | # For the 10th frame, we just sum all the rolls in the frame |
| 189 | # The frame is already complete, so we can just add them up |
| 190 | tenth_frame_start = roll_index |
| 191 | |
| 192 | # Count how many rolls are in the 10th frame |
| 193 | if self._is_strike(tenth_frame_start): |
| 194 | # First roll is a strike |
| 195 | if tenth_frame_start + 2 < len(self.rolls): |
| 196 | # Add all three rolls |
| 197 | score += self.rolls[tenth_frame_start] + self.rolls[tenth_frame_start + 1] + self.rolls[tenth_frame_start + 2] |
| 198 | else: |
| 199 | # This shouldn't happen if game is complete |
| 200 | raise ValueError("Score cannot be taken until the end of the game") |
| 201 | elif (tenth_frame_start + 1 < len(self.rolls) and |
| 202 | self.rolls[tenth_frame_start] + self.rolls[tenth_frame_start + 1] == 10): |
| 203 | # First two rolls are a spare |
| 204 | if tenth_frame_start + 2 < len(self.rolls): |
| 205 | # Add all three rolls |
| 206 | score += self.rolls[tenth_frame_start] + self.rolls[tenth_frame_start + 1] + self.rolls[tenth_frame_start + 2] |
| 207 | else: |
| 208 | # This shouldn't happen if game is complete |
| 209 | raise ValueError("Score cannot be taken until the end of the game") |
| 210 | else: |
| 211 | # Open frame - just two rolls |
| 212 | if tenth_frame_start + 1 < len(self.rolls): |
| 213 | score += self.rolls[tenth_frame_start] + self.rolls[tenth_frame_start + 1] |
| 214 | else: |
| 215 | # This shouldn't happen if game is complete |
| 216 | raise ValueError("Score cannot be taken until the end of the game") |
| 217 | |
| 218 | return score |
| 219 | |
| 220 | def _is_strike(self, roll_index): |
| 221 | """ |
| 222 | Check if a roll is a strike. |
| 223 | |
| 224 | Args: |
| 225 | roll_index (int): Index of the roll to check. |
| 226 | |
| 227 | Returns: |
| 228 | bool: True if the roll is a strike, False otherwise. |
| 229 | """ |
| 230 | return self.rolls[roll_index] == 10 |
| 231 | |
| 232 | def _is_spare(self, roll_index): |
| 233 | """ |
| 234 | Check if a frame is a spare. |
| 235 | |
| 236 | Args: |
| 237 | roll_index (int): Index of the first roll of the frame. |
| 238 | |
| 239 | Returns: |
| 240 | bool: True if the frame is a spare, False otherwise. |
| 241 | """ |
| 242 | if roll_index + 1 >= len(self.rolls): |
| 243 | return False |
| 244 | return self.rolls[roll_index] + self.rolls[roll_index + 1] == 10 |
| 245 | |
| 246 | def _is_game_complete(self): |
| 247 | """ |
| 248 | Check if the game is complete. |
| 249 | |
| 250 | Returns: |
| 251 | bool: True if the game is complete, False otherwise. |
| 252 | """ |
| 253 | if len(self.rolls) == 0: |
| 254 | return False |
| 255 | |
| 256 | roll_index = 0 |
| 257 | frames_completed = 0 |
| 258 | |
| 259 | # Process frames 1-9 |
| 260 | while frames_completed < 9 and roll_index < len(self.rolls): |
| 261 | if self._is_strike(roll_index): |
| 262 | roll_index += 1 |
| 263 | else: |
| 264 | roll_index += 2 |
| 265 | frames_completed += 1 |
| 266 | |
| 267 | # Process 10th frame |
| 268 | if frames_completed == 9 and roll_index < len(self.rolls): |
| 269 | # First roll of 10th frame exists |
| 270 | tenth_frame_start = roll_index |
| 271 | roll_index += 1 |
| 272 | |
| 273 | # Check if we have a second roll |
| 274 | if roll_index < len(self.rolls): |
| 275 | # Second roll exists |
| 276 | # Check if we need a third roll (strike or spare) |
| 277 | if (self.rolls[tenth_frame_start] == 10 or |
| 278 | self.rolls[tenth_frame_start] + self.rolls[tenth_frame_start + 1] == 10): |
| 279 | # Need a third roll |
| 280 | roll_index += 1 |
| 281 | else: |
| 282 | # Open frame - second roll completes frame |
| 283 | roll_index += 1 |
| 284 | |
| 285 | # Check if we have the required number of rolls |
| 286 | frames_completed += 1 |
| 287 | |
| 288 | return frames_completed == 10 and roll_index == len(self.rolls) |
| 289 | |
| 290 | # Handled Edge Cases: Negative pins, too many pins in a single roll, too many pins in a frame, |
| 291 | # game not complete, too many rolls, not enough rolls for strike bonus, |
| 292 | # not enough rolls for spare bonus, incomplete game |