Kratos

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 = 1
8 self.frame_rolls = []
9 self.frames = []
10 self.game_over = False
11
512 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 the number of pins is invalid or game is already over
21 """
22 # Edge Case: Game is already over
23 if self.game_over:
24 raise IndexError("Game is already complete")
25
26 # Edge Case: Invalid number of pins (negative or more than 10)
27 if pins < 0 or pins > 10:
28 raise ValueError("Pins must be between 0 and 10")
29
30 # Edge Case: Too many pins in a frame (except for tenth frame special cases)
31 if len(self.frame_rolls) == 1:
32 # For tenth frame, we allow up to 10 pins on second roll regardless of first roll
33 if self.current_frame == 10:
34 # If first roll was a strike, second roll can be up to 10
35 if self.frame_rolls[0] == 10:
36 pass # Allow any number of pins (0-10) on second roll
37 # If first two rolls sum to 10, it's a spare, third roll is allowed
38 elif self.frame_rolls[0] + pins > 10:
39 raise ValueError("Cannot knock down more than 10 pins in a frame")
40 # For other frames, sum of two rolls cannot exceed 10
41 elif self.frame_rolls[0] + pins > 10:
42 raise ValueError("Cannot knock down more than 10 pins in a frame")
43
44 self.frame_rolls.append(pins)
45 self.rolls.append(pins)
46
47 # Handle frame completion
48 if self.current_frame < 10:
49 # Regular frames
50 if pins == 10 or len(self.frame_rolls) == 2:
51 self.frames.append(self.frame_rolls[:])
52 self.frame_rolls = []
53 self.current_frame += 1
54 else:
55 # Tenth frame
56 # Strike case: need 2 more rolls
57 if len(self.frames) == 9 and (len(self.frame_rolls) == 3 or
58 (len(self.frame_rolls) == 2 and self.frame_rolls[0] != 10 and sum(self.frame_rolls) < 10)):
59 self.frames.append(self.frame_rolls[:])
60 self.frame_rolls = []
61 self.game_over = True
62 # Non-strike case: if we have 2 rolls and it's not a spare, game over
63 elif len(self.frame_rolls) == 2 and sum(self.frame_rolls) < 10:
64 self.frames.append(self.frame_rolls[:])
65 self.frame_rolls = []
66 self.game_over = True
67 # Spare case: need 1 more roll
68 elif len(self.frame_rolls) == 3:
69 self.frames.append(self.frame_rolls[:])
70 self.frame_rolls = []
71 self.game_over = True
72
873 def score(self):
9 pass
74 """
75 Calculate the total score for the game.
76
77 Returns:
78 int: The total score of the game
79
80 Raises:
81 ValueError: If the game is not yet complete
82 """
83 # Edge Case: Game is not complete
84 if not self.game_over or len(self.frames) < 10:
85 raise IndexError("Game is not yet complete")
86
87 total_score = 0
88 roll_index = 0
89
90 # Score first 9 frames
91 for frame_index in range(9):
92 frame = self.frames[frame_index]
93
94 if len(frame) == 1 and frame[0] == 10: # Strike
95 total_score += 10 + self._next_two_balls(frame_index)
96 elif sum(frame) == 10: # Spare
97 total_score += 10 + self._next_ball(frame_index)
98 else: # Open frame
99 total_score += sum(frame)
100
101 # Score 10th frame
102 tenth_frame = self.frames[9]
103 total_score += sum(tenth_frame)
104
105 return total_score
106
107 def _next_ball(self, frame_index):
108 """
109 Get the number of pins from the first roll of the next frame.
110
111 Args:
112 frame_index (int): Index of the current frame
113
114 Returns:
115 int: Number of pins from the next ball
116 """
117 if frame_index + 1 < len(self.frames):
118 next_frame = self.frames[frame_index + 1]
119 if len(next_frame) > 0:
120 return next_frame[0]
121 return 0
122
123 def _next_two_balls(self, frame_index):
124 """
125 Get the number of pins from the next two rolls.
126
127 Args:
128 frame_index (int): Index of the current frame
129
130 Returns:
131 int: Sum of pins from the next two balls
132 """
133 pins = 0
134
135 if frame_index + 1 < len(self.frames):
136 next_frame = self.frames[frame_index + 1]
137
138 if len(next_frame) >= 2:
139 pins += next_frame[0] + next_frame[1]
140 elif len(next_frame) == 1:
141 pins += next_frame[0]
142 if frame_index + 2 < len(self.frames) and len(self.frames[frame_index + 2]) > 0:
143 pins += self.frames[frame_index + 2][0]
144
145 return pins
146
147 # Handled Edge Cases: Game is already over, invalid number of pins, too many pins in a frame, game is 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.