| 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_roll = 0 |
| 8 | |
| 5 | 9 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 10 | """ |
| 11 | Record a roll in the game. |
| 12 | |
| 13 | Args: |
| 14 | pins (int): Number of pins knocked down in this roll. |
| 15 | |
| 16 | Raises: |
| 17 | ValueError: If the number of pins is invalid. |
| 18 | """ |
| 19 | # Edge Case: Invalid pin count (negative or more than 10) |
| 20 | if pins < 0 or pins > 10: |
| 21 | raise ValueError("pins must be between 0 and 10") |
| 22 | |
| 23 | # Edge Case: Game is already complete |
| 24 | if self.is_game_complete(): |
| 25 | raise ValueError("game is already complete") |
| 26 | |
| 27 | # Edge Case: Invalid roll after a strike in frames 1-9 |
| 28 | if self.current_roll < 18: # Frames 1-9 (18 rolls max) |
| 29 | if (self.current_roll % 2 == 0) and pins == 10: # First roll of frame is a strike |
| 30 | # Add the strike and a 0 for the second roll |
| 31 | self.rolls.append(pins) |
| 32 | self.rolls.append(0) |
| 33 | self.current_roll += 2 |
| 34 | return |
| 35 | |
| 36 | # Edge Case: Tenth frame validation |
| 37 | if self.current_roll >= 18: # In tenth frame |
| 38 | # Check if this is the third roll in tenth frame |
| 39 | if self.current_roll == 20: |
| 40 | # Edge Case: Third roll in tenth frame must follow a strike or spare |
| 41 | if not (self.is_tenth_frame_strike() or self.is_tenth_frame_spare()): |
| 42 | raise ValueError("invalid fill balls") |
| 43 | |
| 44 | # Edge Case: Invalid fill ball after a spare |
| 45 | if self.current_roll == 19 and self.is_tenth_frame_spare(): |
| 46 | if pins > 10: |
| 47 | raise ValueError("pins must be between 0 and 10") |
| 48 | |
| 49 | # Edge Case: Invalid second fill ball after two strikes |
| 50 | if self.current_roll == 20 and self.is_tenth_frame_strike(): |
| 51 | # If first two rolls were strikes, third roll can be 0-10 |
| 52 | # If first roll was strike and second wasn't, third roll must complete to <= 10 |
| 53 | if self.rolls[18] == 10: # First roll was strike |
| 54 | if self.rolls[19] == 10: # Second roll was also strike |
| 55 | if pins > 10: |
| 56 | raise ValueError("pins must be between 0 and 10") |
| 57 | else: # Second roll wasn't strike |
| 58 | if self.rolls[19] + pins > 10: |
| 59 | raise ValueError("invalid fill balls") |
| 60 | else: # First roll wasn't strike (must be spare) |
| 61 | if pins > 10: |
| 62 | raise ValueError("pins must be between 0 and 10") |
| 63 | |
| 64 | self.rolls.append(pins) |
| 65 | self.current_roll += 1 |
| 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 | ValueError: If the game is not yet complete. |
| 76 | """ |
| 77 | # Edge Case: Game is not complete |
| 78 | if not self.is_game_complete(): |
| 79 | raise ValueError("game is not yet complete") |
| 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 += self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 88 | # Add third roll if it exists |
| 89 | if len(self.rolls) > roll_index + 2: |
| 90 | total_score += self.rolls[roll_index + 2] |
| 91 | else: |
| 92 | # Edge Case: Strike scoring |
| 93 | if self.is_strike(roll_index): |
| 94 | total_score += 10 + self.strike_bonus(roll_index) |
| 95 | roll_index += 2 |
| 96 | # Edge Case: Spare scoring |
| 97 | elif self.is_spare(roll_index): |
| 98 | total_score += 10 + self.spare_bonus(roll_index) |
| 99 | roll_index += 2 |
| 100 | # Edge Case: Open frame scoring |
| 101 | else: |
| 102 | total_score += self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 103 | roll_index += 2 |
| 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 | # Edge Case: Not enough rolls |
| 115 | if self.current_roll < 18: |
| 116 | return False |
| 117 | |
| 118 | # Edge Case: Exactly 18 rolls but tenth frame was open |
| 119 | if self.current_roll == 18: |
| 120 | return True |
| 121 | |
| 122 | # Edge Case: 19 rolls means spare in tenth frame |
| 123 | if self.current_roll == 19: |
| 124 | return self.is_tenth_frame_spare() |
| 125 | |
| 126 | # Edge Case: 20 rolls means either two strikes or strike+non-strike in tenth frame |
| 127 | if self.current_roll == 20: |
| 128 | return True |
| 129 | |
| 130 | # Edge Case: 21 rolls means three strikes in tenth frame |
| 131 | if self.current_roll == 21: |
| 132 | return True |
| 133 | |
| 134 | return False |
| 135 | |
| 136 | def is_strike(self, roll_index): |
| 137 | """ |
| 138 | Check if the roll at the given index is a strike. |
| 139 | |
| 140 | Args: |
| 141 | roll_index (int): Index of the roll to check. |
| 142 | |
| 143 | Returns: |
| 144 | bool: True if it's a strike, False otherwise. |
| 145 | """ |
| 146 | return self.rolls[roll_index] == 10 |
| 147 | |
| 148 | def is_spare(self, roll_index): |
| 149 | """ |
| 150 | Check if the frame starting at the given index is a spare. |
| 151 | |
| 152 | Args: |
| 153 | roll_index (int): Index of the first roll of the frame. |
| 154 | |
| 155 | Returns: |
| 156 | bool: True if it's a spare, False otherwise. |
| 157 | """ |
| 158 | return self.rolls[roll_index] + self.rolls[roll_index + 1] == 10 |
| 159 | |
| 160 | def strike_bonus(self, roll_index): |
| 161 | """ |
| 162 | Calculate the bonus for a strike. |
| 163 | |
| 164 | Args: |
| 165 | roll_index (int): Index of the strike roll. |
| 166 | |
| 167 | Returns: |
| 168 | int: The bonus points for the strike. |
| 169 | """ |
| 170 | # Edge Case: Strike at the end of game |
| 171 | if roll_index >= len(self.rolls) - 2: |
| 172 | return 0 |
| 173 | return self.rolls[roll_index + 2] + self.rolls[roll_index + 3] |
| 174 | |
| 175 | def spare_bonus(self, roll_index): |
| 176 | """ |
| 177 | Calculate the bonus for a spare. |
| 178 | |
| 179 | Args: |
| 180 | roll_index (int): Index of the first roll of the spare frame. |
| 181 | |
| 182 | Returns: |
| 183 | int: The bonus points for the spare. |
| 184 | """ |
| 185 | # Edge Case: Spare at the end of game |
| 186 | if roll_index >= len(self.rolls) - 2: |
| 187 | return 0 |
| 188 | return self.rolls[roll_index + 2] |
| 189 | |
| 190 | def is_tenth_frame_strike(self): |
| 191 | """ |
| 192 | Check if the tenth frame started with a strike. |
| 193 | |
| 194 | Returns: |
| 195 | bool: True if tenth frame started with a strike, False otherwise. |
| 196 | """ |
| 197 | return len(self.rolls) >= 19 and self.rolls[18] == 10 |
| 198 | |
| 199 | def is_tenth_frame_spare(self): |
| 200 | """ |
| 201 | Check if the tenth frame is a spare. |
| 202 | |
| 203 | Returns: |
| 204 | bool: True if tenth frame is a spare, False otherwise. |
| 205 | """ |
| 206 | return (len(self.rolls) >= 20 and |
| 207 | self.rolls[18] + self.rolls[19] == 10) |
| 208 | |
| 209 | # Handled Edge Cases: Invalid pin count, game already complete, invalid roll after strike, tenth frame validation, invalid fill balls |
| 210 | # Handled Edge Cases: Game not complete, strike scoring, spare scoring, open frame scoring |
| 211 | # Handled Edge Cases: Not enough rolls, exactly 18 rolls, 19 rolls with spare, 20 rolls, 21 rolls |
| 212 | # Handled Edge Cases: Strike at the end of game |
| 213 | # Handled Edge Cases: Spare at the end of game |