Day 1 Part 1
Day 1 Part 2
Day 1 Part 3
Day 2 Part 1
Day 2 Part 2
Day 2 Part 3
Day 2 Part 4
import unittest
import Game
class TestGame(unittest.TestCase):
def setUp(self):
self.game = Game.Game()
def testGutterGameHasZeroScore(self):
for i in range(20):
self.game.roll(0)
assert self.game.score() == 0, "Score is zero"
def testGameAllOnesHasScoreOfTwenty(self):
for i in range(20):
self.game.roll(1)
assert self.game.score() == 20
def testGameThrowsExceptionWithMoreThanTwentyOneRolls(self):
try:
for i in range(22):
self.game.roll(10)
except ValueError:
pass
else:
self.fail("Expected a ValueError")
def testGameWithOneSpare(self):
self.game.roll(1)
self.game.roll(9)
self.game.roll(5)
for i in range(17):
self.game.roll(0)
assert self.game.score() == 20
def testGameWithOneStrike(self):
self.game.roll(10)
self.game.roll(1)
self.game.roll(1)
for i in range(17):
self.game.roll(0)
assert self.game.score() == 14
def testPerfectGameScores300(self):
for i in range(12):
self.game.roll(10)
assert self.game.score() == 300, "Perfect Game should score 300"
if __name__ == "__main__":
unittest.main()
MAX_ROLLS = 21
FRAMES = 10
STRIKE = 10
SPARE = 10
class Game:
def __init__(self):
self.scoreArr = []
def roll(self, pins):
if self.numberOfRollsTaken() > MAX_ROLLS:
raise ValueError
self.scoreArr.append(pins)
def score(self):
score = 0
currentRoll = 0
frame = 0
while frame < FRAMES:
if self.scoreArr[currentRoll] == STRIKE:
score += self.scoreNextTwoBalls(currentRoll)
currentRoll += 1
elif self.frameScore(currentRoll) == SPARE:
score += self.scoreNextTwoBalls(currentRoll)
currentRoll += 2
else:
score += self.frameScore(currentRoll)
currentRoll += 2
frame = frame + 1
return score
def numberOfRollsTaken(self):
return len(self.scoreArr) +1
def scoreNextTwoBalls(self, index):
return self.scoreArr[index] + self.scoreArr[index + 1] + self.scoreArr[index + 2]
def frameScore(self, index):
return self.scoreArr[index] + self.scoreArr[index + 1]
A couple of things that I learned while doing this Bowling Game kata:
def testGameHasScoreOfOneWithFirstRollOfOnePin(self):
game = Game()
game.roll(1)
assert game.score == 1
At this point the tests no longer pass because the Game class does not have a roll method. My first attempt to add the roll method to my Game class failed:def roll(pins):
score += pins
Here is the error that was printed:def roll(self, pins):
self.score += pins
Both of my tests now pass:import unittest
class TestGame(unittest.TestCase):
pass
if __name__ == "__main__":
unittest.main()and my First Test:import unittest
class TestGame(unittest.TestCase):
def testGameHasZeroScore(self):
game = Game()
assert game.score == 0, "Score is zero"
if __name__ == "__main__":
unittest.main()
At this point I am still not certain what all of this is doing. Running the test produces this output:class Game:
score = 0
import unittest
from Game import Game
class TestGame(unittest.TestCase):
def testGameHasZeroScore(self):
game = Game()
assert game.score == 0, "Score is zero"
if __name__ == "__main__":
unittest.main()