fishlium

Finished
11class BowlingGame:
22 def __init__(self):
3 pass
4
3 """
4 Initialize a new bowling game.
5 """
6 self.rolls = []
7 self.frames = []
8 self.current_frame = []
9 self.frame_scores = []
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 ValueError("game is already over")
25
26 # Edge Case: Invalid number of pins (negative or more than 10)
27 if pins < 0 or pins > 10:
28 raise ValueError("invalid number of pins")
29
30 # Edge Case: Too many pins in a frame
31 if self.current_frame and (self.current_frame[0] + pins > 10):
32 raise ValueError("invalid number of pins")
33
34 self.current_frame.append(pins)
35 self.rolls.append(pins)
36
37 # Handle frame completion
38 frame_number = len(self.frames)
39
40 # Regular frames (1-9)
41 if frame_number < 9:
42 # Strike case
43 if len(self.current_frame) == 1 and pins == 10:
44 self.frames.append(self.current_frame[:])
45 self.current_frame = []
46 # Open frame or spare
47 elif len(self.current_frame) == 2:
48 self.frames.append(self.current_frame[:])
49 self.current_frame = []
50 # 10th frame
51 else:
52 # Handle 10th frame special cases
53 if len(self.current_frame) == 1:
54 # First roll was a strike
55 if pins == 10:
56 # Need two more rolls
57 pass
58 else:
59 # Need one more roll to complete frame
60 pass
61 elif len(self.current_frame) == 2:
62 # Second roll in 10th frame
63 frame_sum = self.current_frame[0] + self.current_frame[1]
64 # If it's a strike followed by non-strike
65 if self.current_frame[0] == 10 and self.current_frame[1] < 10:
66 # Need one more roll
67 pass
68 # If it's a spare or open frame
69 elif frame_sum == 10 or frame_sum < 10:
70 self.frames.append(self.current_frame[:])
71 self.current_frame = []
72 self.game_over = True
73 elif len(self.current_frame) == 3:
74 # Third roll in 10th frame
75 self.frames.append(self.current_frame[:])
76 self.current_frame = []
77 self.game_over = True
78
879 def score(self):
9 pass
80 """
81 Calculate the total score for the game.
82
83 Returns:
84 int: The total score of the game
85
86 Raises:
87 ValueError: If the game is not yet complete
88 """
89 # Edge Case: Game is not complete
90 if not self.game_over or self.current_frame:
91 raise ValueError("game is not yet complete")
92
93 total_score = 0
94
95 # Score first 9 frames
96 for i in range(9):
97 # Edge Case: Not enough frames
98 if i >= len(self.frames):
99 raise ValueError("game is not yet complete")
100
101 frame = self.frames[i]
102 frame_score = sum(frame)
103
104 # Strike
105 if len(frame) == 1 and frame[0] == 10:
106 # Add next one or two rolls
107 if i+1 < len(self.frames):
108 next_frame = self.frames[i+1]
109 if len(next_frame) >= 2:
110 frame_score += next_frame[0] + next_frame[1]
111 elif len(next_frame) == 1 and i+2 < len(self.frames):
112 # Next frame is also a strike
113 frame_score += next_frame[0] + self.frames[i+2][0]
114 else:
115 # Edge Case: Not enough rolls to calculate strike bonus
116 raise ValueError("game is not yet complete")
117 # Spare
118 elif len(frame) == 2 and sum(frame) == 10:
119 # Add next one roll
120 if i+1 < len(self.frames):
121 frame_score += self.frames[i+1][0]
122 else:
123 # Edge Case: Not enough rolls to calculate spare bonus
124 raise ValueError("game is not yet complete")
125
126 total_score += frame_score
127
128 # Score 10th frame
129 # Edge Case: Missing 10th frame
130 if len(self.frames) < 10:
131 raise ValueError("game is not yet complete")
132
133 tenth_frame = self.frames[9]
134 total_score += sum(tenth_frame)
135
136 return total_score
137 # Handled Edge Cases: Game already over, invalid pins, too many pins in frame, game not complete, missing frames
Test NameStatus
test_a_spare_in_the_last_frame_gets_a_one_roll_bonus_that_is_counted_once
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_bonus_roll_for_a_spare_in_the_last_frame_must_be_rolled_before_score_can_be_calculated
Fail
test_both_bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_before_score_can_be_calculated
Fail
test_cannot_roll_after_bonus_roll_for_spare
Fail
test_cannot_roll_after_bonus_rolls_for_strike
Fail
test_last_two_strikes_followed_by_only_last_bonus_with_non_strike_points
Fail
test_rolling_a_spare_with_the_two_roll_bonus_does_not_get_a_bonus_roll
Fail
test_second_bonus_roll_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points
Fail
test_strikes_with_the_two_roll_bonus_do_not_get_bonus_rolls
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_can_score_more_than_10_points_if_one_is_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_strike_earns_ten_points_in_a_frame_with_a_single_roll
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_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_consecutive_spares_each_get_a_one_roll_bonus
Pass
test_consecutive_strikes_each_get_the_two_roll_bonus
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_rolls_cannot_score_negative_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_two_rolls_in_a_frame_cannot_score_more_than_10_points
Pass

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