| 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 = 0 |
| 8 | self.current_roll_in_frame = 0 |
| 9 | |
| 5 | 10 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 11 | """ |
| 12 | Record a roll in the game. |
| 13 | |
| 14 | Args: |
| 15 | pins (int): Number of pins knocked down in this roll. |
| 16 | |
| 17 | Raises: |
| 18 | ValueError: If the number of pins is invalid. |
| 19 | IndexError: If the game is already finished. |
| 20 | """ |
| 21 | # Edge Case: Invalid number of pins (negative or more than 10) |
| 22 | if pins < 0 or pins > 10: |
| 23 | raise ValueError("Pins must be between 0 and 10") |
| 24 | |
| 25 | # Edge Case: Game already finished |
| 26 | if self._is_game_complete(): |
| 27 | raise IndexError("Game is already finished") |
| 28 | |
| 29 | # Validate frame pin totals |
| 30 | if self.current_frame < 9: # Frames 1-9 |
| 31 | if self.current_roll_in_frame == 1: # Second roll of frame |
| 32 | first_roll = self.rolls[-1] if self.rolls else 0 |
| 33 | if first_roll + pins > 10: |
| 34 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 35 | |
| 36 | self.rolls.append(pins) |
| 37 | |
| 38 | # Update frame tracking |
| 39 | if self.current_frame < 9: # Frames 1-9 |
| 40 | if pins == 10 or self.current_roll_in_frame == 1: # Strike or second roll |
| 41 | self.current_frame += 1 |
| 42 | self.current_roll_in_frame = 0 |
| 43 | else: |
| 44 | self.current_roll_in_frame += 1 |
| 45 | else: # 10th frame - special handling |
| 46 | self._handle_tenth_frame_progression(pins) |
| 47 | |
| 48 | def _handle_tenth_frame_progression(self, pins): |
| 49 | """Handle progression in the 10th frame.""" |
| 50 | tenth_frame_start = len(self.rolls) - 1 |
| 51 | |
| 52 | if self.current_roll_in_frame == 0: # First roll of 10th frame |
| 53 | self.current_roll_in_frame = 1 |
| 54 | elif self.current_roll_in_frame == 1: # Second roll of 10th frame |
| 55 | first_roll = self.rolls[tenth_frame_start] |
| 56 | if first_roll == 10: # First roll was strike |
| 57 | self.current_roll_in_frame = 2 |
| 58 | elif first_roll + pins == 10: # Spare |
| 59 | self.current_roll_in_frame = 2 |
| 60 | else: # Open frame |
| 61 | self.current_frame += 1 |
| 62 | self.current_roll_in_frame = 0 |
| 63 | elif self.current_roll_in_frame == 2: # Fill ball |
| 64 | self.current_frame += 1 |
| 65 | self.current_roll_in_frame = 0 |
| 66 | |
| 8 | 67 | def score(self): |
| 9 | | pass |
| 68 | """ |
| 69 | Calculate the total score for the game. |
| 70 | |
| 71 | Returns: |
| 72 | int: The total score of the game. |
| 73 | |
| 74 | Raises: |
| 75 | IndexError: If the game is not yet complete. |
| 76 | """ |
| 77 | # Edge Case: Game not yet complete |
| 78 | if not self._is_game_complete(): |
| 79 | raise IndexError("Game is not yet finished") |
| 80 | |
| 81 | total_score = 0 |
| 82 | roll_index = 0 |
| 83 | |
| 84 | for frame in range(10): |
| 85 | # Edge Case: Tenth frame scoring |
| 86 | if frame == 9: # Tenth frame |
| 87 | total_score += sum(self.rolls[roll_index:]) |
| 88 | break |
| 89 | |
| 90 | # Edge Case: Strike scoring (first roll of frame is 10) |
| 91 | if self.rolls[roll_index] == 10: # Strike |
| 92 | total_score += 10 + self.rolls[roll_index + 1] + self.rolls[roll_index + 2] |
| 93 | roll_index += 1 # Move to next frame |
| 94 | |
| 95 | # Edge Case: Spare scoring (sum of two rolls in frame is 10) |
| 96 | elif self.rolls[roll_index] + self.rolls[roll_index + 1] == 10: # Spare |
| 97 | total_score += 10 + self.rolls[roll_index + 2] |
| 98 | roll_index += 2 # Move to next frame |
| 99 | |
| 100 | # Edge Case: Open frame scoring (sum of two rolls < 10) |
| 101 | else: # Open frame |
| 102 | total_score += self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 103 | roll_index += 2 # Move to next frame |
| 104 | |
| 105 | return total_score |
| 106 | |
| 107 | def _is_game_complete(self): |
| 108 | """ |
| 109 | Check if the game is complete. |
| 110 | |
| 111 | Returns: |
| 112 | bool: True if the game is complete, False otherwise. |
| 113 | """ |
| 114 | # Game is complete when we've finished 10 frames |
| 115 | return self.current_frame >= 10 |
| 116 | |
| 117 | # Handled Edge Cases: Invalid number of pins, game already completed, strike in frames 1-9, game not complete, tenth frame scoring, strike scoring, spare scoring, open frame scoring, not enough rolls, strike in frames 1-9 during completion check, not enough rolls for frame during completion check, tenth frame completion check |