fishlium

Finished
11class BowlingGame:
22 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
515 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
893 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
Test NameStatus
test_a_spare_followed_by_zeros_is_worth_ten_points
Fail
test_a_spare_in_the_last_frame_gets_a_one_roll_bonus_that_is_counted_once
Fail
test_a_strike_earns_ten_points_in_a_frame_with_a_single_roll
Fail
test_a_strike_in_the_last_frame_gets_a_two_roll_bonus_that_is_counted_once
Fail
test_a_strike_with_the_one_roll_bonus_after_a_spare_in_the_last_frame_does_not_get_a_bonus
Fail
test_all_strikes_is_a_perfect_game
Fail
test_cannot_roll_after_bonus_roll_for_spare
Fail
test_cannot_roll_after_bonus_rolls_for_strike
Fail
test_consecutive_spares_each_get_a_one_roll_bonus
Fail
test_consecutive_strikes_each_get_the_two_roll_bonus
Fail
test_last_two_strikes_followed_by_only_last_bonus_with_non_strike_points
Fail
test_points_scored_in_the_roll_after_a_spare_are_counted_twice
Fail
test_points_scored_in_the_two_rolls_after_a_strike_are_counted_twice_as_a_bonus
Fail
test_rolling_a_spare_with_the_two_roll_bonus_does_not_get_a_bonus_roll
Fail
test_should_be_able_to_score_a_game_with_all_zeros
Fail
test_should_be_able_to_score_a_game_with_no_strikes_or_spares
Fail
test_strikes_with_the_two_roll_bonus_do_not_get_bonus_rolls
Fail
test_two_bonus_rolls_after_a_strike_in_the_last_frame_can_score_more_than_10_points_if_one_is_a_strike
Fail
test_a_roll_cannot_score_more_than_10_points
Pass
test_an_incomplete_game_cannot_be_scored
Pass
test_an_unstarted_game_cannot_be_scored
Pass
test_bonus_roll_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points
Pass
test_bonus_roll_for_a_spare_in_the_last_frame_must_be_rolled_before_score_can_be_calculated
Pass
test_bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_before_score_can_be_calculated
Pass
test_both_bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_before_score_can_be_calculated
Pass
test_cannot_roll_if_game_already_has_ten_frames
Pass
test_rolls_cannot_score_negative_points
Pass
test_second_bonus_roll_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points
Pass
test_the_second_bonus_rolls_after_a_strike_in_the_last_frame_cannot_be_a_strike_if_the_first_one_is_not_a_strike
Pass
test_two_bonus_rolls_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points
Pass
test_two_rolls_in_a_frame_cannot_score_more_than_10_points
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.