| 1 | 1 | class BowlingGame: |
| 2 | 2 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 3 | """ |
| 4 | Initialize a new bowling game. |
| 5 | |
| 6 | The game tracks rolls, frames, and whether the game is complete. |
| 7 | """ |
| 8 | self.rolls = [] |
| 9 | self.frames = [] |
| 10 | self.current_frame = [] |
| 11 | self.is_complete = False |
| 12 | |
| 5 | 13 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 14 | """ |
| 15 | Record a roll in the game. |
| 16 | |
| 17 | Args: |
| 18 | pins (int): Number of pins knocked down in this roll |
| 19 | |
| 20 | Raises: |
| 21 | ValueError: If pins is negative or greater than 10 |
| 22 | ValueError: If the game is already complete |
| 23 | ValueError: If a roll exceeds 10 pins in a frame |
| 24 | """ |
| 25 | # Edge Case: Game already complete |
| 26 | if self.is_complete: |
| 27 | raise ValueError("Cannot roll after game is over") |
| 28 | |
| 29 | # Edge Case: Invalid pin count |
| 30 | if pins < 0: |
| 31 | raise ValueError("Negative roll is invalid") |
| 32 | |
| 33 | # Edge Case: Invalid pin count |
| 34 | if pins > 10: |
| 35 | raise ValueError("Pin count exceeds pins on the lane") |
| 36 | |
| 37 | # Edge Case: Adding roll would exceed 10 pins in a frame (only for regular frames) |
| 38 | frame_number = len(self.frames) |
| 39 | if frame_number < 9: # Regular frames (1-9) |
| 40 | if len(self.current_frame) > 0 and self.current_frame[0] + pins > 10: |
| 41 | raise ValueError("Pin count exceeds pins on the lane") |
| 42 | elif frame_number == 9: # 10th frame - special validation |
| 43 | if len(self.current_frame) == 1: |
| 44 | # After first roll |
| 45 | if self.current_frame[0] != 10 and self.current_frame[0] + pins > 10: |
| 46 | raise ValueError("Pin count exceeds pins on the lane") |
| 47 | elif len(self.current_frame) == 2: |
| 48 | # After second roll - validate based on what happened |
| 49 | if self.current_frame[0] == 10: |
| 50 | # First was strike, validate second + third |
| 51 | if self.current_frame[1] != 10 and self.current_frame[1] + pins > 10: |
| 52 | raise ValueError("Pin count exceeds pins on the lane") |
| 53 | elif self.current_frame[0] + self.current_frame[1] == 10: |
| 54 | # First two were spare, third roll is always valid (0-10) |
| 55 | pass |
| 56 | else: |
| 57 | # Open frame - shouldn't reach here for third roll |
| 58 | raise ValueError("Cannot roll after open tenth frame") |
| 59 | |
| 60 | self.current_frame.append(pins) |
| 61 | self.rolls.append(pins) |
| 62 | |
| 63 | # Handle frame completion |
| 64 | frame_number = len(self.frames) |
| 65 | |
| 66 | # Regular frames (1-9) |
| 67 | if frame_number < 9: |
| 68 | # Strike (first roll is 10) |
| 69 | if len(self.current_frame) == 1 and self.current_frame[0] == 10: |
| 70 | self.frames.append(self.current_frame[:]) |
| 71 | self.current_frame = [] |
| 72 | # Open frame or spare (two rolls) |
| 73 | elif len(self.current_frame) == 2: |
| 74 | self.frames.append(self.current_frame[:]) |
| 75 | self.current_frame = [] |
| 76 | # 10th frame - special rules |
| 77 | elif frame_number == 9: |
| 78 | # Check if 10th frame is complete |
| 79 | if len(self.current_frame) == 2: |
| 80 | # Open frame (less than 10 pins) - game complete |
| 81 | if self.current_frame[0] + self.current_frame[1] < 10: |
| 82 | self.frames.append(self.current_frame[:]) |
| 83 | self.current_frame = [] |
| 84 | self.is_complete = True |
| 85 | # Spare (exactly 10 pins) - need one more roll |
| 86 | # Don't complete frame yet |
| 87 | elif len(self.current_frame) == 3: |
| 88 | # Strike or spare completed with fill balls |
| 89 | self.frames.append(self.current_frame[:]) |
| 90 | self.current_frame = [] |
| 91 | self.is_complete = True |
| 92 | |
| 8 | 93 | def score(self): |
| 9 | | pass |
| 94 | """ |
| 95 | Calculate the total score for the game. |
| 96 | |
| 97 | Returns: |
| 98 | int: The total score of the game |
| 99 | |
| 100 | Raises: |
| 101 | ValueError: If the game is not yet complete |
| 102 | """ |
| 103 | # Edge Case: Game not complete (but allow empty game) |
| 104 | if not self.is_complete and len(self.rolls) > 0: |
| 105 | raise ValueError("Score cannot be taken until the end of the game") |
| 106 | |
| 107 | # Edge Case: No rolls yet |
| 108 | if len(self.rolls) == 0: |
| 109 | return 0 |
| 110 | |
| 111 | total_score = 0 |
| 112 | roll_index = 0 |
| 113 | |
| 114 | # Score first 9 frames |
| 115 | for frame_index in range(9): |
| 116 | # Strike |
| 117 | if self.rolls[roll_index] == 10: |
| 118 | # Add 10 plus next two rolls |
| 119 | total_score += 10 + self.rolls[roll_index + 1] + self.rolls[roll_index + 2] |
| 120 | roll_index += 1 |
| 121 | # Spare |
| 122 | elif self.rolls[roll_index] + self.rolls[roll_index + 1] == 10: |
| 123 | # Add 10 plus next roll |
| 124 | total_score += 10 + self.rolls[roll_index + 2] |
| 125 | roll_index += 2 |
| 126 | # Open frame |
| 127 | else: |
| 128 | # Add both rolls |
| 129 | total_score += self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 130 | roll_index += 2 |
| 131 | |
| 132 | # Score 10th frame |
| 133 | # Add all remaining rolls (2 or 3) |
| 134 | while roll_index < len(self.rolls): |
| 135 | total_score += self.rolls[roll_index] |
| 136 | roll_index += 1 |
| 137 | |
| 138 | return total_score |
| 139 | |
| 140 | # Handled Edge Cases: Game already complete, invalid pin count, adding roll exceeding 10 pins in frame, game not complete for scoring |