| 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 | self.game_over = False |
| 11 | |
| 5 | 12 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 13 | """ |
| 14 | Record a roll in the game. |
| 15 | |
| 16 | Args: |
| 17 | pins (int): Number of pins knocked down in this roll. |
| 18 | |
| 19 | Raises: |
| 20 | ValueError: If the number of pins is invalid or game is already over. |
| 21 | """ |
| 22 | # Edge Case: Game is already over |
| 23 | if self.game_over: |
| 24 | raise ValueError("game is already over") |
| 25 | |
| 26 | # Edge Case: Invalid number of pins (negative or more than 10) |
| 27 | if pins < 0 or pins > 10: |
| 28 | raise ValueError("invalid number of pins") |
| 29 | |
| 30 | # Edge Case: Too many pins in a frame |
| 31 | if len(self.frame_rolls) == 1: |
| 32 | # For tenth frame, we allow up to 10 pins on second roll regardless of first |
| 33 | if self.current_frame == 10: |
| 34 | # Special case for tenth frame - if first roll was 10 (strike), second can be up to 10 |
| 35 | if self.frame_rolls[0] == 10: |
| 36 | pass # Second roll can be anything up to 10 |
| 37 | # If first roll wasn't 10, sum must not exceed 10 |
| 38 | elif self.frame_rolls[0] + pins > 10: |
| 39 | raise ValueError("invalid number of pins") |
| 40 | # For other frames, sum of two rolls cannot exceed 10 |
| 41 | elif self.frame_rolls[0] + pins > 10: |
| 42 | raise ValueError("invalid number of pins") |
| 43 | |
| 44 | self.frame_rolls.append(pins) |
| 45 | self.rolls.append(pins) |
| 46 | |
| 47 | # Handle frame completion |
| 48 | if self.current_frame < 10: |
| 49 | # Regular frames (1-9) |
| 50 | if pins == 10 or len(self.frame_rolls) == 2: |
| 51 | self.frames.append(self.frame_rolls[:]) |
| 52 | self.frame_rolls = [] |
| 53 | self.current_frame += 1 |
| 54 | else: |
| 55 | # Tenth frame |
| 56 | # Strike case: need two more rolls |
| 57 | if len(self.frames) == 9 and len(self.frame_rolls) == 3: |
| 58 | # Third roll in tenth frame |
| 59 | self.frames.append(self.frame_rolls[:]) |
| 60 | self.frame_rolls = [] |
| 61 | self.game_over = True |
| 62 | elif len(self.frames) == 9 and len(self.frame_rolls) == 2: |
| 63 | # Second roll in tenth frame |
| 64 | # If it's a strike or spare, we need one more roll |
| 65 | if self.frame_rolls[0] == 10 or sum(self.frame_rolls) == 10: |
| 66 | # Need one more roll for fill ball |
| 67 | pass |
| 68 | else: |
| 69 | # Open frame, game over |
| 70 | self.frames.append(self.frame_rolls[:]) |
| 71 | self.frame_rolls = [] |
| 72 | self.game_over = True |
| 73 | elif len(self.frames) == 10: |
| 74 | # This means we already completed the tenth frame |
| 75 | self.game_over = True |
| 76 | |
| 8 | 77 | def score(self): |
| 9 | | pass |
| 78 | """ |
| 79 | Calculate the total score for the game. |
| 80 | |
| 81 | Returns: |
| 82 | int: The total score of the game. |
| 83 | |
| 84 | Raises: |
| 85 | ValueError: If the game is not yet complete. |
| 86 | """ |
| 87 | # Edge Case: Game is not complete |
| 88 | if not self.game_over and (len(self.frames) < 10 or |
| 89 | (len(self.frames) == 10 and len(self.frame_rolls) > 0)): |
| 90 | raise ValueError("game is not yet complete") |
| 91 | |
| 92 | total_score = 0 |
| 93 | roll_index = 0 |
| 94 | |
| 95 | # Score first 9 frames |
| 96 | for frame_index in range(9): |
| 97 | if roll_index >= len(self.rolls): |
| 98 | raise ValueError("game is not yet complete") |
| 99 | |
| 100 | # Strike |
| 101 | if self.rolls[roll_index] == 10: |
| 102 | # Edge Case: Not enough rolls to calculate strike bonus |
| 103 | if roll_index + 2 >= len(self.rolls): |
| 104 | raise ValueError("game is not yet complete") |
| 105 | total_score += 10 + self.rolls[roll_index + 1] + self.rolls[roll_index + 2] |
| 106 | roll_index += 1 |
| 107 | else: |
| 108 | frame_score = self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 109 | # Spare |
| 110 | if frame_score == 10: |
| 111 | # Edge Case: Not enough rolls to calculate spare bonus |
| 112 | if roll_index + 2 >= len(self.rolls): |
| 113 | raise ValueError("game is not yet complete") |
| 114 | total_score += 10 + self.rolls[roll_index + 2] |
| 115 | else: |
| 116 | # Open frame |
| 117 | total_score += frame_score |
| 118 | roll_index += 2 |
| 119 | |
| 120 | # Score tenth frame |
| 121 | if len(self.frames) < 10: |
| 122 | raise ValueError("game is not yet complete") |
| 123 | |
| 124 | tenth_frame = self.frames[9] |
| 125 | |
| 126 | # Edge Case: Tenth frame has invalid number of rolls |
| 127 | if len(tenth_frame) < 2 or len(tenth_frame) > 3: |
| 128 | raise ValueError("game is not yet complete") |
| 129 | |
| 130 | total_score += sum(tenth_frame) |
| 131 | |
| 132 | return total_score |
| 133 | |
| 134 | # Handled Edge Cases: Game already over, invalid pins count, too many pins in frame, |
| 135 | # incomplete game when scoring, insufficient rolls for strike/spare bonus, |
| 136 | # invalid tenth frame rolls |