| 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 game is already over |
| 21 | """ |
| 22 | # Edge Case: Invalid number of pins |
| 23 | if pins < 0: |
| 24 | raise ValueError("Negative roll is invalid") |
| 25 | |
| 26 | # Edge Case: More than 10 pins in a roll |
| 27 | if pins > 10: |
| 28 | raise ValueError("Pin count exceeds pins on the lane") |
| 29 | |
| 30 | # Edge Case: Game already completed |
| 31 | if len(self.frames) == 10 and (len(self.frames[9]) == 2 or |
| 32 | (len(self.frames[9]) == 1 and self.frames[9][0] != 10)): |
| 33 | # Check if we still need fill balls |
| 34 | tenth_frame = self.frames[9] |
| 35 | if len(tenth_frame) == 2 and tenth_frame[0] == 10: # Strike in 10th frame |
| 36 | if len(self.frame_rolls) < 2: # Need 2 fill balls |
| 37 | self.frame_rolls.append(pins) |
| 38 | if len(self.frame_rolls) == 2: |
| 39 | self.frames.append(self.frame_rolls) |
| 40 | return |
| 41 | else: |
| 42 | raise ValueError("Cannot roll after game is over") |
| 43 | elif len(tenth_frame) == 2 and sum(tenth_frame) == 10: # Spare in 10th frame |
| 44 | if len(self.frame_rolls) < 1: # Need 1 fill ball |
| 45 | self.frame_rolls.append(pins) |
| 46 | self.frames.append(self.frame_rolls) |
| 47 | return |
| 48 | else: |
| 49 | raise ValueError("Cannot roll after game is over") |
| 50 | else: |
| 51 | raise ValueError("Cannot roll after game is over") |
| 52 | |
| 53 | # Edge Case: Game already completed (no fill balls needed) |
| 54 | if len(self.frames) == 10: |
| 55 | raise ValueError("Cannot roll after game is over") |
| 56 | |
| 57 | self.frame_rolls.append(pins) |
| 58 | |
| 59 | # Handle frame completion |
| 60 | if len(self.frame_rolls) == 2 or self.frame_rolls[0] == 10: |
| 61 | # Edge Case: Invalid frame (e.g., more than 10 pins in two rolls, except in 10th frame) |
| 62 | if len(self.frame_rolls) == 2 and self.frame_rolls[0] != 10 and sum(self.frame_rolls) > 10: |
| 63 | raise ValueError("Pin count exceeds pins on the lane") |
| 64 | |
| 65 | self.frames.append(self.frame_rolls) |
| 66 | self.current_frame += 1 |
| 67 | self.frame_rolls = [] |
| 68 | |
| 8 | 69 | def score(self): |
| 9 | | pass |
| 70 | """ |
| 71 | Calculate the total score for the game. |
| 72 | |
| 73 | Returns: |
| 74 | int: The total score for the game |
| 75 | |
| 76 | Raises: |
| 77 | ValueError: If the game is not yet complete |
| 78 | """ |
| 79 | # Edge Case: Game not complete |
| 80 | if len(self.frames) < 10: |
| 81 | raise ValueError("Score cannot be taken until the end of the game") |
| 82 | |
| 83 | # Edge Case: 10th frame is incomplete |
| 84 | tenth_frame = self.frames[9] |
| 85 | if len(tenth_frame) == 1 and tenth_frame[0] != 10: |
| 86 | raise ValueError("Score cannot be taken until the end of the game") |
| 87 | if len(tenth_frame) == 2 and tenth_frame[0] == 10 and len(self.frames) < 11: |
| 88 | raise ValueError("Score cannot be taken until the end of the game") |
| 89 | if len(tenth_frame) == 2 and sum(tenth_frame) == 10 and len(self.frames) < 11: |
| 90 | raise ValueError("Score cannot be taken until the end of the game") |
| 91 | |
| 92 | total_score = 0 |
| 93 | |
| 94 | # Score first 9 frames |
| 95 | for i in range(9): |
| 96 | frame = self.frames[i] |
| 97 | frame_score = sum(frame) |
| 98 | |
| 99 | if frame[0] == 10: # Strike |
| 100 | # Add next two rolls |
| 101 | if i+1 < len(self.frames): |
| 102 | next_frame = self.frames[i+1] |
| 103 | if len(next_frame) >= 2: |
| 104 | frame_score += next_frame[0] + next_frame[1] |
| 105 | elif len(next_frame) == 1 and i+2 < len(self.frames): |
| 106 | # Next frame is also a strike |
| 107 | frame_score += next_frame[0] + self.frames[i+2][0] |
| 108 | elif frame_score == 10: # Spare |
| 109 | # Add next roll |
| 110 | if i+1 < len(self.frames): |
| 111 | frame_score += self.frames[i+1][0] |
| 112 | |
| 113 | total_score += frame_score |
| 114 | |
| 115 | # Score 10th frame |
| 116 | tenth_frame_score = sum(tenth_frame) |
| 117 | if len(tenth_frame) > 2: # Fill balls were added |
| 118 | # Add fill ball values |
| 119 | if len(tenth_frame) == 3: |
| 120 | tenth_frame_score = sum(tenth_frame) |
| 121 | |
| 122 | total_score += tenth_frame_score |
| 123 | |
| 124 | return total_score |
| 125 | |
| 126 | # Handled Edge Cases: Invalid number of pins, more than 10 pins in a roll, game already completed, invalid frame, game not complete, 10th frame incomplete |
| 127 | |
| 128 | # Handled Edge Cases: Invalid initialization |
| 129 | # Handled Edge Cases: Invalid number of pins, more than 10 pins in a roll, game already completed, invalid frame, game not complete, 10th frame incomplete |