| 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.current_frame = 1 |
| 8 | self.frame_rolls = [] |
| 9 | self.frames = [] |
| 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 | ValueError: If trying to roll after the game is over |
| 21 | """ |
| 22 | # Edge Case: Invalid number of pins |
| 23 | if pins < 0: |
| 24 | raise ValueError("Negative roll is invalid") |
| 25 | if pins > 10: |
| 26 | raise ValueError("Pin count exceeds pins on the lane") |
| 27 | |
| 28 | # Edge Case: Game already completed |
| 29 | if len(self.frames) == 10: |
| 30 | raise ValueError("Cannot roll after game is over") |
| 31 | |
| 32 | self.frame_rolls.append(pins) |
| 33 | self.rolls.append(pins) |
| 34 | |
| 35 | # Handle regular frames (1-9) |
| 36 | if self.current_frame < 10: |
| 37 | # Strike case - frame ends immediately |
| 38 | if pins == 10 and len(self.frame_rolls) == 1: |
| 39 | self.frames.append(self.frame_rolls[:]) |
| 40 | self.frame_rolls = [] |
| 41 | self.current_frame += 1 |
| 42 | # Two rolls completed |
| 43 | elif len(self.frame_rolls) == 2: |
| 44 | # Edge Case: Frame total exceeds 10 (except for strikes which are handled above) |
| 45 | if sum(self.frame_rolls) > 10: |
| 46 | raise ValueError("Pin count exceeds pins on the lane") |
| 47 | self.frames.append(self.frame_rolls[:]) |
| 48 | self.frame_rolls = [] |
| 49 | self.current_frame += 1 |
| 50 | # Handle 10th frame |
| 51 | else: |
| 52 | # First roll is a strike |
| 53 | if len(self.frame_rolls) == 1 and self.frame_rolls[0] == 10: |
| 54 | # Need two more rolls |
| 55 | if len(self.frame_rolls) == 3: |
| 56 | # Edge Case: Invalid fill balls - second and third rolls exceed 10 |
| 57 | if self.frame_rolls[1] < 10 and self.frame_rolls[1] + self.frame_rolls[2] > 10: |
| 58 | raise ValueError("Pin count exceeds pins on the lane") |
| 59 | self.frames.append(self.frame_rolls[:]) |
| 60 | self.frame_rolls = [] |
| 61 | # First roll not a strike, second roll makes a spare |
| 62 | elif len(self.frame_rolls) == 2 and sum(self.frame_rolls) == 10: |
| 63 | # Need one more roll |
| 64 | if len(self.frame_rolls) == 3: |
| 65 | self.frames.append(self.frame_rolls[:]) |
| 66 | self.frame_rolls = [] |
| 67 | # Open frame (less than 10 pins in first two rolls) |
| 68 | elif len(self.frame_rolls) == 2 and sum(self.frame_rolls) < 10: |
| 69 | self.frames.append(self.frame_rolls[:]) |
| 70 | self.frame_rolls = [] |
| 71 | # Regular case with 3 rolls |
| 72 | elif len(self.frame_rolls) == 3: |
| 73 | # Edge Case: Invalid fill balls - check if rolls are valid |
| 74 | if self.frame_rolls[0] != 10: # First roll wasn't a strike |
| 75 | if self.frame_rolls[0] + self.frame_rolls[1] > 10 and self.frame_rolls[0] != 10: |
| 76 | raise ValueError("Pin count exceeds pins on the lane") |
| 77 | if self.frame_rolls[0] + self.frame_rolls[1] < 10 and len(self.frame_rolls) == 3: |
| 78 | raise ValueError("Should not have 3 rolls in 10th frame with open frame") |
| 79 | if self.frame_rolls[0] + self.frame_rolls[1] == 10 and self.frame_rolls[2] > 10: |
| 80 | raise ValueError("Pin count exceeds pins on the lane") |
| 81 | else: # First roll was a strike |
| 82 | if self.frame_rolls[1] != 10 and self.frame_rolls[1] + self.frame_rolls[2] > 10: |
| 83 | raise ValueError("Pin count exceeds pins on the lane") |
| 84 | self.frames.append(self.frame_rolls[:]) |
| 85 | self.frame_rolls = [] |
| 86 | |
| 8 | 87 | def score(self): |
| 9 | | pass |
| 88 | """ |
| 89 | Calculate the total score for the game. |
| 90 | |
| 91 | Returns: |
| 92 | int: The total score for the game |
| 93 | |
| 94 | Raises: |
| 95 | ValueError: If the game is not yet complete |
| 96 | """ |
| 97 | # Edge Case: Game not complete |
| 98 | if len(self.frames) < 10: |
| 99 | raise ValueError("Score cannot be taken until the end of the game") |
| 100 | |
| 101 | total_score = 0 |
| 102 | roll_index = 0 |
| 103 | |
| 104 | for frame_index in range(10): |
| 105 | frame = self.frames[frame_index] |
| 106 | |
| 107 | # First 9 frames |
| 108 | if frame_index < 9: |
| 109 | if len(frame) == 1 and frame[0] == 10: # Strike |
| 110 | total_score += 10 |
| 111 | # Add next two rolls |
| 112 | if len(self.frames[frame_index + 1]) >= 2: |
| 113 | total_score += self.frames[frame_index + 1][0] + self.frames[frame_index + 1][1] |
| 114 | elif len(self.frames[frame_index + 1]) == 1: # Next frame is also a strike |
| 115 | total_score += 10 |
| 116 | if frame_index + 2 < len(self.frames): |
| 117 | if len(self.frames[frame_index + 2]) > 0: |
| 118 | total_score += self.frames[frame_index + 2][0] |
| 119 | elif len(frame) == 2 and sum(frame) == 10: # Spare |
| 120 | total_score += 10 + self.frames[frame_index + 1][0] |
| 121 | else: # Open frame |
| 122 | total_score += sum(frame) |
| 123 | # 10th frame |
| 124 | else: |
| 125 | total_score += sum(frame) |
| 126 | |
| 127 | return total_score |
| 128 | |
| 129 | # Handled Edge Cases: Invalid number of pins, game already completed, frame total exceeds 10, |
| 130 | # invalid fill balls, game not complete |