| 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 | |
| 11 | def _is_game_complete(self): |
| 12 | """Check if the game is complete.""" |
| 13 | # Game is complete when we have 10 frames and no rolls in progress |
| 14 | if len(self.frames) < 10: |
| 15 | return False |
| 16 | |
| 17 | # If we still have rolls in progress, game is not complete |
| 18 | if len(self.frame_rolls) > 0: |
| 19 | return False |
| 20 | |
| 21 | # For the 10th frame, check if it has the right number of rolls |
| 22 | if len(self.frames) == 10: |
| 23 | tenth_frame = self.frames[9] |
| 24 | # If it's a strike (10 pins on first roll), need 3 rolls total |
| 25 | if len(tenth_frame) >= 1 and tenth_frame[0] == 10: |
| 26 | return len(tenth_frame) == 3 |
| 27 | # If it's a spare (10 pins total in frame), need 3 rolls total |
| 28 | elif len(tenth_frame) >= 2 and sum(tenth_frame[:2]) == 10: |
| 29 | return len(tenth_frame) == 3 |
| 30 | # If it's an open frame, need 2 rolls |
| 31 | elif len(tenth_frame) == 2 and sum(tenth_frame) < 10: |
| 32 | return True |
| 33 | |
| 34 | return False |
| 35 | |
| 5 | 36 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 37 | """ |
| 38 | Record a roll in the game. |
| 39 | |
| 40 | Args: |
| 41 | pins (int): Number of pins knocked down in this roll |
| 42 | |
| 43 | Raises: |
| 44 | ValueError: If pins is negative or greater than 10 |
| 45 | ValueError: If game is already over |
| 46 | """ |
| 47 | # Edge Case: Invalid number of pins |
| 48 | if pins < 0 or pins > 10: |
| 49 | raise ValueError("Pins must be between 0 and 10") |
| 50 | |
| 51 | # Edge Case: Game already completed |
| 52 | if self._is_game_complete(): |
| 53 | raise IndexError("Game is already complete") |
| 54 | |
| 55 | self.frame_rolls.append(pins) |
| 56 | self.rolls.append(pins) |
| 57 | |
| 58 | # Handle regular frames (1-9) |
| 59 | if len(self.frames) < 9: |
| 60 | # Strike case |
| 61 | if pins == 10: |
| 62 | self.frames.append(self.frame_rolls[:]) |
| 63 | self.frame_rolls = [] |
| 64 | self.current_frame += 1 |
| 65 | # Two rolls completed |
| 66 | elif len(self.frame_rolls) == 2: |
| 67 | # Edge Case: Frame total exceeds 10 (invalid) |
| 68 | if sum(self.frame_rolls) > 10: |
| 69 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 70 | self.frames.append(self.frame_rolls[:]) |
| 71 | self.frame_rolls = [] |
| 72 | self.current_frame += 1 |
| 73 | # Handle 10th frame (frames 10 and potential fill balls) |
| 74 | else: |
| 75 | # Edge Case: Invalid rolls in 10th frame |
| 76 | if len(self.frame_rolls) == 2 and self.frame_rolls[0] != 10 and sum(self.frame_rolls) > 10: |
| 77 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 78 | |
| 79 | # Determine if 10th frame is complete |
| 80 | if len(self.frame_rolls) == 3: |
| 81 | self.frames.append(self.frame_rolls[:]) |
| 82 | self.frame_rolls = [] |
| 83 | elif len(self.frame_rolls) == 2: |
| 84 | # If not a strike or spare, frame is complete |
| 85 | if self.frame_rolls[0] != 10 and sum(self.frame_rolls) < 10: |
| 86 | self.frames.append(self.frame_rolls[:]) |
| 87 | self.frame_rolls = [] |
| 88 | # If it's a strike or spare, need one more roll |
| 89 | elif len(self.frame_rolls) == 1 and self.frame_rolls[0] < 10: |
| 90 | # First roll was not a strike, need one more |
| 91 | pass |
| 92 | |
| 8 | 93 | def score(self): |
| 9 | | pass |
| 94 | """ |
| 95 | Calculate the total score for the game. |
| 96 | |
| 97 | Returns: |
| 98 | int: Total score for the game |
| 99 | |
| 100 | Raises: |
| 101 | IndexError: If the game is not yet complete |
| 102 | """ |
| 103 | # For a new game with no frames, score is 0 |
| 104 | if len(self.frames) == 0: |
| 105 | return 0 |
| 106 | |
| 107 | # Edge Case: Game not yet complete |
| 108 | if not self._is_game_complete(): |
| 109 | raise IndexError("Game is not yet complete") |
| 110 | |
| 111 | total = 0 |
| 112 | roll_index = 0 |
| 113 | |
| 114 | # Score frames 1-9 |
| 115 | for frame_num in range(9): |
| 116 | # Edge Case: Incomplete frames |
| 117 | if frame_num >= len(self.frames): |
| 118 | raise ValueError("Score cannot be taken until the end of the game") |
| 119 | |
| 120 | frame = self.frames[frame_num] |
| 121 | |
| 122 | if len(frame) == 1 and frame[0] == 10: # Strike |
| 123 | total += 10 |
| 124 | # Add next two rolls |
| 125 | if frame_num + 1 < len(self.frames): |
| 126 | next_frame = self.frames[frame_num + 1] |
| 127 | if len(next_frame) >= 1: |
| 128 | total += next_frame[0] |
| 129 | if len(next_frame) >= 2: |
| 130 | total += next_frame[1] |
| 131 | elif len(next_frame) == 1 and frame_num + 2 < len(self.frames): |
| 132 | # If next frame is also a strike |
| 133 | next_next_frame = self.frames[frame_num + 2] |
| 134 | if len(next_next_frame) >= 1: |
| 135 | total += next_next_frame[0] |
| 136 | elif len(frame) == 2 and sum(frame) == 10: # Spare |
| 137 | total += 10 |
| 138 | # Add next roll |
| 139 | if frame_num + 1 < len(self.frames): |
| 140 | next_frame = self.frames[frame_num + 1] |
| 141 | if len(next_frame) >= 1: |
| 142 | total += next_frame[0] |
| 143 | else: # Open frame |
| 144 | total += sum(frame) |
| 145 | |
| 146 | # Score 10th frame |
| 147 | if len(self.frames) > 9: |
| 148 | frame10 = self.frames[9] |
| 149 | total += sum(frame10) |
| 150 | |
| 151 | return total |
| 152 | # Handled Edge Cases: Game not yet complete, Incomplete frames |