Skip to content

Commit 1785b1f

Browse files
committed
bowling: tested and implemeneted first 14 tests
1 parent 4527240 commit 1785b1f

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

exercises/bowling/bowling_test.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,62 @@ def test_consecutive_stikes_each_get_the_two_roll_bonus(self):
103103

104104
self.assertEqual(score, 81)
105105

106+
def test_strike_in_last_frame_gets_two_roll_bonus_counted_once(self):
107+
rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 1]
108+
109+
self.game = BowlingGame()
110+
111+
for roll in rolls:
112+
self.game.roll(roll)
113+
score = self.game.score()
114+
115+
self.assertEqual(score, 18)
116+
117+
def test_rolling_spare_with_bonus_roll_does_not_get_bonus(self):
118+
rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 3]
119+
120+
self.game = BowlingGame()
121+
122+
for roll in rolls:
123+
self.game.roll(roll)
124+
score = self.game.score()
125+
126+
self.assertEqual(score, 20)
127+
128+
def test_strikes_with_the_two_bonus_rolls_do_not_get_bonus_rolls(self):
129+
rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10]
130+
131+
self.game = BowlingGame()
132+
133+
for roll in rolls:
134+
self.game.roll(roll)
135+
score = self.game.score()
136+
137+
self.assertEqual(score, 30)
138+
139+
def test_strike_with_bonus_after_spare_in_last_frame_gets_no_bonus(self):
140+
rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 10]
141+
142+
self.game = BowlingGame()
143+
144+
for roll in rolls:
145+
self.game.roll(roll)
146+
score = self.game.score()
147+
148+
self.assertEqual(score, 20)
149+
150+
def test_all_strikes_is_a_perfect_game(self):
151+
152+
rolls = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
153+
154+
self.game = BowlingGame()
155+
156+
for roll in rolls:
157+
self.game.roll(roll)
158+
score = self.game.score()
159+
160+
self.assertEqual(score, 300)
161+
106162

107163
if __name__ == '__main__':
108164
unittest.main()

exercises/bowling/example.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,31 @@ def roll(self, pins):
1313

1414
def score(self):
1515
roll_index = 0
16+
frame_index = 1
1617

17-
while (roll_index < len(self.rolls)-1):
18-
19-
#get the two rolls that make up a frame
18+
while (frame_index <= NUM_FRAMES):
19+
# get the two rolls that make up a frame
2020
roll1 = self.rolls[roll_index]
2121
if(not self.isLastFrame(roll_index)):
22-
roll2 = self.rolls[roll_index + 1]
22+
roll2 = self.rolls[roll_index + 1]
2323
else:
2424
roll2 = 0
2525

2626
if (self.isStrike(roll1)):
2727
self.totalScore += roll1 + self.stikeBonus(roll_index)
28-
#frame should only advance by 1 roll if it was a strike
28+
# frame should only advance by 1 roll if it was a strike
2929
roll_index += 1
3030
else:
3131
if(self.isSpare(roll1, roll2)):
32-
self.totalScore += roll1 + roll2 + self.spareBonus(roll_index)
32+
self.totalScore += roll1 + roll2 + \
33+
self.spareBonus(roll_index)
3334
else:
3435
self.totalScore += roll1 + roll2
35-
#frame should only advance by 2 rolls normally
36+
# frame should advance by 2 rolls normally
3637
roll_index += 2
3738

39+
frame_index += 1
40+
3841
return self.totalScore
3942

4043
def isStrike(self, pins):

0 commit comments

Comments
 (0)