| 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.frames = [] |
| 8 | self.current_frame = [] |
| 9 | self.game_over = False |
| 10 | |
| 5 | 11 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 12 | """ |
| 13 | Record a roll in the game. |
| 14 | |
| 15 | Args: |
| 16 | pins (int): Number of pins knocked down in this roll |
| 17 | |
| 18 | Raises: |
| 19 | ValueError: If pins is negative or greater than 10 |
| 20 | IndexError: If game is already over |
| 21 | """ |
| 22 | # Edge Case: Game already over |
| 23 | if self.game_over: |
| 24 | raise IndexError("Game is already complete") |
| 25 | |
| 26 | # Edge Case: Invalid pin count |
| 27 | if pins < 0 or pins > 10: |
| 28 | raise ValueError("Pins must be between 0 and 10") |
| 29 | |
| 30 | # Special validation for tenth frame fill balls (before adding the roll) |
| 31 | if len(self.frames) == 9 and len(self.current_frame) == 2: # About to add third roll in tenth frame |
| 32 | if self.current_frame[0] == 10: # First roll was strike |
| 33 | # If second roll is also a strike, third roll can be 0-10 |
| 34 | # If second roll is not a strike, third roll cannot exceed (10 - second roll) |
| 35 | if self.current_frame[1] != 10 and self.current_frame[1] + pins > 10: |
| 36 | raise ValueError("invalid fill balls") |
| 37 | else: # First roll wasn't strike (must be spare) |
| 38 | # First two rolls must sum to 10 for spare |
| 39 | if sum(self.current_frame[:2]) != 10: |
| 40 | raise ValueError("invalid fill balls") |
| 41 | |
| 42 | self.rolls.append(pins) |
| 43 | self.current_frame.append(pins) |
| 44 | |
| 45 | # Edge Case: Too many pins in a frame (only for frames 1-9) |
| 46 | if len(self.frames) < 9: # Only for frames 1-9 |
| 47 | if len(self.current_frame) == 2: |
| 48 | # Two rolls in frame - validate total |
| 49 | if self.current_frame[0] + self.current_frame[1] > 10: |
| 50 | raise ValueError("Cannot knock down more than 10 pins in a frame") |
| 51 | |
| 52 | # Handle frame completion |
| 53 | if len(self.frames) < 9: # Frames 1-9 |
| 54 | # Complete frame if it's a strike or has two rolls |
| 55 | if pins == 10 or len(self.current_frame) == 2: |
| 56 | self.frames.append(self.current_frame) |
| 57 | self.current_frame = [] |
| 58 | elif len(self.frames) == 9: # Tenth frame |
| 59 | # Check if tenth frame is complete |
| 60 | if len(self.current_frame) == 2: |
| 61 | # Two rolls in tenth frame |
| 62 | if self.current_frame[0] == 10 or sum(self.current_frame) == 10: |
| 63 | # Strike or spare - need third roll |
| 64 | pass |
| 65 | else: |
| 66 | # Open frame - game complete |
| 67 | self.frames.append(self.current_frame) |
| 68 | self.game_over = True |
| 69 | elif len(self.current_frame) == 3: |
| 70 | # Three rolls in tenth frame - game complete |
| 71 | self.frames.append(self.current_frame) |
| 72 | self.game_over = True |
| 73 | |
| 8 | 74 | def score(self): |
| 9 | | pass |
| 75 | """ |
| 76 | Calculate the total score for the game. |
| 77 | |
| 78 | Returns: |
| 79 | int: The total score of the game |
| 80 | |
| 81 | Raises: |
| 82 | IndexError: If the game is not yet complete |
| 83 | """ |
| 84 | # For a new game with no rolls, return 0 |
| 85 | if len(self.rolls) == 0: |
| 86 | return 0 |
| 87 | |
| 88 | # Edge Case: Game not complete |
| 89 | if not self.game_over: |
| 90 | raise IndexError("Game is not yet complete") |
| 91 | |
| 92 | total_score = 0 |
| 93 | roll_index = 0 |
| 94 | |
| 95 | for frame_index in range(10): |
| 96 | if frame_index < 9: # Frames 1-9 |
| 97 | if self.rolls[roll_index] == 10: # Strike |
| 98 | total_score += 10 + self.rolls[roll_index + 1] + self.rolls[roll_index + 2] |
| 99 | roll_index += 1 |
| 100 | elif self.rolls[roll_index] + self.rolls[roll_index + 1] == 10: # Spare |
| 101 | total_score += 10 + self.rolls[roll_index + 2] |
| 102 | roll_index += 2 |
| 103 | else: # Open frame |
| 104 | total_score += self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 105 | roll_index += 2 |
| 106 | else: # Tenth frame |
| 107 | # For the 10th frame, sum all the rolls in that frame |
| 108 | # Find the start of the 10th frame by counting rolls in previous frames |
| 109 | tenth_frame_start = 0 |
| 110 | for i in range(9): |
| 111 | if tenth_frame_start < len(self.rolls) and self.rolls[tenth_frame_start] == 10: # Strike |
| 112 | tenth_frame_start += 1 |
| 113 | elif tenth_frame_start + 1 < len(self.rolls): |
| 114 | tenth_frame_start += 2 |
| 115 | else: |
| 116 | break |
| 117 | |
| 118 | # Sum all remaining rolls (10th frame) |
| 119 | while tenth_frame_start < len(self.rolls): |
| 120 | total_score += self.rolls[tenth_frame_start] |
| 121 | tenth_frame_start += 1 |
| 122 | |
| 123 | return total_score |
| 124 | |
| 125 | # Handled Edge Cases: Game already over, invalid pin count, too many pins in a frame, invalid fill balls, game completed, game not complete |
| 126 | # Handled Edge Cases: Game not complete |