| 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 | IndexError: If game is already over |
| 21 | """ |
| 22 | # Edge Case: Invalid pin count (negative) |
| 23 | if pins < 0: |
| 24 | raise ValueError("Pins cannot be negative") |
| 25 | |
| 26 | # Edge Case: Game already completed |
| 27 | if self._is_game_complete(): |
| 28 | raise ValueError("Game is already complete") |
| 29 | |
| 30 | # Edge Case: Invalid fill balls (check before general pin validation) |
| 31 | if len(self.frames) == 9 and self._needs_fill_balls(): |
| 32 | # We're in the 10th frame and need fill balls |
| 33 | # Check if this roll is a fill ball with invalid pin count |
| 34 | if pins > 10: |
| 35 | raise ValueError("Invalid fill ball") |
| 36 | |
| 37 | # Edge Case: Invalid pin count (more than 10) |
| 38 | if pins > 10: |
| 39 | raise ValueError("Pin count exceeds pins on the lane") |
| 40 | |
| 41 | # Edge Case: Invalid roll in frame (more than 10 pins in a frame) |
| 42 | if len(self.frame_rolls) == 1 and not self._is_strike(self.frame_rolls[0]): |
| 43 | if self.frame_rolls[0] + pins > 10: |
| 44 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 45 | |
| 46 | self.frame_rolls.append(pins) |
| 47 | self.rolls.append(pins) |
| 48 | |
| 49 | # Check if frame is complete |
| 50 | if len(self.frames) < 9: # Frames 1-9 |
| 51 | if self._is_strike(pins) or len(self.frame_rolls) == 2: |
| 52 | self.frames.append(self.frame_rolls[:]) |
| 53 | self.frame_rolls = [] |
| 54 | self.current_frame += 1 |
| 55 | elif len(self.frames) == 9: # 10th frame |
| 56 | # Handle 10th frame completion |
| 57 | if len(self.frame_rolls) == 2: |
| 58 | # Check if we have an open frame or need fill balls |
| 59 | if not self._is_spare(self.frame_rolls[0], self.frame_rolls[1]) and not self._is_strike(self.frame_rolls[0]): |
| 60 | # Open frame - game complete |
| 61 | self.frames.append(self.frame_rolls[:]) |
| 62 | self.frame_rolls = [] |
| 63 | elif len(self.frame_rolls) == 3: |
| 64 | # 10th frame with fill balls - game complete |
| 65 | self.frames.append(self.frame_rolls[:]) |
| 66 | self.frame_rolls = [] |
| 67 | |
| 8 | 68 | def score(self): |
| 9 | | pass |
| 69 | """ |
| 70 | Calculate the total score for the game. |
| 71 | |
| 72 | Returns: |
| 73 | int: The total score for the game |
| 74 | |
| 75 | Raises: |
| 76 | IndexError: If the game is not yet complete |
| 77 | """ |
| 78 | # Edge Case: Game not yet complete |
| 79 | if not self._is_game_complete(): |
| 80 | raise ValueError("Game is not complete") |
| 81 | |
| 82 | total_score = 0 |
| 83 | roll_index = 0 |
| 84 | |
| 85 | for frame_index in range(10): |
| 86 | frame = self.frames[frame_index] |
| 87 | |
| 88 | if frame_index < 9: # Frames 1-9 |
| 89 | if self._is_strike(frame[0]): |
| 90 | # Strike: 10 + next two rolls |
| 91 | total_score += 10 + self._next_two_rolls(roll_index) |
| 92 | roll_index += 1 |
| 93 | elif self._is_spare(frame[0], frame[1]): |
| 94 | # Spare: 10 + next roll |
| 95 | total_score += 10 + self._next_roll(roll_index) |
| 96 | roll_index += 2 |
| 97 | else: |
| 98 | # Open frame: sum of two rolls |
| 99 | total_score += frame[0] + frame[1] |
| 100 | roll_index += 2 |
| 101 | else: # 10th frame |
| 102 | total_score += sum(frame) |
| 103 | |
| 104 | return total_score |
| 105 | |
| 106 | def _is_strike(self, pins): |
| 107 | """ |
| 108 | Check if a roll is a strike. |
| 109 | |
| 110 | Args: |
| 111 | pins (int): Number of pins knocked down |
| 112 | |
| 113 | Returns: |
| 114 | bool: True if it's a strike, False otherwise |
| 115 | """ |
| 116 | return pins == 10 |
| 117 | |
| 118 | def _is_spare(self, first_roll, second_roll): |
| 119 | """ |
| 120 | Check if two rolls form a spare. |
| 121 | |
| 122 | Args: |
| 123 | first_roll (int): Pins knocked down in first roll |
| 124 | second_roll (int): Pins knocked down in second roll |
| 125 | |
| 126 | Returns: |
| 127 | bool: True if it's a spare, False otherwise |
| 128 | """ |
| 129 | return first_roll + second_roll == 10 |
| 130 | |
| 131 | def _next_roll(self, roll_index): |
| 132 | """ |
| 133 | Get the value of the next roll. |
| 134 | |
| 135 | Args: |
| 136 | roll_index (int): Current roll index |
| 137 | |
| 138 | Returns: |
| 139 | int: Value of the next roll |
| 140 | """ |
| 141 | return self.rolls[roll_index + 2] if roll_index + 2 < len(self.rolls) else 0 |
| 142 | |
| 143 | def _next_two_rolls(self, roll_index): |
| 144 | """ |
| 145 | Get the sum of the next two rolls. |
| 146 | |
| 147 | Args: |
| 148 | roll_index (int): Current roll index |
| 149 | |
| 150 | Returns: |
| 151 | int: Sum of the next two rolls |
| 152 | """ |
| 153 | result = 0 |
| 154 | if roll_index + 1 < len(self.rolls): |
| 155 | result += self.rolls[roll_index + 1] |
| 156 | if roll_index + 2 < len(self.rolls): |
| 157 | result += self.rolls[roll_index + 2] |
| 158 | return result |
| 159 | |
| 160 | def _is_game_complete(self): |
| 161 | """ |
| 162 | Check if the game is complete. |
| 163 | |
| 164 | Returns: |
| 165 | bool: True if the game is complete, False otherwise |
| 166 | """ |
| 167 | if len(self.frames) < 10: |
| 168 | return False |
| 169 | |
| 170 | # If we have 10 frames and no current frame rolls, game is complete |
| 171 | if len(self.frames) == 10 and len(self.frame_rolls) == 0: |
| 172 | return True |
| 173 | |
| 174 | # Check 10th frame completion |
| 175 | if len(self.frames) == 9: # Working on 10th frame |
| 176 | if len(self.frame_rolls) == 0: |
| 177 | return False |
| 178 | elif len(self.frame_rolls) == 1: |
| 179 | if self._is_strike(self.frame_rolls[0]): |
| 180 | return False # Need 2 more rolls |
| 181 | else: |
| 182 | return False # Need at least 1 more roll |
| 183 | elif len(self.frame_rolls) == 2: |
| 184 | if self._is_strike(self.frame_rolls[0]): |
| 185 | return False # Need 1 more roll |
| 186 | elif self._is_spare(self.frame_rolls[0], self.frame_rolls[1]): |
| 187 | return False # Need 1 more roll |
| 188 | else: |
| 189 | return True # Open frame, game complete |
| 190 | elif len(self.frame_rolls) == 3: |
| 191 | return True # 10th frame with fill balls complete |
| 192 | |
| 193 | return False |
| 194 | |
| 195 | def _needs_fill_balls(self): |
| 196 | """ |
| 197 | Check if the 10th frame needs fill balls. |
| 198 | |
| 199 | Returns: |
| 200 | bool: True if fill balls are needed, False otherwise |
| 201 | """ |
| 202 | # Only relevant when we're working on the 10th frame |
| 203 | if len(self.frames) < 9: |
| 204 | return False |
| 205 | if len(self.frames) == 10: |
| 206 | return False # 10th frame is already complete |
| 207 | |
| 208 | # We're working on the 10th frame (9 complete frames so far) |
| 209 | if len(self.frame_rolls) == 1: |
| 210 | # If first roll is a strike, we need 2 more rolls |
| 211 | return self._is_strike(self.frame_rolls[0]) |
| 212 | elif len(self.frame_rolls) == 2: |
| 213 | # If first roll was a strike, we still need 1 more roll |
| 214 | if self._is_strike(self.frame_rolls[0]): |
| 215 | return True |
| 216 | # If it's a spare, we need 1 more roll |
| 217 | elif self._is_spare(self.frame_rolls[0], self.frame_rolls[1]): |
| 218 | return True |
| 219 | # If it's not a spare and not a strike, we're done |
| 220 | else: |
| 221 | return False |
| 222 | elif len(self.frame_rolls) == 3: |
| 223 | # We have all rolls for the 10th frame |
| 224 | return False |
| 225 | |
| 226 | return False |
| 227 | |
| 228 | # Handled Edge Cases: Invalid pin count (negative or more than 10), game already completed, invalid roll in frame, invalid fill balls, game not yet complete |