| 1 | 1 | class BowlingGame: |
| 2 | 2 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 3 | self.rolls = [] |
| 4 | self.current_frame = 0 |
| 5 | self.frame_rolls = [] |
| 6 | |
| 5 | 7 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 8 | # Edge Case: Invalid pin count (negative or more than 10) |
| 9 | if pins < 0 or pins > 10: |
| 10 | raise ValueError("Pins must be between 0 and 10") |
| 11 | |
| 12 | # Edge Case: Game already has 10 complete frames |
| 13 | if self.current_frame >= 10 and not self._needs_fill_balls(): |
| 14 | raise ValueError("Cannot roll after game is complete") |
| 15 | |
| 16 | # Edge Case: Adding too many pins in a frame (except in fill balls) |
| 17 | if len(self.frame_rolls) == 1: |
| 18 | if self.current_frame == 9: |
| 19 | # 10th frame special handling |
| 20 | if self.frame_rolls[0] == 10: |
| 21 | # After a strike, any pins are allowed for first fill ball |
| 22 | pass |
| 23 | elif self.frame_rolls[0] + pins > 10: |
| 24 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 25 | elif self.frame_rolls[0] + pins > 10: |
| 26 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 27 | |
| 28 | # No validation needed for fill balls in 10th frame |
| 29 | # Each fill ball can be 0-10 regardless of previous rolls |
| 30 | |
| 31 | self.rolls.append(pins) |
| 32 | self.frame_rolls.append(pins) |
| 33 | |
| 34 | # Determine if frame is complete |
| 35 | frame_complete = False |
| 36 | if self.current_frame < 9: # Frames 1-9 |
| 37 | if self.frame_rolls[0] == 10 or len(self.frame_rolls) == 2: # Strike or two rolls |
| 38 | frame_complete = True |
| 39 | else: # 10th frame |
| 40 | if len(self.frame_rolls) == 3: # Three rolls in 10th frame |
| 41 | frame_complete = True |
| 42 | elif len(self.frame_rolls) == 2: # Two rolls in 10th frame |
| 43 | # If not a strike or spare, frame is complete |
| 44 | if self.frame_rolls[0] != 10 and sum(self.frame_rolls) < 10: |
| 45 | frame_complete = True |
| 46 | # If it's a strike or spare, need one more roll (fill ball) |
| 47 | elif self.frame_rolls[0] == 10 or sum(self.frame_rolls) == 10: |
| 48 | frame_complete = False |
| 49 | |
| 50 | if frame_complete: |
| 51 | self.current_frame += 1 |
| 52 | self.frame_rolls = [] |
| 53 | |
| 8 | 54 | def score(self): |
| 9 | | pass |
| 55 | # Edge Case: No rolls yet - return 0 |
| 56 | if len(self.rolls) == 0: |
| 57 | return 0 |
| 58 | |
| 59 | # Edge Case: Game not yet complete |
| 60 | if self.current_frame < 10 or self._needs_fill_balls(): |
| 61 | raise ValueError("Game is not yet complete") |
| 62 | |
| 63 | total_score = 0 |
| 64 | roll_index = 0 |
| 65 | |
| 66 | for frame in range(10): |
| 67 | if self._is_strike(roll_index): # Strike |
| 68 | # Edge Case: Incomplete game data for strike calculation |
| 69 | if roll_index + 2 >= len(self.rolls): |
| 70 | raise ValueError("Game is not yet complete") |
| 71 | total_score += 10 + self.rolls[roll_index + 1] + self.rolls[roll_index + 2] |
| 72 | roll_index += 1 |
| 73 | elif self._is_spare(roll_index): # Spare |
| 74 | # Edge Case: Incomplete game data for spare calculation |
| 75 | if roll_index + 2 >= len(self.rolls): |
| 76 | raise ValueError("Game is not yet complete") |
| 77 | total_score += 10 + self.rolls[roll_index + 2] |
| 78 | roll_index += 2 |
| 79 | else: # Open frame |
| 80 | total_score += self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 81 | roll_index += 2 |
| 82 | |
| 83 | return total_score |
| 84 | |
| 85 | def _is_strike(self, roll_index): |
| 86 | return self.rolls[roll_index] == 10 |
| 87 | |
| 88 | def _is_spare(self, roll_index): |
| 89 | # Handle either an index or a list of two rolls |
| 90 | if isinstance(roll_index, int): |
| 91 | return self.rolls[roll_index] + self.rolls[roll_index + 1] == 10 |
| 92 | else: # It's a list |
| 93 | return sum(roll_index) == 10 |
| 94 | |
| 95 | def _needs_fill_balls(self): |
| 96 | # Edge Case: Check if 10th frame needs fill balls but hasn't received them |
| 97 | if self.current_frame < 9: # Not in 10th frame yet |
| 98 | return False |
| 99 | if self.current_frame == 9 and len(self.frame_rolls) == 0: # Just started 10th frame |
| 100 | return False |
| 101 | |
| 102 | # In 10th frame, check if we need more rolls |
| 103 | if len(self.frame_rolls) == 1: # Only first roll in 10th frame |
| 104 | return True # Always need at least one more |
| 105 | elif len(self.frame_rolls) == 2: # Two rolls in 10th frame |
| 106 | # If it's a strike or spare, need one more (fill ball) |
| 107 | return self.frame_rolls[0] == 10 or sum(self.frame_rolls) == 10 |
| 108 | else: # Already have 3 rolls in 10th frame |
| 109 | return False |
| 110 | |
| 111 | # Handled Edge Cases: Invalid pin count, game already complete, too many pins in frame, incomplete game for scoring, strike calculation with insufficient data, spare calculation with insufficient data, 10th frame fill ball requirements |