| 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 | ValueError: If trying to roll after the game is complete |
| 21 | """ |
| 22 | # Edge Case: Invalid pin count (negative or more than 10) |
| 23 | if pins < 0 or pins > 10: |
| 24 | raise ValueError("Pins must be between 0 and 10") |
| 25 | |
| 26 | # Edge Case: Game already completed |
| 27 | # Game is complete when we have 10 completed frames and no rolls in progress |
| 28 | if len(self.frames) == 10 and len(self.frame_rolls) == 0: |
| 29 | raise IndexError("Game is already complete") |
| 30 | |
| 31 | # Edge Case: Invalid roll in tenth frame - check if second ball exceeds 10 pins in regular frames |
| 32 | # But for the 10th frame, fill balls can each be 0-10 |
| 33 | if len(self.frames) < 10 and len(self.frame_rolls) == 1 and self.frame_rolls[0] != 10: |
| 34 | if self.frame_rolls[0] + pins > 10: |
| 35 | raise ValueError("Cannot knock down more pins than remaining") |
| 36 | |
| 37 | # Edge Case: Too many pins in a frame (except tenth frame) |
| 38 | if len(self.frames) < 10 and len(self.frame_rolls) == 1 and self.frame_rolls[0] != 10: |
| 39 | if self.frame_rolls[0] + pins > 10: |
| 40 | raise ValueError("Cannot knock down more pins than remaining") |
| 41 | |
| 42 | self.frame_rolls.append(pins) |
| 43 | self.rolls.append(pins) |
| 44 | |
| 45 | # Check if frame is complete |
| 46 | # If we've completed 9 frames, we're working on the 10th frame |
| 47 | if len(self.frames) == 9: # Tenth frame |
| 48 | # Handle tenth frame completion |
| 49 | # Frame is complete if: |
| 50 | # 1. We have 3 rolls (strike followed by two rolls, or spare with one fill ball) |
| 51 | # 2. We have 2 rolls and it's not a strike or spare (open frame) |
| 52 | tenth_frame_complete = False |
| 53 | if len(self.frame_rolls) == 3: |
| 54 | # We have 3 rolls - this is always complete for 10th frame |
| 55 | tenth_frame_complete = True |
| 56 | elif len(self.frame_rolls) == 2: |
| 57 | # We have 2 rolls - check if it's an open frame (not strike or spare) |
| 58 | if self.frame_rolls[0] != 10 and (self.frame_rolls[0] + self.frame_rolls[1]) != 10: |
| 59 | # Open frame - complete with just 2 rolls |
| 60 | tenth_frame_complete = True |
| 61 | # If it's a strike or spare, we need more rolls (fill balls) - don't complete yet |
| 62 | |
| 63 | if tenth_frame_complete: |
| 64 | self.frames.append(list(self.frame_rolls)) |
| 65 | self.frame_rolls = [] |
| 66 | elif len(self.frames) < 9: # Regular frames (1-9) |
| 67 | # Strike |
| 68 | if len(self.frame_rolls) == 1 and pins == 10: |
| 69 | self.frames.append(list(self.frame_rolls)) |
| 70 | self.frame_rolls = [] |
| 71 | self.current_frame += 1 |
| 72 | # Regular frame complete |
| 73 | elif len(self.frame_rolls) == 2: |
| 74 | self.frames.append(list(self.frame_rolls)) |
| 75 | self.frame_rolls = [] |
| 76 | self.current_frame += 1 |
| 77 | |
| 78 | def _needs_fill_balls(self): |
| 79 | """ |
| 80 | Determine if the tenth frame needs fill balls. |
| 81 | |
| 82 | Returns: |
| 83 | bool: True if fill balls are needed, False otherwise |
| 84 | """ |
| 85 | # If we haven't completed 9 frames yet, we're not in the tenth frame |
| 86 | if len(self.frames) < 9: |
| 87 | return False |
| 88 | |
| 89 | # If we've completed 10 frames, the game is over |
| 90 | if len(self.frames) == 10: |
| 91 | return False |
| 92 | |
| 93 | # We're in the process of completing the tenth frame |
| 94 | # len(self.frames) == 9 means we've completed 9 frames and are working on the 10th |
| 95 | |
| 96 | if len(self.frame_rolls) == 0: |
| 97 | return False # Haven't started the tenth frame rolls yet |
| 98 | |
| 99 | # If we haven't rolled enough balls in the tenth frame |
| 100 | if len(self.frame_rolls) == 1: |
| 101 | # If first roll is a strike, we need two more rolls (fill balls) |
| 102 | if self.frame_rolls[0] == 10: |
| 103 | return True |
| 104 | else: |
| 105 | return False # Need a second roll to complete the frame |
| 106 | |
| 107 | if len(self.frame_rolls) == 2: |
| 108 | # If first roll is strike or second completes a spare, need one more fill ball |
| 109 | if self.frame_rolls[0] == 10 or (self.frame_rolls[0] + self.frame_rolls[1]) == 10: |
| 110 | return True |
| 111 | else: |
| 112 | return False # Open frame is complete |
| 113 | |
| 114 | return False # Third roll means we're done |
| 115 | |
| 8 | 116 | def score(self): |
| 9 | | pass |
| 117 | """ |
| 118 | Calculate the total score for the game. |
| 119 | |
| 120 | Returns: |
| 121 | int: The total score for the game |
| 122 | |
| 123 | Raises: |
| 124 | IndexError: If the game is not yet complete |
| 125 | """ |
| 126 | # Edge Case: Game not complete |
| 127 | if len(self.frames) < 10: |
| 128 | raise IndexError("Game is not complete") |
| 129 | |
| 130 | # Additional validation: Check if we have enough rolls to calculate all scores |
| 131 | # For frames 1-9, we might need up to 2 additional rolls for strike/spare bonuses |
| 132 | # So we need at least as many rolls as we've recorded |
| 133 | if len(self.rolls) < len([roll for frame in self.frames for roll in frame]): |
| 134 | raise IndexError("Game is not complete") |
| 135 | |
| 136 | total_score = 0 |
| 137 | roll_index = 0 |
| 138 | |
| 139 | for frame_num in range(10): |
| 140 | # Edge Case: Incomplete game |
| 141 | if roll_index >= len(self.rolls): |
| 142 | raise IndexError("Game is not complete") |
| 143 | |
| 144 | if frame_num < 9: # Frames 1-9 |
| 145 | # Validate we have enough rolls for this frame and potential bonuses |
| 146 | if roll_index >= len(self.rolls): |
| 147 | raise IndexError("Game is not complete") |
| 148 | |
| 149 | # Strike - need current roll and next two rolls |
| 150 | if self.rolls[roll_index] == 10: |
| 151 | if roll_index + 2 >= len(self.rolls): |
| 152 | raise IndexError("Game is not complete") |
| 153 | total_score += 10 + self.rolls[roll_index + 1] + self.rolls[roll_index + 2] |
| 154 | roll_index += 1 |
| 155 | # Spare - need current two rolls and next roll |
| 156 | elif self.rolls[roll_index] + self.rolls[roll_index + 1] == 10: |
| 157 | if roll_index + 2 >= len(self.rolls): |
| 158 | raise IndexError("Game is not complete") |
| 159 | total_score += 10 + self.rolls[roll_index + 2] |
| 160 | roll_index += 2 |
| 161 | # Open frame - need current two rolls |
| 162 | else: |
| 163 | if roll_index + 1 >= len(self.rolls): |
| 164 | raise IndexError("Game is not complete") |
| 165 | total_score += self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 166 | roll_index += 2 |
| 167 | else: # Tenth frame |
| 168 | # Sum all the rolls in the tenth frame |
| 169 | frame_rolls = self.frames[9] |
| 170 | for roll in frame_rolls: |
| 171 | total_score += roll |
| 172 | |
| 173 | return total_score |
| 174 | |
| 175 | # Handled Edge Cases: Invalid pin count, game already completed, invalid roll in tenth frame, too many pins in a frame, game not complete |