| 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.frame_scores = [] |
| 10 | self.game_over = False |
| 11 | |
| 5 | 12 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 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 the number of pins is invalid or game is already over |
| 21 | """ |
| 22 | # Edge Case: Game is already over |
| 23 | if self.game_over: |
| 24 | raise ValueError("game is already over") |
| 25 | |
| 26 | # Edge Case: Invalid number of pins (negative or more than 10) |
| 27 | if pins < 0 or pins > 10: |
| 28 | raise ValueError("invalid number of pins") |
| 29 | |
| 30 | # Edge Case: Too many pins in a frame |
| 31 | if self.current_frame and (self.current_frame[0] + pins > 10): |
| 32 | raise ValueError("invalid number of pins") |
| 33 | |
| 34 | self.current_frame.append(pins) |
| 35 | self.rolls.append(pins) |
| 36 | |
| 37 | # Handle frame completion |
| 38 | frame_number = len(self.frames) |
| 39 | |
| 40 | # Regular frames (1-9) |
| 41 | if frame_number < 9: |
| 42 | # Strike case |
| 43 | if len(self.current_frame) == 1 and pins == 10: |
| 44 | self.frames.append(self.current_frame[:]) |
| 45 | self.current_frame = [] |
| 46 | # Open frame or spare |
| 47 | elif len(self.current_frame) == 2: |
| 48 | self.frames.append(self.current_frame[:]) |
| 49 | self.current_frame = [] |
| 50 | # 10th frame |
| 51 | else: |
| 52 | # Handle 10th frame special cases |
| 53 | if len(self.current_frame) == 1: |
| 54 | # First roll was a strike |
| 55 | if pins == 10: |
| 56 | # Need two more rolls |
| 57 | pass |
| 58 | else: |
| 59 | # Need one more roll to complete frame |
| 60 | pass |
| 61 | elif len(self.current_frame) == 2: |
| 62 | # Second roll in 10th frame |
| 63 | frame_sum = self.current_frame[0] + self.current_frame[1] |
| 64 | # If it's a strike followed by non-strike |
| 65 | if self.current_frame[0] == 10 and self.current_frame[1] < 10: |
| 66 | # Need one more roll |
| 67 | pass |
| 68 | # If it's a spare or open frame |
| 69 | elif frame_sum == 10 or frame_sum < 10: |
| 70 | self.frames.append(self.current_frame[:]) |
| 71 | self.current_frame = [] |
| 72 | self.game_over = True |
| 73 | elif len(self.current_frame) == 3: |
| 74 | # Third roll in 10th frame |
| 75 | self.frames.append(self.current_frame[:]) |
| 76 | self.current_frame = [] |
| 77 | self.game_over = True |
| 78 | |
| 8 | 79 | def score(self): |
| 9 | | pass |
| 80 | """ |
| 81 | Calculate the total score for the game. |
| 82 | |
| 83 | Returns: |
| 84 | int: The total score of the game |
| 85 | |
| 86 | Raises: |
| 87 | ValueError: If the game is not yet complete |
| 88 | """ |
| 89 | # Edge Case: Game is not complete |
| 90 | if not self.game_over or self.current_frame: |
| 91 | raise ValueError("game is not yet complete") |
| 92 | |
| 93 | total_score = 0 |
| 94 | |
| 95 | # Score first 9 frames |
| 96 | for i in range(9): |
| 97 | # Edge Case: Not enough frames |
| 98 | if i >= len(self.frames): |
| 99 | raise ValueError("game is not yet complete") |
| 100 | |
| 101 | frame = self.frames[i] |
| 102 | frame_score = sum(frame) |
| 103 | |
| 104 | # Strike |
| 105 | if len(frame) == 1 and frame[0] == 10: |
| 106 | # Add next one or two rolls |
| 107 | if i+1 < len(self.frames): |
| 108 | next_frame = self.frames[i+1] |
| 109 | if len(next_frame) >= 2: |
| 110 | frame_score += next_frame[0] + next_frame[1] |
| 111 | elif len(next_frame) == 1 and i+2 < len(self.frames): |
| 112 | # Next frame is also a strike |
| 113 | frame_score += next_frame[0] + self.frames[i+2][0] |
| 114 | else: |
| 115 | # Edge Case: Not enough rolls to calculate strike bonus |
| 116 | raise ValueError("game is not yet complete") |
| 117 | # Spare |
| 118 | elif len(frame) == 2 and sum(frame) == 10: |
| 119 | # Add next one roll |
| 120 | if i+1 < len(self.frames): |
| 121 | frame_score += self.frames[i+1][0] |
| 122 | else: |
| 123 | # Edge Case: Not enough rolls to calculate spare bonus |
| 124 | raise ValueError("game is not yet complete") |
| 125 | |
| 126 | total_score += frame_score |
| 127 | |
| 128 | # Score 10th frame |
| 129 | # Edge Case: Missing 10th frame |
| 130 | if len(self.frames) < 10: |
| 131 | raise ValueError("game is not yet complete") |
| 132 | |
| 133 | tenth_frame = self.frames[9] |
| 134 | total_score += sum(tenth_frame) |
| 135 | |
| 136 | return total_score |
| 137 | # Handled Edge Cases: Game already over, invalid pins, too many pins in frame, game not complete, missing frames |