| 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_roll = 0 |
| 8 | |
| 5 | 9 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 10 | """ |
| 11 | Record a roll in the game. |
| 12 | |
| 13 | Args: |
| 14 | pins (int): Number of pins knocked down in this roll. |
| 15 | |
| 16 | Raises: |
| 17 | ValueError: If the number of pins is invalid. |
| 18 | """ |
| 19 | # Edge Case: Invalid number of pins (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 is already over (10 frames completed) |
| 24 | if self.is_game_over(): |
| 25 | raise ValueError("Cannot roll after game is over") |
| 26 | |
| 27 | # Edge Case: Too many pins in a frame (not a strike) |
| 28 | if len(self.rolls) % 2 == 1 and not self.is_strike(len(self.rolls) - 1): # Second roll of a frame |
| 29 | if self.rolls[-1] + pins > 10: |
| 30 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 31 | |
| 32 | self.rolls.append(pins) |
| 33 | self.current_roll += 1 |
| 34 | |
| 8 | 35 | def score(self): |
| 9 | | pass |
| 36 | """ |
| 37 | Calculate the total score for the game. |
| 38 | |
| 39 | Returns: |
| 40 | int: The total score of the game. |
| 41 | |
| 42 | Raises: |
| 43 | ValueError: If the game is not yet complete. |
| 44 | """ |
| 45 | # Edge Case: Game is not complete yet |
| 46 | if not self.is_game_over(): |
| 47 | raise IndexError("Game is not yet complete") |
| 48 | |
| 49 | score = 0 |
| 50 | roll_index = 0 |
| 51 | |
| 52 | for frame in range(10): |
| 53 | # Edge Case: Strike in regular frames (1-9) |
| 54 | if self.is_strike(roll_index) and frame < 9: |
| 55 | score += 10 + self.strike_bonus(roll_index) |
| 56 | roll_index += 1 |
| 57 | # Edge Case: Spare in regular frames (1-9) |
| 58 | elif self.is_spare(roll_index) and frame < 9: |
| 59 | score += 10 + self.spare_bonus(roll_index) |
| 60 | roll_index += 2 |
| 61 | # Edge Case: Open frame in regular frames (1-9) |
| 62 | elif frame < 9: |
| 63 | score += self.sum_of_balls_in_frame(roll_index) |
| 64 | roll_index += 2 |
| 65 | # Edge Case: Tenth frame handling |
| 66 | else: # frame == 9 (10th frame) |
| 67 | # Edge Case: Strike in 10th frame |
| 68 | if self.is_strike(roll_index): |
| 69 | # Edge Case: Invalid number of rolls after strike in 10th frame |
| 70 | if len(self.rolls) < roll_index + 3: |
| 71 | raise ValueError("Score cannot be taken until the end of the game") |
| 72 | score += 10 + self.rolls[roll_index + 1] + self.rolls[roll_index + 2] |
| 73 | # Edge Case: Spare in 10th frame |
| 74 | elif self.is_spare(roll_index): |
| 75 | # Edge Case: Invalid number of rolls after spare in 10th frame |
| 76 | if len(self.rolls) < roll_index + 3: |
| 77 | raise ValueError("Score cannot be taken until the end of the game") |
| 78 | score += 10 + self.rolls[roll_index + 2] |
| 79 | # Edge Case: Open frame in 10th frame |
| 80 | else: |
| 81 | # Edge Case: Invalid number of rolls in 10th frame |
| 82 | if len(self.rolls) < roll_index + 2: |
| 83 | raise ValueError("Score cannot be taken until the end of the game") |
| 84 | score += self.sum_of_balls_in_frame(roll_index) |
| 85 | |
| 86 | return score |
| 87 | |
| 88 | def is_game_over(self): |
| 89 | """ |
| 90 | Check if the game is over (10 frames have been completed). |
| 91 | |
| 92 | Returns: |
| 93 | bool: True if the game is over, False otherwise. |
| 94 | """ |
| 95 | roll_index = 0 |
| 96 | frame_count = 0 |
| 97 | |
| 98 | while frame_count < 10 and roll_index < len(self.rolls): |
| 99 | if frame_count < 9: # Frames 1-9 |
| 100 | if self.is_strike(roll_index): |
| 101 | roll_index += 1 |
| 102 | else: |
| 103 | roll_index += 2 |
| 104 | else: # 10th frame |
| 105 | if self.is_strike(roll_index): |
| 106 | # Need two more rolls |
| 107 | if roll_index + 2 < len(self.rolls): |
| 108 | return True |
| 109 | else: |
| 110 | return False |
| 111 | elif self.is_spare(roll_index): |
| 112 | # Need one more roll |
| 113 | if roll_index + 2 < len(self.rolls): |
| 114 | return True |
| 115 | else: |
| 116 | return False |
| 117 | else: |
| 118 | # Open frame - need two rolls |
| 119 | if roll_index + 1 < len(self.rolls): |
| 120 | return True |
| 121 | else: |
| 122 | return False |
| 123 | frame_count += 1 |
| 124 | |
| 125 | return frame_count >= 10 |
| 126 | |
| 127 | def is_strike(self, roll_index): |
| 128 | """ |
| 129 | Check if a roll is a strike. |
| 130 | |
| 131 | Args: |
| 132 | roll_index (int): Index of the roll to check. |
| 133 | |
| 134 | Returns: |
| 135 | bool: True if the roll is a strike, False otherwise. |
| 136 | """ |
| 137 | # Edge Case: Roll index out of bounds |
| 138 | if roll_index >= len(self.rolls): |
| 139 | return False |
| 140 | return self.rolls[roll_index] == 10 |
| 141 | |
| 142 | def is_spare(self, roll_index): |
| 143 | """ |
| 144 | Check if a frame is a spare. |
| 145 | |
| 146 | Args: |
| 147 | roll_index (int): Index of the first roll in the frame. |
| 148 | |
| 149 | Returns: |
| 150 | bool: True if the frame is a spare, False otherwise. |
| 151 | """ |
| 152 | # Edge Case: Not enough rolls to form a frame |
| 153 | if roll_index + 1 >= len(self.rolls): |
| 154 | return False |
| 155 | return self.rolls[roll_index] + self.rolls[roll_index + 1] == 10 |
| 156 | |
| 157 | def strike_bonus(self, roll_index): |
| 158 | """ |
| 159 | Calculate the bonus for a strike. |
| 160 | |
| 161 | Args: |
| 162 | roll_index (int): Index of the strike roll. |
| 163 | |
| 164 | Returns: |
| 165 | int: The bonus points for the strike. |
| 166 | """ |
| 167 | # Edge Case: Not enough rolls for bonus calculation |
| 168 | if roll_index + 2 >= len(self.rolls): |
| 169 | return 0 |
| 170 | return self.rolls[roll_index + 1] + self.rolls[roll_index + 2] |
| 171 | |
| 172 | def spare_bonus(self, roll_index): |
| 173 | """ |
| 174 | Calculate the bonus for a spare. |
| 175 | |
| 176 | Args: |
| 177 | roll_index (int): Index of the first roll in the spare frame. |
| 178 | |
| 179 | Returns: |
| 180 | int: The bonus points for the spare. |
| 181 | """ |
| 182 | # Edge Case: Not enough rolls for bonus calculation |
| 183 | if roll_index + 2 >= len(self.rolls): |
| 184 | return 0 |
| 185 | return self.rolls[roll_index + 2] |
| 186 | |
| 187 | def sum_of_balls_in_frame(self, roll_index): |
| 188 | """ |
| 189 | Calculate the sum of pins knocked down in a frame. |
| 190 | |
| 191 | Args: |
| 192 | roll_index (int): Index of the first roll in the frame. |
| 193 | |
| 194 | Returns: |
| 195 | int: The sum of pins knocked down in the frame. |
| 196 | """ |
| 197 | # Edge Case: Not enough rolls to form a frame |
| 198 | if roll_index + 1 >= len(self.rolls): |
| 199 | return 0 if roll_index >= len(self.rolls) else self.rolls[roll_index] |
| 200 | return self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 201 | |
| 202 | # Handled Edge Cases: Invalid number of pins, game already over, game not complete yet, |
| 203 | # strike in regular frames, spare in regular frames, open frame in regular frames, |
| 204 | # strike in 10th frame, invalid number of rolls after strike in 10th frame, |
| 205 | # spare in 10th frame, invalid number of rolls after spare in 10th frame, |
| 206 | # open frame in 10th frame, invalid number of rolls in 10th frame, |
| 207 | # roll index out of bounds, not enough rolls to form a frame, |
| 208 | # not enough rolls for bonus calculation |