| 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 pins is negative or greater than 10 |
| 17 | ValueError: If game is already over |
| 18 | """ |
| 19 | # Validate pins |
| 20 | if pins < 0: |
| 21 | raise ValueError("Pins must be between 0 and 10") |
| 22 | if pins > 10: |
| 23 | raise ValueError("Pins must be between 0 and 10") |
| 24 | |
| 25 | # Check if game is already over |
| 26 | if self._is_game_complete(): |
| 27 | raise ValueError("Game is already complete") |
| 28 | |
| 29 | self.rolls.append(pins) |
| 30 | |
| 8 | 31 | def score(self): |
| 9 | | pass |
| 32 | """ |
| 33 | Calculate the total score for the game. |
| 34 | |
| 35 | Returns: |
| 36 | int: The total score for the game |
| 37 | |
| 38 | Raises: |
| 39 | ValueError: If the game is not yet complete |
| 40 | """ |
| 41 | if not self._is_game_complete(): |
| 42 | raise ValueError("Game is not yet complete") |
| 43 | |
| 44 | total_score = 0 |
| 45 | roll_index = 0 |
| 46 | |
| 47 | # Score first 9 frames |
| 48 | for frame in range(9): |
| 49 | if roll_index >= len(self.rolls): |
| 50 | raise ValueError("Game is not yet complete") |
| 51 | |
| 52 | # Strike |
| 53 | if self.rolls[roll_index] == 10: |
| 54 | total_score += 10 + self.rolls[roll_index + 1] + self.rolls[roll_index + 2] |
| 55 | roll_index += 1 |
| 56 | # Spare |
| 57 | elif self.rolls[roll_index] + self.rolls[roll_index + 1] == 10: |
| 58 | total_score += 10 + self.rolls[roll_index + 2] |
| 59 | roll_index += 2 |
| 60 | # Open frame |
| 61 | else: |
| 62 | total_score += self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 63 | roll_index += 2 |
| 64 | |
| 65 | # Score 10th frame |
| 66 | while roll_index < len(self.rolls): |
| 67 | total_score += self.rolls[roll_index] |
| 68 | roll_index += 1 |
| 69 | |
| 70 | return total_score |
| 71 | |
| 72 | def _is_game_complete(self): |
| 73 | """ |
| 74 | Check if the game is complete. |
| 75 | |
| 76 | Returns: |
| 77 | bool: True if the game is complete, False otherwise |
| 78 | """ |
| 79 | if len(self.rolls) < 12: # Minimum rolls needed (9 open frames + 3 rolls in 10th frame) |
| 80 | return False |
| 81 | |
| 82 | roll_index = 0 |
| 83 | frame_count = 0 |
| 84 | |
| 85 | # Check first 9 frames |
| 86 | while frame_count < 9 and roll_index < len(self.rolls): |
| 87 | if self.rolls[roll_index] == 10: # Strike |
| 88 | roll_index += 1 |
| 89 | else: |
| 90 | roll_index += 2 |
| 91 | frame_count += 1 |
| 92 | |
| 93 | # If we haven't completed 9 frames, game is not complete |
| 94 | if frame_count < 9: |
| 95 | return False |
| 96 | |
| 97 | # Check 10th frame |
| 98 | if roll_index >= len(self.rolls): |
| 99 | return False |
| 100 | |
| 101 | # 10th frame: strike gets 2 more rolls |
| 102 | if self.rolls[roll_index] == 10: |
| 103 | return roll_index + 3 <= len(self.rolls) |
| 104 | # 10th frame: spare gets 1 more roll |
| 105 | elif roll_index + 1 < len(self.rolls) and self.rolls[roll_index] + self.rolls[roll_index + 1] == 10: |
| 106 | return roll_index + 3 <= len(self.rolls) |
| 107 | # 10th frame: open frame needs 2 rolls |
| 108 | else: |
| 109 | return roll_index + 2 <= len(self.rolls) |