| 1 | 1 | class BowlingGame: |
| 2 | 2 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 3 | self.rolls = [] |
| 4 | self.current_roll = 0 |
| 5 | |
| 5 | 6 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 7 | # Edge Case: Number of pins must be between 0 and 10 |
| 8 | if pins < 0 or pins > 10: |
| 9 | raise ValueError("Pins must be between 0 and 10") |
| 10 | |
| 11 | # Edge Case: Cannot roll after game is over |
| 12 | if self.is_game_over(): |
| 13 | raise ValueError("Cannot roll after game is over") |
| 14 | |
| 15 | self.rolls.append(pins) |
| 16 | self.current_roll += 1 |
| 17 | |
| 8 | 18 | def score(self): |
| 9 | | pass |
| 19 | # Edge Case: Cannot score incomplete game |
| 20 | if not self.is_game_over(): |
| 21 | raise IndexError("Score cannot be taken until the end of the game") |
| 22 | |
| 23 | total_score = 0 |
| 24 | roll_index = 0 |
| 25 | |
| 26 | for frame in range(10): |
| 27 | # Edge Case: Invalid frame state |
| 28 | if roll_index >= len(self.rolls): |
| 29 | raise ValueError("Invalid game state") |
| 30 | |
| 31 | if self.is_strike(roll_index): # Strike |
| 32 | # Edge Case: Incomplete strike frame |
| 33 | if roll_index + 2 >= len(self.rolls): |
| 34 | raise ValueError("Invalid game state") |
| 35 | |
| 36 | total_score += 10 + self.strike_bonus(roll_index) |
| 37 | roll_index += 1 |
| 38 | elif self.is_spare(roll_index): # Spare |
| 39 | # Edge Case: Incomplete spare frame |
| 40 | if roll_index + 2 >= len(self.rolls): |
| 41 | raise ValueError("Invalid game state") |
| 42 | |
| 43 | total_score += 10 + self.spare_bonus(roll_index) |
| 44 | roll_index += 2 |
| 45 | else: # Open frame |
| 46 | # Edge Case: Incomplete open frame |
| 47 | if roll_index + 1 >= len(self.rolls): |
| 48 | raise ValueError("Invalid game state") |
| 49 | |
| 50 | frame_score = self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 51 | # Edge Case: Frame score cannot exceed 10 in open frame |
| 52 | if frame_score > 10: |
| 53 | raise ValueError("Invalid frame score") |
| 54 | |
| 55 | total_score += frame_score |
| 56 | roll_index += 2 |
| 57 | |
| 58 | return total_score |
| 59 | |
| 60 | def is_strike(self, roll_index): |
| 61 | return self.rolls[roll_index] == 10 |
| 62 | |
| 63 | def is_spare(self, roll_index): |
| 64 | return self.rolls[roll_index] + self.rolls[roll_index + 1] == 10 |
| 65 | |
| 66 | def strike_bonus(self, roll_index): |
| 67 | return self.rolls[roll_index + 1] + self.rolls[roll_index + 2] |
| 68 | |
| 69 | def spare_bonus(self, roll_index): |
| 70 | return self.rolls[roll_index + 2] |
| 71 | |
| 72 | def is_game_over(self): |
| 73 | if len(self.rolls) < 12: # Minimum rolls needed |
| 74 | return False |
| 75 | |
| 76 | # Check if we have exactly 10 frames worth of rolls |
| 77 | roll_index = 0 |
| 78 | frame_count = 0 |
| 79 | |
| 80 | while frame_count < 10 and roll_index < len(self.rolls): |
| 81 | if self.is_strike(roll_index): |
| 82 | roll_index += 1 |
| 83 | else: |
| 84 | roll_index += 2 |
| 85 | frame_count += 1 |
| 86 | |
| 87 | # If we've processed exactly 10 frames |
| 88 | if frame_count == 10: |
| 89 | # Edge Case: Tenth frame was a strike or spare, need to check fill balls |
| 90 | if roll_index < len(self.rolls): |
| 91 | # If tenth frame was a strike |
| 92 | if self.rolls[roll_index-1] == 10: |
| 93 | # Need exactly two more rolls |
| 94 | return roll_index + 1 == len(self.rolls) |
| 95 | # If tenth frame was open |
| 96 | else: |
| 97 | return roll_index == len(self.rolls) |
| 98 | else: |
| 99 | return True |
| 100 | |
| 101 | return False |
| 102 | |
| 103 | # Handled Edge Cases: Pins out of range, rolling after game over, scoring incomplete game, incomplete frames, invalid frame scores |