| 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 | |
| 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 | IndexError: If game is already over |
| 21 | """ |
| 22 | # Edge Case: Invalid number of pins |
| 23 | if pins < 0: |
| 24 | raise ValueError("Pins must be between 0 and 10") |
| 25 | |
| 26 | # Edge Case: Too many pins |
| 27 | if pins > 10: |
| 28 | raise ValueError("Pins must be between 0 and 10") |
| 29 | |
| 30 | # Edge Case: Game already finished |
| 31 | if len(self.frames) == 10 and len(self.frame_rolls) == 0: |
| 32 | raise IndexError("Game is already complete") |
| 33 | |
| 34 | # Edge Case: Game already finished |
| 35 | if len(self.frames) == 10 and len(self.frame_rolls) == 0: |
| 36 | raise IndexError("Game is already complete") |
| 37 | |
| 38 | self.frame_rolls.append(pins) |
| 39 | self.rolls.append(pins) |
| 40 | |
| 41 | # Debug output |
| 42 | # print(f"After append: frame_rolls={self.frame_rolls}") |
| 43 | |
| 44 | # Handle regular frames (1-9) |
| 45 | if self.current_frame < 10: |
| 46 | # Strike case |
| 47 | if pins == 10: |
| 48 | self.frames.append(self.frame_rolls[:]) |
| 49 | self.frame_rolls = [] |
| 50 | self.current_frame += 1 |
| 51 | # Two rolls completed |
| 52 | elif len(self.frame_rolls) == 2: |
| 53 | # Edge Case: Frame total exceeds 10 |
| 54 | if sum(self.frame_rolls) > 10: |
| 55 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 56 | self.frames.append(self.frame_rolls[:]) |
| 57 | self.frame_rolls = [] |
| 58 | self.current_frame += 1 |
| 59 | # Handle 10th frame |
| 60 | else: |
| 61 | # Edge Case: Invalid rolls in 10th frame |
| 62 | if len(self.frame_rolls) == 1: |
| 63 | if self.frame_rolls[0] < 10: |
| 64 | # This is the second roll of the 10th frame (not a fill ball) |
| 65 | # Validate that total doesn't exceed 10 |
| 66 | if pins > 10 - self.frame_rolls[0]: |
| 67 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 68 | # No validation needed for third roll in 10th frame (fill ball) |
| 69 | # No validation needed for fill balls - they can be 0-10 regardless of previous rolls |
| 70 | elif len(self.frame_rolls) == 2: |
| 71 | # After two rolls in 10th frame |
| 72 | if self.frame_rolls[0] == 10: |
| 73 | # First was strike, second roll is first fill ball |
| 74 | # No validation needed for fill balls (can be 0-10) |
| 75 | pass |
| 76 | elif sum(self.frame_rolls[:2]) == 10: |
| 77 | # First two rolls make a spare |
| 78 | # Third roll is fill ball, no validation needed |
| 79 | pass |
| 80 | # For open frame or incomplete spare/strike, no validation needed for fill balls |
| 81 | # Fill balls can be 0-10 regardless of previous rolls in 10th frame |
| 82 | |
| 83 | # Determine if 10th frame is complete |
| 84 | if len(self.frame_rolls) == 3: |
| 85 | # Frame is complete after 3 rolls |
| 86 | self.frames.append(self.frame_rolls[:]) |
| 87 | self.frame_rolls = [] |
| 88 | elif len(self.frame_rolls) == 2: |
| 89 | # Check if frame is complete after 2 rolls |
| 90 | if self.frame_rolls[0] < 10 and sum(self.frame_rolls) < 10: |
| 91 | # Open frame - complete |
| 92 | self.frames.append(self.frame_rolls[:]) |
| 93 | self.frame_rolls = [] |
| 94 | # If strike or spare, need third roll - frame not complete yet |
| 95 | |
| 96 | # Edge Case: Game already finished |
| 97 | # Check if game is complete (only after 10th frame is fully processed) |
| 98 | # This check will be done at the beginning of the next roll call |
| 99 | |
| 100 | # Handled Edge Cases: Invalid number of pins, too many pins, game already finished, frame total exceeds 10, invalid rolls in 10th frame |
| 101 | |
| 8 | 102 | def score(self): |
| 9 | | pass |
| 103 | """ |
| 104 | Calculate the total score for the game. |
| 105 | |
| 106 | Returns: |
| 107 | int: Total score for the game |
| 108 | |
| 109 | Raises: |
| 110 | IndexError: If game is not yet complete |
| 111 | """ |
| 112 | # Edge Case: Game not complete |
| 113 | if len(self.frames) < 10: |
| 114 | # Special case: no frames completed yet (initial score) |
| 115 | if len(self.frames) == 0 and len(self.rolls) == 0: |
| 116 | return 0 |
| 117 | raise IndexError("Game is not yet complete") |
| 118 | |
| 119 | # Edge Case: 10th frame incomplete |
| 120 | if len(self.frames) == 10 and len(self.frames[9]) < 2: |
| 121 | raise IndexError("Score cannot be taken until the end of the game") |
| 122 | |
| 123 | # Edge Case: 10th frame is a strike or spare but missing fill balls |
| 124 | if len(self.frames) == 10 and len(self.frames[9]) == 2: |
| 125 | frame10 = self.frames[9] |
| 126 | if frame10[0] == 10 or sum(frame10) == 10: |
| 127 | raise IndexError("Score cannot be taken until the end of the game") |
| 128 | |
| 129 | total_score = 0 |
| 130 | |
| 131 | # Score frames 1-9 |
| 132 | for i in range(9): |
| 133 | frame = self.frames[i] |
| 134 | frame_score = sum(frame) |
| 135 | |
| 136 | # Strike |
| 137 | if len(frame) == 1: |
| 138 | # Add next one or two rolls |
| 139 | if i+1 < len(self.frames): |
| 140 | next_frame = self.frames[i+1] |
| 141 | if len(next_frame) >= 2: |
| 142 | frame_score += next_frame[0] + next_frame[1] |
| 143 | elif len(next_frame) == 1 and i+2 < len(self.frames): |
| 144 | # Next frame is also a strike |
| 145 | frame_score += next_frame[0] + self.frames[i+2][0] |
| 146 | # Spare |
| 147 | elif len(frame) == 2 and sum(frame) == 10: |
| 148 | # Add next one roll |
| 149 | if i+1 < len(self.frames): |
| 150 | frame_score += self.frames[i+1][0] |
| 151 | |
| 152 | total_score += frame_score |
| 153 | |
| 154 | # Score 10th frame |
| 155 | frame10 = self.frames[9] |
| 156 | total_score += sum(frame10) |
| 157 | |
| 158 | return total_score |
| 159 | |
| 160 | # Handled Edge Cases: Game not complete, 10th frame incomplete, 10th frame is a strike or spare but missing fill balls |