| 1 | 1 | class BowlingGame: |
| 2 | 2 | def __init__(self): |
| 3 | | pass |
| 3 | """ |
| 4 | Initialize a new bowling game. |
| 5 | """ |
| 6 | self.rolls = [] |
| 7 | self.frames = [] |
| 8 | self.current_frame = [] |
| 9 | self.frame_scores = [] |
| 10 | self.game_over = False |
| 4 | 11 | |
| 5 | 12 | def roll(self, pins): |
| 6 | | pass |
| 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 pins is negative or greater than 10 |
| 21 | ValueError: If the game is already over |
| 22 | """ |
| 23 | # Edge Case: Game already over |
| 24 | if self.game_over: |
| 25 | raise ValueError("game already finished") |
| 26 | |
| 27 | # Edge Case: Invalid pin count |
| 28 | if pins < 0 or pins > 10: |
| 29 | raise ValueError("pin count must be between 0 and 10") |
| 30 | |
| 31 | # Edge Case: Too many pins in a frame (except 10th frame special cases) |
| 32 | if len(self.current_frame) > 0 and len(self.frames) < 9: |
| 33 | if self.current_frame[0] + pins > 10: |
| 34 | raise ValueError("pin count exceeds pins on the lane") |
| 35 | |
| 36 | self.current_frame.append(pins) |
| 37 | self.rolls.append(pins) |
| 38 | |
| 39 | # Handle regular frames (1-9) |
| 40 | if len(self.frames) < 9: |
| 41 | # Strike case |
| 42 | if pins == 10: |
| 43 | self.frames.append(self.current_frame[:]) |
| 44 | self.current_frame = [] |
| 45 | # Two rolls completed |
| 46 | elif len(self.current_frame) == 2: |
| 47 | self.frames.append(self.current_frame[:]) |
| 48 | self.current_frame = [] |
| 49 | # Handle 10th frame |
| 50 | else: |
| 51 | # Edge Case: Invalid fill balls |
| 52 | if len(self.current_frame) == 2: |
| 53 | # If first two rolls sum to less than 10, no third roll allowed |
| 54 | if self.current_frame[0] + self.current_frame[1] < 10: |
| 55 | self.frames.append(self.current_frame[:]) |
| 56 | self.current_frame = [] |
| 57 | self.game_over = True |
| 58 | elif len(self.current_frame) == 3: |
| 59 | # Validate the 10th frame |
| 60 | first_roll = self.current_frame[0] |
| 61 | second_roll = self.current_frame[1] |
| 62 | third_roll = self.current_frame[2] |
| 63 | |
| 64 | # If first roll is a strike |
| 65 | if first_roll == 10: |
| 66 | # Second roll is also a strike |
| 67 | if second_roll == 10: |
| 68 | # Third roll must be valid |
| 69 | if third_roll > 10: |
| 70 | raise ValueError("pin count exceeds pins on the lane") |
| 71 | # Second roll is not a strike |
| 72 | else: |
| 73 | # Sum of second and third must not exceed 10 |
| 74 | if second_roll + third_roll > 10 and second_roll != 10: |
| 75 | raise ValueError("pin count exceeds pins on the lane") |
| 76 | # If first roll is not a strike but first two make a spare |
| 77 | elif first_roll + second_roll == 10: |
| 78 | # Third roll must be valid |
| 79 | if third_roll > 10: |
| 80 | raise ValueError("pin count exceeds pins on the lane") |
| 81 | # If first two rolls are less than 10 |
| 82 | else: |
| 83 | # Should not have a third roll |
| 84 | raise ValueError("should not be able to roll after game is over") |
| 85 | |
| 86 | self.frames.append(self.current_frame[:]) |
| 87 | self.current_frame = [] |
| 88 | self.game_over = True |
| 7 | 89 | |
| 8 | 90 | def score(self): |
| 9 | | pass |
| 91 | """ |
| 92 | Calculate the total score for the game. |
| 93 | |
| 94 | Returns: |
| 95 | int: The total score for the game |
| 96 | |
| 97 | Raises: |
| 98 | ValueError: If the game is not yet complete |
| 99 | """ |
| 100 | # Edge Case: Game not complete |
| 101 | if not self.game_over and (len(self.frames) < 10 or |
| 102 | (len(self.frames) == 10 and len(self.current_frame) > 0)): |
| 103 | raise ValueError("game not yet complete") |
| 104 | |
| 105 | # Edge Case: Not enough frames |
| 106 | if len(self.frames) < 10: |
| 107 | raise ValueError("game not yet complete") |
| 108 | |
| 109 | total_score = 0 |
| 110 | |
| 111 | # Score first 9 frames |
| 112 | for i in range(9): |
| 113 | frame = self.frames[i] |
| 114 | frame_score = sum(frame) |
| 115 | |
| 116 | # Strike |
| 117 | if len(frame) == 1 and frame[0] == 10: |
| 118 | # Add next two rolls |
| 119 | if i+1 < len(self.frames): |
| 120 | next_frame = self.frames[i+1] |
| 121 | if len(next_frame) >= 2: |
| 122 | frame_score += next_frame[0] + next_frame[1] |
| 123 | elif len(next_frame) == 1 and i+2 < len(self.frames): |
| 124 | # If next frame is also a strike |
| 125 | frame_score += next_frame[0] + self.frames[i+2][0] |
| 126 | else: |
| 127 | # Not enough rolls to calculate score |
| 128 | raise ValueError("game not yet complete") |
| 129 | # Spare |
| 130 | elif len(frame) == 2 and sum(frame) == 10: |
| 131 | # Add next roll |
| 132 | if i+1 < len(self.frames): |
| 133 | frame_score += self.frames[i+1][0] |
| 134 | else: |
| 135 | # Not enough rolls to calculate score |
| 136 | raise ValueError("game not yet complete") |
| 137 | |
| 138 | total_score += frame_score |
| 139 | |
| 140 | # Score 10th frame |
| 141 | if len(self.frames) >= 10: |
| 142 | tenth_frame = self.frames[9] |
| 143 | total_score += sum(tenth_frame) |
| 144 | |
| 145 | return total_score |
| 146 | |
| 147 | # Handled Edge Cases: Game already over, invalid pin count, too many pins in a frame, invalid fill balls, game not complete, not enough frames |
| 148 | # Handled Edge Cases: Invalid pin count, game already over, too many pins in frame, invalid fill balls, game not complete, not enough frames |