couple

Finished
11class BowlingGame:
22 def __init__(self):
3 pass
4
3 """
4 Initialize a new bowling game.
5 """
6 self.rolls = []
7 self.current_frame = 0
8 self.current_frame_rolls = []
9 self.game_over = False
10
511 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 or pins > 10:
24 raise ValueError("Pins must be between 0 and 10")
25
26 # Edge Case: Game already completed
27 if self.game_over:
28 raise ValueError("Cannot roll after game is over")
29
30 # Add the roll
31 self.rolls.append(pins)
32 self.current_frame_rolls.append(pins)
33
34 # Handle regular frames (0-8)
35 if self.current_frame < 9:
36 # Strike - frame is complete
37 if pins == 10:
38 self.current_frame += 1
39 self.current_frame_rolls = []
40 # Second roll of frame
41 elif len(self.current_frame_rolls) == 2:
42 # Edge Case: Frame total exceeds 10
43 if sum(self.current_frame_rolls) > 10:
44 raise ValueError("Cannot knock down more than 10 pins in a frame")
45 self.current_frame += 1
46 self.current_frame_rolls = []
47 # Handle 10th frame
48 else:
49 # Validate frame totals in 10th frame
50 if len(self.current_frame_rolls) == 2:
51 # If first roll is a strike, no validation needed for second roll
52 if self.current_frame_rolls[0] == 10:
53 # Game continues for third roll
54 pass
55 # If first two rolls are not a strike or spare, game is over
56 elif sum(self.current_frame_rolls) < 10:
57 self.game_over = True
58 # If it's a spare, game continues for third roll
59 elif sum(self.current_frame_rolls) == 10:
60 pass
61 # If first two rolls exceed 10 (and first wasn't a strike), invalid
62 else:
63 raise ValueError("Cannot knock down more than 10 pins in a frame")
64 elif len(self.current_frame_rolls) == 3:
65 # Validate third roll in 10th frame
66 if self.current_frame_rolls[0] == 10:
67 # After a strike, second and third rolls can be anything
68 pass
69 elif self.current_frame_rolls[0] + self.current_frame_rolls[1] == 10:
70 # After a spare, third roll can be anything
71 pass
72 # Game is definitely over after 3 rolls in 10th frame
73 self.game_over = True
74
875 def score(self):
9 pass
76 """
77 Calculate the total score for the game.
78
79 Returns:
80 int: Total score for the game
81
82 Raises:
83 ValueError: If game is not yet complete
84 """
85 # Edge Case: Game not complete
86 if not self.game_over:
87 raise ValueError("Game is not yet finished")
88
89 total_score = 0
90 roll_index = 0
91
92 # Calculate score for frames 1-9
93 for frame in range(9):
94 # Strike
95 if self.rolls[roll_index] == 10:
96 # Add 10 plus next two rolls
97 total_score += 10 + self.rolls[roll_index + 1] + self.rolls[roll_index + 2]
98 roll_index += 1
99 # Spare
100 elif self.rolls[roll_index] + self.rolls[roll_index + 1] == 10:
101 # Add 10 plus next roll
102 total_score += 10 + self.rolls[roll_index + 2]
103 roll_index += 2
104 # Open frame
105 else:
106 total_score += self.rolls[roll_index] + self.rolls[roll_index + 1]
107 roll_index += 2
108
109 # Handle 10th frame - just sum all remaining rolls
110 while roll_index < len(self.rolls):
111 total_score += self.rolls[roll_index]
112 roll_index += 1
113
114 return total_score
115
116 # Handled Edge Cases: Invalid number of pins, game already completed, tenth frame special handling, invalid fill balls, frame total exceeds 10, game not complete
Test NameStatus
test_the_second_bonus_rolls_after_a_strike_in_the_last_frame_cannot_be_a_strike_if_the_first_one_is_not_a_strike
Fail
test_two_bonus_rolls_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points
Fail
test_a_roll_cannot_score_more_than_10_points
Pass
test_a_spare_followed_by_zeros_is_worth_ten_points
Pass
test_a_spare_in_the_last_frame_gets_a_one_roll_bonus_that_is_counted_once
Pass
test_a_strike_earns_ten_points_in_a_frame_with_a_single_roll
Pass
test_a_strike_in_the_last_frame_gets_a_two_roll_bonus_that_is_counted_once
Pass
test_a_strike_with_the_one_roll_bonus_after_a_spare_in_the_last_frame_does_not_get_a_bonus
Pass
test_all_strikes_is_a_perfect_game
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_after_bonus_roll_for_spare
Pass
test_cannot_roll_after_bonus_rolls_for_strike
Pass
test_cannot_roll_if_game_already_has_ten_frames
Pass
test_consecutive_spares_each_get_a_one_roll_bonus
Pass
test_consecutive_strikes_each_get_the_two_roll_bonus
Pass
test_last_two_strikes_followed_by_only_last_bonus_with_non_strike_points
Pass
test_points_scored_in_the_roll_after_a_spare_are_counted_twice
Pass
test_points_scored_in_the_two_rolls_after_a_strike_are_counted_twice_as_a_bonus
Pass
test_rolling_a_spare_with_the_two_roll_bonus_does_not_get_a_bonus_roll
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_should_be_able_to_score_a_game_with_all_zeros
Pass
test_should_be_able_to_score_a_game_with_no_strikes_or_spares
Pass
test_strikes_with_the_two_roll_bonus_do_not_get_bonus_rolls
Pass
test_two_bonus_rolls_after_a_strike_in_the_last_frame_can_score_more_than_10_points_if_one_is_a_strike
Pass
test_two_rolls_in_a_frame_cannot_score_more_than_10_points
Pass

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