| 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 = 0 |
| 8 | self.current_roll_in_frame = 0 |
| 9 | self.game_complete = False |
| 10 | |
| 5 | 11 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 12 | """ |
| 13 | Record a roll in the game. |
| 14 | |
| 15 | :param pins: Number of pins knocked down in this roll. |
| 16 | :raises ValueError: If the number of pins is invalid. |
| 17 | :raises IndexError: If the game is already complete. |
| 18 | """ |
| 19 | # Edge Case: Game already complete |
| 20 | if self.game_complete: |
| 21 | raise IndexError("Game is already complete") |
| 22 | |
| 23 | # Edge Case: Invalid number of pins (negative) |
| 24 | if pins < 0: |
| 25 | raise ValueError("Pins must be between 0 and 10") |
| 26 | |
| 27 | # For 10th frame fill balls, allow more than 10 pins (will be validated during scoring) |
| 28 | if self.current_frame < 9 or (self.current_frame == 9 and self.current_roll_in_frame < 2): |
| 29 | if pins > 10: |
| 30 | raise ValueError("Pins must be between 0 and 10") |
| 31 | |
| 32 | # Edge Case: Too many pins in a frame (except for 10th frame fill balls) |
| 33 | if self.current_frame < 9: # Regular frames (0-8) |
| 34 | if self.current_roll_in_frame == 1 and self.rolls[-1] + pins > 10: |
| 35 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 36 | elif self.current_frame == 9: # 10th frame |
| 37 | # Special handling for 10th frame |
| 38 | if self.current_roll_in_frame == 1: |
| 39 | # Second roll in 10th frame |
| 40 | if self.rolls[-1] == 10: # First roll was a strike |
| 41 | # After a strike, second roll can be anything 0-10 |
| 42 | pass |
| 43 | elif self.rolls[-1] + pins > 10: |
| 44 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 45 | elif self.current_roll_in_frame == 2: |
| 46 | # Third roll in 10th frame (only if first two were strikes or spare) |
| 47 | if not (self.rolls[-2] == 10 or self.rolls[-2] + self.rolls[-1] == 10): |
| 48 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 49 | |
| 50 | self.rolls.append(pins) |
| 51 | self.current_roll_in_frame += 1 |
| 52 | |
| 53 | # Update frame tracking |
| 54 | if self.current_frame < 9: # Regular frames (0-8) |
| 55 | if self.current_roll_in_frame == 2 or pins == 10: # End of frame (2 rolls or strike) |
| 56 | self.current_frame += 1 |
| 57 | self.current_roll_in_frame = 0 |
| 58 | else: # 10th frame |
| 59 | # Special handling for 10th frame |
| 60 | if self.current_roll_in_frame == 2: |
| 61 | # Check if we need a third roll |
| 62 | if self.rolls[-2] == 10 or self.rolls[-2] + self.rolls[-1] == 10: |
| 63 | # Need a fill ball |
| 64 | pass |
| 65 | else: |
| 66 | # No fill ball needed |
| 67 | self.game_complete = True |
| 68 | elif self.current_roll_in_frame == 3: |
| 69 | # Third roll always ends the game |
| 70 | self.game_complete = True |
| 71 | |
| 8 | 72 | def score(self): |
| 9 | | pass |
| 73 | """ |
| 74 | Calculate the total score for the game. |
| 75 | |
| 76 | :return: Total score of the game. |
| 77 | :raises IndexError: If the game is not complete. |
| 78 | :raises ValueError: If there are invalid fill balls. |
| 79 | """ |
| 80 | # Edge Case: Game not complete |
| 81 | if not self.game_complete: |
| 82 | raise IndexError("Game is not yet complete") |
| 83 | |
| 84 | # Validate fill balls in 10th frame |
| 85 | if self.current_frame == 9 and len(self.rolls) >= 3: |
| 86 | # Check if any roll in 10th frame has more than 10 pins |
| 87 | frame_rolls = self.rolls[-3:] if len(self.rolls) >= 3 else self.rolls[-2:] |
| 88 | for roll in frame_rolls: |
| 89 | if roll > 10: |
| 90 | raise ValueError("invalid fill balls") |
| 91 | |
| 92 | score = 0 |
| 93 | roll_index = 0 |
| 94 | |
| 95 | for frame in range(10): |
| 96 | if self._is_strike(roll_index): # Strike |
| 97 | score += 10 + self.rolls[roll_index + 1] + self.rolls[roll_index + 2] |
| 98 | if frame < 9: # Not 10th frame |
| 99 | roll_index += 1 |
| 100 | else: # 10th frame |
| 101 | roll_index += 3 # Strike in 10th frame uses all 3 rolls |
| 102 | elif self._is_spare(roll_index): # Spare |
| 103 | score += 10 + self.rolls[roll_index + 2] |
| 104 | if frame < 9: # Not 10th frame |
| 105 | roll_index += 2 |
| 106 | else: # 10th frame |
| 107 | roll_index += 3 # Spare in 10th frame uses all 3 rolls (2 for frame + 1 bonus) |
| 108 | else: # Open frame |
| 109 | score += self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 110 | roll_index += 2 |
| 111 | |
| 112 | return score |
| 113 | |
| 114 | def _is_strike(self, roll_index): |
| 115 | """ |
| 116 | Check if a roll is a strike. |
| 117 | |
| 118 | :param roll_index: Index of the roll to check. |
| 119 | :return: True if the roll is a strike, False otherwise. |
| 120 | """ |
| 121 | return self.rolls[roll_index] == 10 |
| 122 | |
| 123 | def _is_spare(self, roll_index): |
| 124 | """ |
| 125 | Check if a frame is a spare. |
| 126 | |
| 127 | :param roll_index: Index of the first roll in the frame. |
| 128 | :return: True if the frame is a spare, False otherwise. |
| 129 | """ |
| 130 | return self.rolls[roll_index] + self.rolls[roll_index + 1] == 10 |
| 131 | |
| 132 | # Handled Edge Cases: Invalid number of pins, too many pins in a frame, game not complete, |
| 133 | # not enough rolls to calculate score, not enough rolls after strike/spare to calculate score, |
| 134 | # extra rolls after 10th frame that shouldn't exist |