| 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 ValueError("Score cannot be taken until the end of the game") |
| 22 | |
| 23 | score = 0 |
| 24 | roll_index = 0 |
| 25 | |
| 26 | for frame in range(10): |
| 27 | # Edge Case: Strike (first roll of frame knocks down all 10 pins) |
| 28 | if self.is_strike(roll_index): |
| 29 | score += 10 + self.strike_bonus(roll_index) |
| 30 | roll_index += 1 |
| 31 | # Edge Case: Spare (two rolls in frame knock down all 10 pins) |
| 32 | elif self.is_spare(roll_index): |
| 33 | score += 10 + self.spare_bonus(roll_index) |
| 34 | roll_index += 2 |
| 35 | # Edge Case: Open frame (less than 10 pins knocked down in frame) |
| 36 | else: |
| 37 | score += self.sum_of_balls_in_frame(roll_index) |
| 38 | roll_index += 2 |
| 39 | |
| 40 | return score |
| 41 | |
| 42 | def is_game_over(self): |
| 43 | if len(self.rolls) < 20: |
| 44 | return False |
| 45 | |
| 46 | # Check if we have exactly 20 rolls and they form 10 complete frames |
| 47 | roll_index = 0 |
| 48 | for frame in range(10): |
| 49 | if frame < 9: # First 9 frames |
| 50 | if self.is_strike(roll_index): |
| 51 | roll_index += 1 |
| 52 | else: |
| 53 | roll_index += 2 |
| 54 | else: # 10th frame |
| 55 | # Edge Case: 10th frame with strike needs 2 additional rolls |
| 56 | if self.is_strike(roll_index): |
| 57 | if len(self.rolls) >= roll_index + 3: |
| 58 | return True |
| 59 | else: |
| 60 | return False |
| 61 | # Edge Case: 10th frame with spare needs 1 additional roll |
| 62 | elif roll_index + 1 < len(self.rolls) and self.is_spare(roll_index): |
| 63 | if len(self.rolls) >= roll_index + 3: |
| 64 | return True |
| 65 | else: |
| 66 | return False |
| 67 | # Edge Case: 10th frame is open (no strike, no spare) |
| 68 | else: |
| 69 | if len(self.rolls) >= roll_index + 2: |
| 70 | return True |
| 71 | else: |
| 72 | return False |
| 73 | |
| 74 | return roll_index <= len(self.rolls) |
| 75 | |
| 76 | def is_strike(self, roll_index): |
| 77 | # Edge Case: Check if roll_index is valid |
| 78 | if roll_index >= len(self.rolls): |
| 79 | return False |
| 80 | return self.rolls[roll_index] == 10 |
| 81 | |
| 82 | def is_spare(self, roll_index): |
| 83 | # Edge Case: Check if roll_index and roll_index+1 are valid |
| 84 | if roll_index + 1 >= len(self.rolls): |
| 85 | return False |
| 86 | return self.rolls[roll_index] + self.rolls[roll_index + 1] == 10 |
| 87 | |
| 88 | def strike_bonus(self, roll_index): |
| 89 | # Edge Case: Check if there are enough rolls for bonus calculation |
| 90 | if roll_index + 2 >= len(self.rolls): |
| 91 | raise ValueError("Not enough rolls to calculate strike bonus") |
| 92 | return self.rolls[roll_index + 1] + self.rolls[roll_index + 2] |
| 93 | |
| 94 | def spare_bonus(self, roll_index): |
| 95 | # Edge Case: Check if there is enough rolls for bonus calculation |
| 96 | if roll_index + 2 >= len(self.rolls): |
| 97 | raise ValueError("Not enough rolls to calculate spare bonus") |
| 98 | return self.rolls[roll_index + 2] |
| 99 | |
| 100 | def sum_of_balls_in_frame(self, roll_index): |
| 101 | # Edge Case: Check if roll_index and roll_index+1 are valid |
| 102 | if roll_index + 1 >= len(self.rolls): |
| 103 | raise ValueError("Not enough rolls to calculate frame score") |
| 104 | return self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 105 | |
| 106 | # Handled Edge Cases: |
| 107 | # - Number of pins must be between 0 and 10 |
| 108 | # - Cannot roll after game is over |
| 109 | # - Cannot score incomplete game |
| 110 | # - Strike (first roll of frame knocks down all 10 pins) |
| 111 | # - Spare (two rolls in frame knock down all 10 pins) |
| 112 | # - Open frame (less than 10 pins knocked down in frame) |
| 113 | # - 10th frame with strike needs 2 additional rolls |
| 114 | # - 10th frame with spare needs 1 additional roll |
| 115 | # - 10th frame is open (no strike, no spare) |
| 116 | # - Check if roll_index is valid for strike |
| 117 | # - Check if roll_index and roll_index+1 are valid for spare |
| 118 | # - Check if there are enough rolls for strike bonus calculation |
| 119 | # - Check if there are enough rolls for spare bonus calculation |
| 120 | # - Check if roll_index and roll_index+1 are valid for frame sum calculation |