tobe

Finished
11class BowlingGame:
22 def __init__(self):
3 pass
4
3 self.rolls = []
4 self.current_roll = 0
5
56 def roll(self, pins):
6 pass
7
7 # Edge Case: Number of pins must be between 0 and 10
8 if pins < 0 or pins > 10:
9 raise ValueError("Pins must be between 0 and 10")
10
11 # Edge Case: Cannot roll after game is over
12 if self.is_game_over():
13 raise ValueError("Cannot roll after game is over")
14
15 self.rolls.append(pins)
16 self.current_roll += 1
17
818 def score(self):
9 pass
19 # Edge Case: Cannot score incomplete game
20 if not self.is_game_over():
21 raise ValueError("Score cannot be taken until the end of the game")
22
23 score = 0
24 roll_index = 0
25
26 for frame in range(10):
27 # Edge Case: Strike (10 pins in first roll)
28 if self.is_strike(roll_index):
29 score += 10 + self.strike_bonus(roll_index)
30 roll_index += 1
31 # Edge Case: Spare (10 pins in two rolls)
32 elif self.is_spare(roll_index):
33 score += 10 + self.spare_bonus(roll_index)
34 roll_index += 2
35 # Edge Case: Open frame (less than 10 pins in two rolls)
36 else:
37 score += self.sum_of_balls_in_frame(roll_index)
38 roll_index += 2
39
40 return score
41
42 def is_game_over(self):
43 if len(self.rolls) < 20:
44 return False
45
46 # Check if we have exactly 20 rolls and they form 10 complete frames
47 roll_index = 0
48 for frame in range(10):
49 if frame < 9: # First 9 frames
50 if self.is_strike(roll_index):
51 roll_index += 1
52 else:
53 roll_index += 2
54 else: # 10th frame
55 # Edge Case: 10th frame with strike or spare needs additional rolls
56 if self.is_strike(roll_index):
57 # Strike in 10th frame - need 2 more rolls
58 return len(self.rolls) >= roll_index + 3
59 elif self.is_spare(roll_index):
60 # Spare in 10th frame - need 1 more roll
61 return len(self.rolls) >= roll_index + 3
62 else:
63 # Open frame in 10th frame - no additional rolls needed
64 return len(self.rolls) >= roll_index + 2
65
66 return True
67
68 def is_strike(self, roll_index):
69 return self.rolls[roll_index] == 10
70
71 def is_spare(self, roll_index):
72 return self.rolls[roll_index] + self.rolls[roll_index + 1] == 10
73
74 def sum_of_balls_in_frame(self, roll_index):
75 return self.rolls[roll_index] + self.rolls[roll_index + 1]
76
77 def spare_bonus(self, roll_index):
78 return self.rolls[roll_index + 2]
79
80 def strike_bonus(self, roll_index):
81 return self.rolls[roll_index + 1] + self.rolls[roll_index + 2]
82
83 # Handled Edge Cases: Number of pins must be between 0 and 10, Cannot roll after game is over, Cannot score incomplete game, Strike (10 pins in first roll), Spare (10 pins in two rolls), Open frame (less than 10 pins in two rolls), 10th frame with strike or spare needs additional rolls
Test NameStatus
test_a_strike_earns_ten_points_in_a_frame_with_a_single_roll
Fail
test_all_strikes_is_a_perfect_game
Fail
test_consecutive_strikes_each_get_the_two_roll_bonus
Fail
test_points_scored_in_the_two_rolls_after_a_strike_are_counted_twice_as_a_bonus
Fail
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_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_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_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_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.