| 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 = 1 |
| 8 | self.frame_rolls = [] |
| 9 | self.frames = [] |
| 10 | self.game_over = False |
| 11 | |
| 5 | 12 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 13 | """ |
| 14 | Record a roll in the game. |
| 15 | |
| 16 | Args: |
| 17 | pins (int): Number of pins knocked down in this roll |
| 18 | |
| 19 | Raises: |
| 20 | ValueError: If the number of pins is invalid or game is already over |
| 21 | """ |
| 22 | # Edge Case: Game is already over |
| 23 | if self.game_over: |
| 24 | raise IndexError("Game is already complete") |
| 25 | |
| 26 | # Edge Case: Invalid number of pins (negative or more than 10) |
| 27 | if pins < 0 or pins > 10: |
| 28 | raise ValueError("Pins must be between 0 and 10") |
| 29 | |
| 30 | # Edge Case: Too many pins in a frame (except for tenth frame special cases) |
| 31 | if len(self.frame_rolls) == 1: |
| 32 | # For tenth frame, we allow up to 10 pins on second roll regardless of first roll |
| 33 | if self.current_frame == 10: |
| 34 | # If first roll was a strike, second roll can be up to 10 |
| 35 | if self.frame_rolls[0] == 10: |
| 36 | pass # Allow any number of pins (0-10) on second roll |
| 37 | # If first two rolls sum to 10, it's a spare, third roll is allowed |
| 38 | elif self.frame_rolls[0] + pins > 10: |
| 39 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 40 | # For other frames, sum of two rolls cannot exceed 10 |
| 41 | elif self.frame_rolls[0] + pins > 10: |
| 42 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 43 | |
| 44 | self.frame_rolls.append(pins) |
| 45 | self.rolls.append(pins) |
| 46 | |
| 47 | # Handle frame completion |
| 48 | if self.current_frame < 10: |
| 49 | # Regular frames |
| 50 | if pins == 10 or len(self.frame_rolls) == 2: |
| 51 | self.frames.append(self.frame_rolls[:]) |
| 52 | self.frame_rolls = [] |
| 53 | self.current_frame += 1 |
| 54 | else: |
| 55 | # Tenth frame |
| 56 | # Strike case: need 2 more rolls |
| 57 | if len(self.frames) == 9 and (len(self.frame_rolls) == 3 or |
| 58 | (len(self.frame_rolls) == 2 and self.frame_rolls[0] != 10 and sum(self.frame_rolls) < 10)): |
| 59 | self.frames.append(self.frame_rolls[:]) |
| 60 | self.frame_rolls = [] |
| 61 | self.game_over = True |
| 62 | # Non-strike case: if we have 2 rolls and it's not a spare, game over |
| 63 | elif len(self.frame_rolls) == 2 and sum(self.frame_rolls) < 10: |
| 64 | self.frames.append(self.frame_rolls[:]) |
| 65 | self.frame_rolls = [] |
| 66 | self.game_over = True |
| 67 | # Spare case: need 1 more roll |
| 68 | elif len(self.frame_rolls) == 3: |
| 69 | self.frames.append(self.frame_rolls[:]) |
| 70 | self.frame_rolls = [] |
| 71 | self.game_over = True |
| 72 | |
| 8 | 73 | def score(self): |
| 9 | | pass |
| 74 | """ |
| 75 | Calculate the total score for the game. |
| 76 | |
| 77 | Returns: |
| 78 | int: The total score of the game |
| 79 | |
| 80 | Raises: |
| 81 | ValueError: If the game is not yet complete |
| 82 | """ |
| 83 | # Edge Case: Game is not complete |
| 84 | if not self.game_over or len(self.frames) < 10: |
| 85 | raise IndexError("Game is not yet complete") |
| 86 | |
| 87 | total_score = 0 |
| 88 | roll_index = 0 |
| 89 | |
| 90 | # Score first 9 frames |
| 91 | for frame_index in range(9): |
| 92 | frame = self.frames[frame_index] |
| 93 | |
| 94 | if len(frame) == 1 and frame[0] == 10: # Strike |
| 95 | total_score += 10 + self._next_two_balls(frame_index) |
| 96 | elif sum(frame) == 10: # Spare |
| 97 | total_score += 10 + self._next_ball(frame_index) |
| 98 | else: # Open frame |
| 99 | total_score += sum(frame) |
| 100 | |
| 101 | # Score 10th frame |
| 102 | tenth_frame = self.frames[9] |
| 103 | total_score += sum(tenth_frame) |
| 104 | |
| 105 | return total_score |
| 106 | |
| 107 | def _next_ball(self, frame_index): |
| 108 | """ |
| 109 | Get the number of pins from the first roll of the next frame. |
| 110 | |
| 111 | Args: |
| 112 | frame_index (int): Index of the current frame |
| 113 | |
| 114 | Returns: |
| 115 | int: Number of pins from the next ball |
| 116 | """ |
| 117 | if frame_index + 1 < len(self.frames): |
| 118 | next_frame = self.frames[frame_index + 1] |
| 119 | if len(next_frame) > 0: |
| 120 | return next_frame[0] |
| 121 | return 0 |
| 122 | |
| 123 | def _next_two_balls(self, frame_index): |
| 124 | """ |
| 125 | Get the number of pins from the next two rolls. |
| 126 | |
| 127 | Args: |
| 128 | frame_index (int): Index of the current frame |
| 129 | |
| 130 | Returns: |
| 131 | int: Sum of pins from the next two balls |
| 132 | """ |
| 133 | pins = 0 |
| 134 | |
| 135 | if frame_index + 1 < len(self.frames): |
| 136 | next_frame = self.frames[frame_index + 1] |
| 137 | |
| 138 | if len(next_frame) >= 2: |
| 139 | pins += next_frame[0] + next_frame[1] |
| 140 | elif len(next_frame) == 1: |
| 141 | pins += next_frame[0] |
| 142 | if frame_index + 2 < len(self.frames) and len(self.frames[frame_index + 2]) > 0: |
| 143 | pins += self.frames[frame_index + 2][0] |
| 144 | |
| 145 | return pins |
| 146 | |
| 147 | # Handled Edge Cases: Game is already over, invalid number of pins, too many pins in a frame, game is not complete |