| 1 | 1 | class BowlingGame: |
| 2 | 2 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 3 | """ |
| 4 | Initialize a new bowling game. |
| 5 | """ |
| 6 | self.rolls = [] |
| 7 | |
| 5 | 8 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 9 | """ |
| 10 | Record a roll in the game. |
| 11 | |
| 12 | Args: |
| 13 | pins (int): Number of pins knocked down in this roll |
| 14 | |
| 15 | Raises: |
| 16 | ValueError: If pins is negative or greater than 10 |
| 17 | IndexError: If game is already over |
| 18 | """ |
| 19 | # Edge Case: Invalid pin count (negative or more than 10) |
| 20 | if pins < 0 or pins > 10: |
| 21 | raise ValueError("Pins must be between 0 and 10") |
| 22 | |
| 23 | # Edge Case: Game already completed |
| 24 | if self.is_game_complete(): |
| 25 | raise IndexError("Game is already complete") |
| 26 | |
| 27 | # Edge Case: Too many pins in a frame (except for fill balls in 10th frame) |
| 28 | if not self.is_in_fill_balls(): |
| 29 | if len(self.rolls) % 2 == 1 and self.rolls[-1] != 10: # Second roll of frame, and first wasn't a strike |
| 30 | if self.rolls[-1] + pins > 10: |
| 31 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 32 | |
| 33 | self.rolls.append(pins) |
| 34 | |
| 35 | def is_in_fill_balls(self): |
| 36 | """Check if we're in the fill balls phase of the 10th frame""" |
| 37 | # Count how many frames we've completed |
| 38 | roll_index = 0 |
| 39 | frames_completed = 0 |
| 40 | |
| 41 | # Count frames 1-9 |
| 42 | while frames_completed < 9 and roll_index < len(self.rolls): |
| 43 | if self.rolls[roll_index] == 10: # Strike - one roll frame |
| 44 | roll_index += 1 |
| 45 | else: # Open frame or spare - two roll frame |
| 46 | if roll_index + 1 < len(self.rolls): |
| 47 | roll_index += 2 |
| 48 | else: |
| 49 | break # Incomplete frame |
| 50 | frames_completed += 1 |
| 51 | |
| 52 | # If we haven't completed 9 frames, we're not in 10th frame yet |
| 53 | if frames_completed < 9: |
| 54 | return False |
| 55 | |
| 56 | # We're in 10th frame, check if we need fill balls |
| 57 | tenth_frame_rolls = self.rolls[roll_index:] |
| 58 | |
| 59 | # If we have fewer than 2 rolls, we're not in fill balls yet |
| 60 | if len(tenth_frame_rolls) < 2: |
| 61 | return False |
| 62 | |
| 63 | # If first two rolls don't make a strike or spare, no fill balls needed |
| 64 | if tenth_frame_rolls[0] != 10 and sum(tenth_frame_rolls[:2]) < 10: |
| 65 | return False |
| 66 | |
| 67 | # If we have exactly 2 rolls and they make a strike or spare, we need fill balls |
| 68 | if len(tenth_frame_rolls) == 2: |
| 69 | if tenth_frame_rolls[0] == 10 or sum(tenth_frame_rolls[:2]) == 10: |
| 70 | return True |
| 71 | else: |
| 72 | return False |
| 73 | |
| 74 | # If we have 3 rolls, we're done with fill balls |
| 75 | if len(tenth_frame_rolls) >= 3: |
| 76 | return False |
| 77 | |
| 78 | return False |
| 79 | |
| 80 | def is_game_complete(self): |
| 81 | """Check if the game is complete""" |
| 82 | # Count how many frames we've completed |
| 83 | roll_index = 0 |
| 84 | frames_completed = 0 |
| 85 | |
| 86 | # Count frames 1-9 |
| 87 | while frames_completed < 9 and roll_index < len(self.rolls): |
| 88 | if self.rolls[roll_index] == 10: # Strike - one roll frame |
| 89 | roll_index += 1 |
| 90 | else: # Open frame or spare - two roll frame |
| 91 | if roll_index + 1 < len(self.rolls): |
| 92 | roll_index += 2 |
| 93 | else: |
| 94 | break # Incomplete frame |
| 95 | frames_completed += 1 |
| 96 | |
| 97 | # If we haven't completed 9 frames, game is not complete |
| 98 | if frames_completed < 9: |
| 99 | return False |
| 100 | |
| 101 | # Check 10th frame completion |
| 102 | tenth_frame_rolls = self.rolls[roll_index:] |
| 103 | |
| 104 | # If we have 3 rolls in 10th frame, we're done |
| 105 | if len(tenth_frame_rolls) == 3: |
| 106 | return True |
| 107 | |
| 108 | # If we have 2 rolls in 10th frame |
| 109 | if len(tenth_frame_rolls) == 2: |
| 110 | # If first roll was a strike, we need 1 more roll (fill ball) |
| 111 | if tenth_frame_rolls[0] == 10: |
| 112 | return False # Need 1 more fill ball |
| 113 | # If it's a spare, we need 1 more roll (fill ball) |
| 114 | elif sum(tenth_frame_rolls) == 10: |
| 115 | return False # Need 1 fill ball |
| 116 | else: |
| 117 | return True # Open frame, game complete |
| 118 | |
| 119 | # If we have 1 roll in 10th frame |
| 120 | if len(tenth_frame_rolls) == 1: |
| 121 | # If it's a strike, we need 2 more rolls (fill balls) |
| 122 | if tenth_frame_rolls[0] == 10: |
| 123 | return False # Need 2 fill balls |
| 124 | else: |
| 125 | return False # Need second roll |
| 126 | |
| 127 | return False |
| 128 | |
| 8 | 129 | def score(self): |
| 9 | | pass |
| 130 | """ |
| 131 | Calculate the total score for the game. |
| 132 | |
| 133 | Returns: |
| 134 | int: The total score for the game |
| 135 | |
| 136 | Raises: |
| 137 | IndexError: If the game is not yet complete |
| 138 | """ |
| 139 | # Edge Case: Game not complete |
| 140 | if not self.is_game_complete(): |
| 141 | raise IndexError("Game is not yet complete") |
| 142 | |
| 143 | total_score = 0 |
| 144 | roll_index = 0 |
| 145 | |
| 146 | # Score frames 1-10 |
| 147 | for frame in range(10): |
| 148 | if roll_index >= len(self.rolls): |
| 149 | break |
| 150 | |
| 151 | # Check for strike (first roll of frame is 10) |
| 152 | if self.rolls[roll_index] == 10: |
| 153 | # Strike: 10 + next two rolls |
| 154 | frame_score = 10 |
| 155 | if roll_index + 1 < len(self.rolls): |
| 156 | frame_score += self.rolls[roll_index + 1] |
| 157 | if roll_index + 2 < len(self.rolls): |
| 158 | frame_score += self.rolls[roll_index + 2] |
| 159 | roll_index += 1 |
| 160 | else: |
| 161 | # Open frame or spare |
| 162 | frame_score = self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 163 | # Check for spare |
| 164 | if frame_score == 10: |
| 165 | # Spare: 10 + next roll |
| 166 | if roll_index + 2 < len(self.rolls): |
| 167 | frame_score += self.rolls[roll_index + 2] |
| 168 | roll_index += 2 |
| 169 | |
| 170 | total_score += frame_score |
| 171 | |
| 172 | return total_score |