| 1 | 1 | class BowlingGame: |
| 2 | 2 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 3 | """ |
| 4 | Initialize a new bowling game. |
| 5 | |
| 6 | Sets up the game state including frames, current frame, and roll tracking. |
| 7 | """ |
| 8 | self.frames = [[] for _ in range(10)] # 10 frames |
| 9 | self.current_frame = 0 |
| 10 | self.rolls = [] |
| 11 | self.game_over = False |
| 12 | |
| 5 | 13 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 14 | """ |
| 15 | Record a roll in the bowling game. |
| 16 | |
| 17 | Args: |
| 18 | pins (int): Number of pins knocked down in this roll |
| 19 | |
| 20 | Raises: |
| 21 | ValueError: If pins is negative or greater than 10 |
| 22 | IndexError: If attempting to roll after the game is over |
| 23 | """ |
| 24 | # Edge Case: Invalid pin count (negative or more than 10) |
| 25 | if pins < 0 or pins > 10: |
| 26 | raise ValueError("invalid pin count") |
| 27 | |
| 28 | # Edge Case: Attempting to roll after game is over |
| 29 | if self.game_over: |
| 30 | raise IndexError("Cannot roll after game is over") |
| 31 | |
| 32 | # Handle rolls based on current game state |
| 33 | if self.current_frame < 9: # Frames 0-8 (normal frames) |
| 34 | # Validate second roll in frame |
| 35 | if len(self.frames[self.current_frame]) == 1: |
| 36 | if self.frames[self.current_frame][0] != 10 and self.frames[self.current_frame][0] + pins > 10: |
| 37 | raise ValueError("invalid pin count") |
| 38 | |
| 39 | self.rolls.append(pins) |
| 40 | self.frames[self.current_frame].append(pins) |
| 41 | |
| 42 | # Move to next frame if current frame is complete |
| 43 | if len(self.frames[self.current_frame]) == 2 or pins == 10: # Strike or second roll |
| 44 | self.current_frame += 1 |
| 45 | |
| 46 | elif self.current_frame == 9: # 10th frame (special rules) |
| 47 | # Validate rolls in 10th frame |
| 48 | if len(self.frames[self.current_frame]) == 1: |
| 49 | # Second roll validation |
| 50 | if self.frames[self.current_frame][0] != 10 and self.frames[self.current_frame][0] + pins > 10: |
| 51 | raise ValueError("invalid pin count") |
| 52 | elif len(self.frames[self.current_frame]) == 2: |
| 53 | # Third roll validation |
| 54 | first_roll = self.frames[self.current_frame][0] |
| 55 | second_roll = self.frames[self.current_frame][1] |
| 56 | # If first two rolls are strikes, third can be 0-10 |
| 57 | # If first is strike and second is not, they must sum <= 10 |
| 58 | # If first is not strike, and first+second=10 (spare), third can be 0-10 |
| 59 | # If first+second<10 (open frame), there shouldn't be a third roll |
| 60 | if first_roll == 10 and second_roll != 10 and second_roll + pins > 10: |
| 61 | raise ValueError("invalid fill balls") |
| 62 | elif first_roll != 10 and first_roll + second_roll == 10: |
| 63 | # Spare - third roll is valid (0-10) |
| 64 | pass |
| 65 | elif first_roll != 10 and first_roll + second_roll < 10: |
| 66 | # This shouldn't happen - trying to add third roll to open frame |
| 67 | raise ValueError("invalid fill balls") |
| 68 | |
| 69 | self.rolls.append(pins) |
| 70 | self.frames[self.current_frame].append(pins) |
| 71 | |
| 72 | # Check if 10th frame is complete |
| 73 | frame_rolls = self.frames[self.current_frame] |
| 74 | if len(frame_rolls) == 2: |
| 75 | # Two rolls in 10th frame |
| 76 | if frame_rolls[0] != 10 and sum(frame_rolls) < 10: |
| 77 | # Open frame in 10th - game over |
| 78 | self.game_over = True |
| 79 | elif len(frame_rolls) == 3: |
| 80 | # Three rolls in 10th frame - game over |
| 81 | self.game_over = True |
| 82 | |
| 83 | else: # Fill ball phase (current_frame > 9) |
| 84 | # This should not happen with correct logic |
| 85 | # All fill balls should be handled in the 10th frame |
| 86 | raise ValueError("Game state error") |
| 87 | |
| 8 | 88 | def score(self): |
| 9 | | pass |
| 89 | """ |
| 90 | Calculate the total score for the bowling game. |
| 91 | |
| 92 | Returns: |
| 93 | int: The total score of the game |
| 94 | |
| 95 | Raises: |
| 96 | IndexError: If the game is not yet complete |
| 97 | """ |
| 98 | # Edge Case: New game with no rolls should score as 0 |
| 99 | if len(self.rolls) == 0: |
| 100 | return 0 |
| 101 | |
| 102 | # Edge Case: Attempting to score before game is over |
| 103 | if not self.game_over: |
| 104 | raise IndexError("Score cannot be taken until the end of the game") |
| 105 | |
| 106 | total_score = 0 |
| 107 | roll_index = 0 |
| 108 | |
| 109 | for frame in range(10): |
| 110 | # Edge Case: Incomplete frames |
| 111 | if roll_index >= len(self.rolls): |
| 112 | raise IndexError("Score cannot be taken until the end of the game") |
| 113 | |
| 114 | if self.rolls[roll_index] == 10: # Strike |
| 115 | # Edge Case: Not enough rolls for strike calculation |
| 116 | if roll_index + 2 >= len(self.rolls): |
| 117 | raise IndexError("Score cannot be taken until the end of the game") |
| 118 | total_score += 10 + self.rolls[roll_index + 1] + self.rolls[roll_index + 2] |
| 119 | roll_index += 1 |
| 120 | elif roll_index + 1 < len(self.rolls) and self.rolls[roll_index] + self.rolls[roll_index + 1] == 10: # Spare |
| 121 | # Edge Case: Not enough rolls for spare calculation |
| 122 | if roll_index + 2 >= len(self.rolls): |
| 123 | raise IndexError("Score cannot be taken until the end of the game") |
| 124 | total_score += 10 + self.rolls[roll_index + 2] |
| 125 | roll_index += 2 |
| 126 | else: # Open frame |
| 127 | # Edge Case: Not enough rolls for open frame calculation |
| 128 | if roll_index + 1 >= len(self.rolls): |
| 129 | raise IndexError("Score cannot be taken until the end of the game") |
| 130 | total_score += self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 131 | roll_index += 2 |
| 132 | |
| 133 | return total_score |
| 134 | # Handled Edge Cases: Invalid pin count, rolling after game over, invalid rolls in frames, |
| 135 | # game completion tracking, fill balls validation, scoring before game end, |
| 136 | # incomplete frames during scoring, insufficient rolls for strike/spare calculations |