harrison

Finished
11class BowlingGame:
22 def __init__(self):
3 pass
4
3 """
4 Initialize a new bowling game.
5 """
6 self.rolls = []
7
58 def roll(self, pins):
6 pass
7
9 """
10 Record a roll in the game.
11
12 Args:
13 pins (int): Number of pins knocked down in this roll
14
15 Raises:
16 ValueError: If pins is negative or greater than 10
17 ValueError: If game is already over
18 """
19 # Validate pins
20 if pins < 0:
21 raise ValueError("Pins must be between 0 and 10")
22 if pins > 10:
23 raise ValueError("Pins must be between 0 and 10")
24
25 # Check if game is already over
26 if self._is_game_complete():
27 raise ValueError("Game is already complete")
28
29 self.rolls.append(pins)
30
831 def score(self):
9 pass
32 """
33 Calculate the total score for the game.
34
35 Returns:
36 int: The total score for the game
37
38 Raises:
39 ValueError: If the game is not yet complete
40 """
41 if not self._is_game_complete():
42 raise ValueError("Game is not yet complete")
43
44 total_score = 0
45 roll_index = 0
46
47 # Score first 9 frames
48 for frame in range(9):
49 if roll_index >= len(self.rolls):
50 raise ValueError("Game is not yet complete")
51
52 # Strike
53 if self.rolls[roll_index] == 10:
54 total_score += 10 + self.rolls[roll_index + 1] + self.rolls[roll_index + 2]
55 roll_index += 1
56 # Spare
57 elif self.rolls[roll_index] + self.rolls[roll_index + 1] == 10:
58 total_score += 10 + self.rolls[roll_index + 2]
59 roll_index += 2
60 # Open frame
61 else:
62 total_score += self.rolls[roll_index] + self.rolls[roll_index + 1]
63 roll_index += 2
64
65 # Score 10th frame
66 while roll_index < len(self.rolls):
67 total_score += self.rolls[roll_index]
68 roll_index += 1
69
70 return total_score
71
72 def _is_game_complete(self):
73 """
74 Check if the game is complete.
75
76 Returns:
77 bool: True if the game is complete, False otherwise
78 """
79 if len(self.rolls) < 12: # Minimum rolls needed (9 open frames + 3 rolls in 10th frame)
80 return False
81
82 roll_index = 0
83 frame_count = 0
84
85 # Check first 9 frames
86 while frame_count < 9 and roll_index < len(self.rolls):
87 if self.rolls[roll_index] == 10: # Strike
88 roll_index += 1
89 else:
90 roll_index += 2
91 frame_count += 1
92
93 # If we haven't completed 9 frames, game is not complete
94 if frame_count < 9:
95 return False
96
97 # Check 10th frame
98 if roll_index >= len(self.rolls):
99 return False
100
101 # 10th frame: strike gets 2 more rolls
102 if self.rolls[roll_index] == 10:
103 return roll_index + 3 <= len(self.rolls)
104 # 10th frame: spare gets 1 more roll
105 elif roll_index + 1 < len(self.rolls) and self.rolls[roll_index] + self.rolls[roll_index + 1] == 10:
106 return roll_index + 3 <= len(self.rolls)
107 # 10th frame: open frame needs 2 rolls
108 else:
109 return roll_index + 2 <= len(self.rolls)
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_two_rolls_in_a_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

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