| 1 | 1 | class BowlingGame: |
| 2 | 2 | def __init__(self): |
| 3 | | pass |
| 3 | self.rolls = [] |
| 4 | self.frame_scores = [] |
| 5 | self.current_frame = 0 |
| 6 | self.frame_rolls = [] |
| 7 | self.game_over = False |
| 4 | 8 | |
| 5 | 9 | def roll(self, pins): |
| 6 | | pass |
| 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 or more than 10) |
| 15 | if pins < 0 or pins > 10: |
| 16 | raise ValueError("Pins must be between 0 and 10") |
| 17 | |
| 18 | # Edge Case: Too many pins in a frame (except for special 10th frame cases) |
| 19 | if len(self.frame_rolls) > 0 and self.current_frame < 9: |
| 20 | if self.frame_rolls[0] + pins > 10: |
| 21 | raise ValueError("Invalid roll: pins downed exceed 10 in a frame") |
| 22 | |
| 23 | self.frame_rolls.append(pins) |
| 24 | self.rolls.append(pins) |
| 25 | |
| 26 | # Handle frame completion |
| 27 | if self.current_frame < 9: # Frames 1-9 |
| 28 | # Strike case |
| 29 | if len(self.frame_rolls) == 1 and pins == 10: |
| 30 | self.frame_scores.append(self.frame_rolls) |
| 31 | self.frame_rolls = [] |
| 32 | self.current_frame += 1 |
| 33 | # Open frame or spare |
| 34 | elif len(self.frame_rolls) == 2: |
| 35 | self.frame_scores.append(self.frame_rolls) |
| 36 | self.frame_rolls = [] |
| 37 | self.current_frame += 1 |
| 38 | else: # 10th frame |
| 39 | # Handle the special rules for the 10th frame |
| 40 | if len(self.frame_rolls) == 3: |
| 41 | # All rolls are done |
| 42 | self.frame_scores.append(self.frame_rolls) |
| 43 | self.game_over = True |
| 44 | elif len(self.frame_rolls) == 2: |
| 45 | # Check if we need a third roll |
| 46 | if self.frame_rolls[0] == 10 or sum(self.frame_rolls) == 10: |
| 47 | # Strike or spare - need one more roll |
| 48 | pass |
| 49 | else: |
| 50 | # Open frame - done |
| 51 | self.frame_scores.append(self.frame_rolls) |
| 52 | self.game_over = True |
| 53 | elif len(self.frame_rolls) == 1 and self.frame_rolls[0] < 10: |
| 54 | # First roll wasn't a strike, so we need a second |
| 55 | pass |
| 7 | 56 | |
| 8 | 57 | def score(self): |
| 9 | | pass |
| 58 | # Edge Case: Empty game should return 0 |
| 59 | if len(self.rolls) == 0: |
| 60 | return 0 |
| 61 | |
| 62 | # Edge Case: Game is not yet complete |
| 63 | if not self.game_over and (self.current_frame < 9 or len(self.frame_scores) < 10): |
| 64 | raise ValueError("Score cannot be taken until the end of the game") |
| 65 | |
| 66 | total_score = 0 |
| 67 | roll_index = 0 |
| 68 | |
| 69 | # Score first 9 frames |
| 70 | for frame in range(9): |
| 71 | # Edge Case: Not enough frames completed |
| 72 | if frame >= len(self.frame_scores): |
| 73 | raise ValueError("Score cannot be taken until the end of the game") |
| 74 | |
| 75 | frame_rolls = self.frame_scores[frame] |
| 76 | |
| 77 | if len(frame_rolls) >= 1 and frame_rolls[0] == 10: # Strike |
| 78 | # Edge Case: Not enough rolls for strike bonus |
| 79 | if len(self.rolls) <= roll_index + 2: |
| 80 | raise ValueError("Score cannot be taken until the end of the game") |
| 81 | total_score += 10 + self.rolls[roll_index + 1] + self.rolls[roll_index + 2] |
| 82 | roll_index += 1 |
| 83 | elif sum(frame_rolls[:2]) == 10: # Spare |
| 84 | # Edge Case: Not enough rolls for spare bonus |
| 85 | if len(self.rolls) <= roll_index + 2: |
| 86 | raise ValueError("Score cannot be taken until the end of the game") |
| 87 | total_score += 10 + self.rolls[roll_index + 2] |
| 88 | roll_index += 2 |
| 89 | else: # Open frame |
| 90 | total_score += sum(frame_rolls[:2]) |
| 91 | roll_index += 2 |
| 92 | |
| 93 | # Score 10th frame |
| 94 | # Edge Case: 10th frame not completed |
| 95 | if len(self.frame_scores) < 10: |
| 96 | raise ValueError("Score cannot be taken until the end of the game") |
| 97 | |
| 98 | tenth_frame = self.frame_scores[9] |
| 99 | total_score += sum(tenth_frame) |
| 100 | |
| 101 | return total_score |
| 102 | |
| 103 | # Handled Edge Cases: Game already over, invalid pin count, too many pins in a frame, game not complete for scoring, insufficient rolls for strike/spare bonus, 10th frame completion rules |