| 1 | 1 | class BowlingGame: |
| 2 | 2 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 3 | self.frames = [] |
| 4 | self.current_frame = [] |
| 5 | self.frame_count = 0 |
| 6 | self.game_over = False |
| 7 | self.rolls = [] |
| 8 | |
| 5 | 9 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 10 | # Edge Case: Game is already over |
| 11 | if self.game_over: |
| 12 | raise ValueError("Cannot roll after game is over") |
| 13 | |
| 14 | # Edge Case: Invalid pin count (negative) |
| 15 | if pins < 0: |
| 16 | raise ValueError("Negative roll is invalid") |
| 17 | |
| 18 | # Edge Case: Invalid pin count (more than 10) |
| 19 | if pins > 10: |
| 20 | raise ValueError("Pin count exceeds pins on the lane") |
| 21 | |
| 22 | # Edge Case: First roll in a frame with more than 10 pins |
| 23 | if len(self.current_frame) == 1 and self.frame_count < 9: |
| 24 | if self.current_frame[0] + pins > 10: |
| 25 | raise ValueError("Pin count exceeds pins on the lane") |
| 26 | |
| 27 | self.current_frame.append(pins) |
| 28 | self.rolls.append(pins) |
| 29 | |
| 30 | # Handle regular frames (0-8) |
| 31 | if self.frame_count < 9: |
| 32 | # Strike case |
| 33 | if pins == 10 and len(self.current_frame) == 1: |
| 34 | self.frames.append(self.current_frame[:]) |
| 35 | self.current_frame = [] |
| 36 | self.frame_count += 1 |
| 37 | # Regular case (2 rolls) |
| 38 | elif len(self.current_frame) == 2: |
| 39 | self.frames.append(self.current_frame[:]) |
| 40 | self.current_frame = [] |
| 41 | self.frame_count += 1 |
| 42 | # Handle last frame (9) |
| 43 | else: |
| 44 | # Edge Case: Invalid fill balls in 10th frame |
| 45 | if len(self.current_frame) == 2: |
| 46 | # If first two rolls are not strike or spare, no third roll allowed |
| 47 | if self.current_frame[0] != 10 and sum(self.current_frame) < 10: |
| 48 | self.frames.append(self.current_frame[:]) |
| 49 | self.current_frame = [] |
| 50 | self.game_over = True |
| 51 | elif len(self.current_frame) == 3: |
| 52 | # Validate the third roll |
| 53 | # If first roll was strike |
| 54 | if self.current_frame[0] == 10: |
| 55 | # Second roll is strike |
| 56 | if self.current_frame[1] == 10: |
| 57 | # Valid - third roll can be anything 0-10 |
| 58 | pass |
| 59 | # Second roll is not strike |
| 60 | else: |
| 61 | # Third roll combined with second must not exceed 10 (unless second is 10) |
| 62 | if self.current_frame[1] + self.current_frame[2] > 10 and self.current_frame[1] != 10: |
| 63 | raise ValueError("invalid fill balls") |
| 64 | # First roll not strike, but second makes spare |
| 65 | elif self.current_frame[0] != 10 and self.current_frame[0] + self.current_frame[1] == 10: |
| 66 | # Third roll is fine (0-10) |
| 67 | pass |
| 68 | # First two rolls don't make strike or spare |
| 69 | else: |
| 70 | raise ValueError("invalid fill balls") |
| 71 | |
| 72 | self.frames.append(self.current_frame[:]) |
| 73 | self.current_frame = [] |
| 74 | self.game_over = True |
| 75 | |
| 8 | 76 | def score(self): |
| 9 | | pass |
| 77 | # Edge Case: Attempting to score before game is complete |
| 78 | if not self.game_over and (len(self.frames) < 10 or (len(self.frames) == 10 and self.current_frame)): |
| 79 | raise IndexError("Score cannot be taken until the end of the game") |
| 80 | |
| 81 | total_score = 0 |
| 82 | roll_index = 0 |
| 83 | |
| 84 | # Score first 9 frames |
| 85 | for frame_num in range(9): |
| 86 | if roll_index >= len(self.rolls): |
| 87 | break |
| 88 | |
| 89 | # Strike |
| 90 | if self.rolls[roll_index] == 10: |
| 91 | total_score += 10 |
| 92 | # Add next two rolls |
| 93 | if roll_index + 1 < len(self.rolls): |
| 94 | total_score += self.rolls[roll_index + 1] |
| 95 | if roll_index + 2 < len(self.rolls): |
| 96 | total_score += self.rolls[roll_index + 2] |
| 97 | roll_index += 1 |
| 98 | else: |
| 99 | frame_score = self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 100 | total_score += frame_score |
| 101 | # Spare |
| 102 | if frame_score == 10: |
| 103 | # Add next roll |
| 104 | if roll_index + 2 < len(self.rolls): |
| 105 | total_score += self.rolls[roll_index + 2] |
| 106 | roll_index += 2 |
| 107 | |
| 108 | # Score 10th frame |
| 109 | while roll_index < len(self.rolls): |
| 110 | total_score += self.rolls[roll_index] |
| 111 | roll_index += 1 |
| 112 | |
| 113 | return total_score |
| 114 | # Handled Edge Cases: Game already over, negative pins, too many pins, invalid pin count in frame, invalid fill balls, scoring before game completion |