| 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_frame_rolls = [] |
| 9 | self.game_over = False |
| 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 | ValueError: If game is already over |
| 21 | """ |
| 22 | # Edge Case: Invalid number of pins |
| 23 | if pins < 0 or pins > 10: |
| 24 | raise ValueError("Pins must be between 0 and 10") |
| 25 | |
| 26 | # Edge Case: Game already completed |
| 27 | if self.game_over: |
| 28 | raise ValueError("Cannot roll after game is over") |
| 29 | |
| 30 | # Add the roll |
| 31 | self.rolls.append(pins) |
| 32 | self.current_frame_rolls.append(pins) |
| 33 | |
| 34 | # Handle regular frames (0-8) |
| 35 | if self.current_frame < 9: |
| 36 | # Strike - frame is complete |
| 37 | if pins == 10: |
| 38 | self.current_frame += 1 |
| 39 | self.current_frame_rolls = [] |
| 40 | # Second roll of frame |
| 41 | elif len(self.current_frame_rolls) == 2: |
| 42 | # Edge Case: Frame total exceeds 10 |
| 43 | if sum(self.current_frame_rolls) > 10: |
| 44 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 45 | self.current_frame += 1 |
| 46 | self.current_frame_rolls = [] |
| 47 | # Handle 10th frame |
| 48 | else: |
| 49 | # Validate frame totals in 10th frame |
| 50 | if len(self.current_frame_rolls) == 2: |
| 51 | # If first roll is a strike, no validation needed for second roll |
| 52 | if self.current_frame_rolls[0] == 10: |
| 53 | # Game continues for third roll |
| 54 | pass |
| 55 | # If first two rolls are not a strike or spare, game is over |
| 56 | elif sum(self.current_frame_rolls) < 10: |
| 57 | self.game_over = True |
| 58 | # If it's a spare, game continues for third roll |
| 59 | elif sum(self.current_frame_rolls) == 10: |
| 60 | pass |
| 61 | # If first two rolls exceed 10 (and first wasn't a strike), invalid |
| 62 | else: |
| 63 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 64 | elif len(self.current_frame_rolls) == 3: |
| 65 | # Validate third roll in 10th frame |
| 66 | if self.current_frame_rolls[0] == 10: |
| 67 | # After a strike, second and third rolls can be anything |
| 68 | pass |
| 69 | elif self.current_frame_rolls[0] + self.current_frame_rolls[1] == 10: |
| 70 | # After a spare, third roll can be anything |
| 71 | pass |
| 72 | # Game is definitely over after 3 rolls in 10th frame |
| 73 | self.game_over = True |
| 74 | |
| 8 | 75 | def score(self): |
| 9 | | pass |
| 76 | """ |
| 77 | Calculate the total score for the game. |
| 78 | |
| 79 | Returns: |
| 80 | int: Total score for the game |
| 81 | |
| 82 | Raises: |
| 83 | ValueError: If game is not yet complete |
| 84 | """ |
| 85 | # Edge Case: Game not complete |
| 86 | if not self.game_over: |
| 87 | raise ValueError("Game is not yet finished") |
| 88 | |
| 89 | total_score = 0 |
| 90 | roll_index = 0 |
| 91 | |
| 92 | # Calculate score for frames 1-9 |
| 93 | for frame in range(9): |
| 94 | # Strike |
| 95 | if self.rolls[roll_index] == 10: |
| 96 | # Add 10 plus next two rolls |
| 97 | total_score += 10 + self.rolls[roll_index + 1] + self.rolls[roll_index + 2] |
| 98 | roll_index += 1 |
| 99 | # Spare |
| 100 | elif self.rolls[roll_index] + self.rolls[roll_index + 1] == 10: |
| 101 | # Add 10 plus next roll |
| 102 | total_score += 10 + self.rolls[roll_index + 2] |
| 103 | roll_index += 2 |
| 104 | # Open frame |
| 105 | else: |
| 106 | total_score += self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 107 | roll_index += 2 |
| 108 | |
| 109 | # Handle 10th frame - just sum all remaining rolls |
| 110 | while roll_index < len(self.rolls): |
| 111 | total_score += self.rolls[roll_index] |
| 112 | roll_index += 1 |
| 113 | |
| 114 | return total_score |
| 115 | |
| 116 | # Handled Edge Cases: Invalid number of pins, game already completed, tenth frame special handling, invalid fill balls, frame total exceeds 10, game not complete |