| 1 | 1 | class BowlingGame: |
| 2 | 2 | def __init__(self): |
| 3 | | pass |
| 4 | | |
| 3 | """ |
| 4 | Initialize a new bowling game. |
| 5 | |
| 6 | Sets up the game state including tracking rolls, current frame, |
| 7 | and whether the game is complete. |
| 8 | """ |
| 9 | self.rolls = [] |
| 10 | self.frame_rolls = [] |
| 11 | self.frames = [] |
| 12 | self.current_frame = 1 |
| 13 | self.game_complete = False |
| 14 | |
| 5 | 15 | def roll(self, pins): |
| 6 | | pass |
| 7 | | |
| 16 | """ |
| 17 | Record a roll in the bowling game. |
| 18 | |
| 19 | Args: |
| 20 | pins (int): Number of pins knocked down in this roll |
| 21 | |
| 22 | Raises: |
| 23 | ValueError: If pins is negative or greater than 10 |
| 24 | IndexError: If game is already complete |
| 25 | """ |
| 26 | # Edge Case: Game already complete |
| 27 | if self.game_complete: |
| 28 | raise IndexError("Cannot roll after game is over") |
| 29 | |
| 30 | # Edge Case: Invalid pin count |
| 31 | if pins < 0: |
| 32 | raise ValueError("Negative roll is invalid") |
| 33 | |
| 34 | # Edge Case: Invalid pin count |
| 35 | if pins > 10: |
| 36 | raise ValueError("Pin count exceeds pins on the lane") |
| 37 | |
| 38 | # Edge Case: First roll of a frame after a strike in previous frame |
| 39 | if len(self.frame_rolls) == 0 and self.current_frame <= 10: |
| 40 | # This is the start of a new frame |
| 41 | self.frame_rolls = [pins] |
| 42 | |
| 43 | # Edge Case: Strike in frames 1-9 |
| 44 | if pins == 10 and self.current_frame < 10: |
| 45 | self.frames.append(self.frame_rolls) |
| 46 | self.frame_rolls = [] |
| 47 | self.current_frame += 1 |
| 48 | |
| 49 | # Edge Case: Second roll of a frame |
| 50 | elif len(self.frame_rolls) == 1: |
| 51 | first_roll = self.frame_rolls[0] |
| 52 | |
| 53 | # Edge Case: Invalid pin count in second roll |
| 54 | if first_roll + pins > 10 and first_roll < 10: |
| 55 | raise ValueError("Pin count exceeds pins on the lane") |
| 56 | |
| 57 | self.frame_rolls.append(pins) |
| 58 | self.frames.append(self.frame_rolls) |
| 59 | self.frame_rolls = [] |
| 60 | self.current_frame += 1 |
| 61 | |
| 62 | # Edge Case: Tenth frame handling |
| 63 | elif self.current_frame == 10 and len(self.frame_rolls) > 0: |
| 64 | self.frame_rolls.append(pins) |
| 65 | |
| 66 | # Edge Case: Third roll in tenth frame |
| 67 | if len(self.frame_rolls) == 3: |
| 68 | self.frames.append(self.frame_rolls) |
| 69 | self.game_complete = True |
| 70 | |
| 71 | # Edge Case: Invalid state |
| 72 | else: |
| 73 | raise ValueError("Invalid game state") |
| 74 | |
| 75 | # Edge Case: Game completion after tenth frame |
| 76 | if self.current_frame > 10 and not self.game_complete: |
| 77 | if len(self.frame_rolls) == 2: |
| 78 | # Check if we need a third roll |
| 79 | if self.frame_rolls[0] == 10 or sum(self.frame_rolls) == 10: |
| 80 | # Need a third roll |
| 81 | pass |
| 82 | else: |
| 83 | # No third roll needed |
| 84 | self.frames.append(self.frame_rolls) |
| 85 | self.game_complete = True |
| 86 | elif len(self.frame_rolls) == 3: |
| 87 | self.frames.append(self.frame_rolls) |
| 88 | self.game_complete = True |
| 89 | |
| 90 | # Keep track of all rolls for scoring calculation |
| 91 | self.rolls.append(pins) |
| 92 | |
| 8 | 93 | def score(self): |
| 9 | | pass |
| 94 | """ |
| 95 | Calculate the total score for the bowling game. |
| 96 | |
| 97 | Returns: |
| 98 | int: The total score of the game |
| 99 | |
| 100 | Raises: |
| 101 | IndexError: If the game is not yet complete |
| 102 | """ |
| 103 | # Edge Case: Game not complete |
| 104 | if not self.game_complete: |
| 105 | raise IndexError("Score cannot be taken until the end of the game") |
| 106 | |
| 107 | total_score = 0 |
| 108 | roll_index = 0 |
| 109 | |
| 110 | # Edge Case: Scoring for frames 1-9 |
| 111 | for frame_index in range(10): |
| 112 | # Edge Case: Strike frame |
| 113 | if self.rolls[roll_index] == 10: # Strike |
| 114 | total_score += 10 + self.rolls[roll_index + 1] + self.rolls[roll_index + 2] |
| 115 | roll_index += 1 |
| 116 | # Edge Case: Spare frame |
| 117 | elif self.rolls[roll_index] + self.rolls[roll_index + 1] == 10: # Spare |
| 118 | total_score += 10 + self.rolls[roll_index + 2] |
| 119 | roll_index += 2 |
| 120 | # Edge Case: Open frame |
| 121 | else: # Open frame |
| 122 | total_score += self.rolls[roll_index] + self.rolls[roll_index + 1] |
| 123 | roll_index += 2 |
| 124 | |
| 125 | return total_score |
| 126 | |
| 127 | # Handled Edge Cases: Game already complete, invalid pin count, strike in frames 1-9, |
| 128 | # second roll of frame, invalid pin count in second roll, tenth frame handling, |
| 129 | # third roll in tenth frame, invalid state, game completion after tenth frame |
| 130 | |
| 131 | # Handled Edge Cases: Game not complete, strike frame, spare frame, open frame |