| 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 | 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 pins is negative or greater than 10 |
| 21 | IndexError: If game is already over |
| 22 | """ |
| 23 | # Edge Case: Game already over |
| 24 | if self.game_over: |
| 25 | raise IndexError("Cannot roll after game is over") |
| 26 | |
| 27 | # Edge Case: Invalid pin count |
| 28 | if pins < 0: |
| 29 | raise ValueError("Negative roll is invalid") |
| 30 | |
| 31 | # Edge Case: Too many pins |
| 32 | if pins > 10: |
| 33 | raise ValueError("Pin count exceeds pins on the lane") |
| 34 | |
| 35 | # Edge Case: Too many pins in a frame (except 10th frame special case) |
| 36 | if len(self.frame_rolls) > 0 and self.current_frame < 10: |
| 37 | if self.frame_rolls[0] + pins > 10: |
| 38 | raise ValueError("Pin count exceeds pins on the lane") |
| 39 | |
| 40 | # Add the roll |
| 41 | self.rolls.append(pins) |
| 42 | self.frame_rolls.append(pins) |
| 43 | |
| 44 | # Handle frame completion |
| 45 | if self.current_frame < 10: |
| 46 | # Regular frames |
| 47 | if pins == 10 or len(self.frame_rolls) == 2: |
| 48 | self.frames.append(self.frame_rolls[:]) |
| 49 | self.frame_rolls = [] |
| 50 | self.current_frame += 1 |
| 51 | else: |
| 52 | # 10th frame special handling |
| 53 | # Edge Case: Invalid fill balls |
| 54 | if len(self.frame_rolls) == 3: |
| 55 | # Check if third roll is valid |
| 56 | if self.frame_rolls[0] == 10 and self.frame_rolls[1] < 10 and self.frame_rolls[1] + self.frame_rolls[2] > 10: |
| 57 | raise ValueError("Pin count exceeds pins on the lane") |
| 58 | if self.frame_rolls[0] < 10 and self.frame_rolls[0] + self.frame_rolls[1] < 10: |
| 59 | raise ValueError("invalid fill balls") |
| 60 | self.frames.append(self.frame_rolls[:]) |
| 61 | self.frame_rolls = [] |
| 62 | self.game_over = True |
| 63 | elif len(self.frame_rolls) == 2: |
| 64 | # If not a strike or spare, game is over |
| 65 | if self.frame_rolls[0] + self.frame_rolls[1] < 10: |
| 66 | self.frames.append(self.frame_rolls[:]) |
| 67 | self.frame_rolls = [] |
| 68 | self.game_over = True |
| 69 | elif len(self.frame_rolls) == 1 and self.frame_rolls[0] < 10: |
| 70 | # First roll wasn't a strike, so second roll completes frame if not a spare |
| 71 | pass |
| 72 | |
| 8 | 73 | def score(self): |
| 9 | | pass |
| 74 | """ |
| 75 | Calculate the total score for the game. |
| 76 | |
| 77 | Returns: |
| 78 | int: The total score for the game |
| 79 | |
| 80 | Raises: |
| 81 | IndexError: If the game is not yet complete |
| 82 | """ |
| 83 | # Edge Case: Game not started |
| 84 | if len(self.rolls) == 0: |
| 85 | return 0 |
| 86 | |
| 87 | # Edge Case: Game not complete |
| 88 | if not self.game_over and (len(self.frames) < 10 or (len(self.frames) == 10 and len(self.frame_rolls) > 0)): |
| 89 | raise IndexError("Score cannot be taken until the end of the game") |
| 90 | |
| 91 | # Edge Case: Game not started or incomplete |
| 92 | if len(self.frames) < 10: |
| 93 | raise IndexError("Score cannot be taken until the end of the game") |
| 94 | |
| 95 | total_score = 0 |
| 96 | roll_index = 0 |
| 97 | |
| 98 | for frame_num in range(10): |
| 99 | frame = self.frames[frame_num] |
| 100 | |
| 101 | if frame[0] == 10: # Strike |
| 102 | total_score += 10 |
| 103 | # Add next two rolls |
| 104 | if frame_num == 9: # 10th frame |
| 105 | if len(frame) > 1: |
| 106 | total_score += frame[1] |
| 107 | if len(frame) > 2: |
| 108 | total_score += frame[2] |
| 109 | else: # Regular frame |
| 110 | next_frame = self.frames[frame_num + 1] |
| 111 | if len(next_frame) > 0: |
| 112 | total_score += next_frame[0] |
| 113 | if len(next_frame) > 1: |
| 114 | total_score += next_frame[1] |
| 115 | elif next_frame[0] == 10 and frame_num + 2 < len(self.frames): # Next frame was also a strike |
| 116 | total_score += self.frames[frame_num + 2][0] |
| 117 | elif frame[0] + frame[1] == 10: # Spare |
| 118 | total_score += 10 + (frame[2] if frame_num == 9 else self.frames[frame_num + 1][0]) |
| 119 | else: # Open frame |
| 120 | total_score += frame[0] + frame[1] |
| 121 | |
| 122 | return total_score |
| 123 | # Handled Edge Cases: Game already over, invalid pin count, too many pins, too many pins in a frame, invalid fill balls, game not complete, game not started or incomplete |