added bowling example
This commit is contained in:
1 parent
6a6351c2ef
commit
26f824fd06
2 files changed
+72
No files matched your search
@@ -0,0 +1,45 @@
|
||||
|
||||
class BowlingCase:
|
||||
def __init__(self):
|
||||
self.first_throw = None
|
||||
self.second_throw = None
|
||||
self.xtra_throw = None
|
||||
|
||||
def add_score(self, pins_down):
|
||||
if self.first_throw is None:
|
||||
self.first_throw = pins_down
|
||||
else:
|
||||
self.second_throw = pins_down
|
||||
|
||||
def is_completed(self):
|
||||
if self.first_throw == 10:
|
||||
return True
|
||||
return isinstance(self.first_throw, int) and isinstance(self.second_throw, int)
|
||||
|
||||
def is_spare(self):
|
||||
return self.first_throw != 10 and self.score() == 10
|
||||
|
||||
def requires_extra_throw(self):
|
||||
if self.is_spare():
|
||||
return self.xtra_throw is None
|
||||
return False
|
||||
|
||||
def set_extra_throw(self, throw):
|
||||
self.xtra_throw = throw
|
||||
|
||||
def score(self):
|
||||
return (self.first_throw or 0) + (self.second_throw or 0)
|
||||
|
||||
|
||||
class BowlingGame:
|
||||
def __init__(self):
|
||||
self._score = [BowlingCase() for i in range(10)]
|
||||
|
||||
def throw(self, pin_downs):
|
||||
for case in self._score:
|
||||
if not case.is_completed():
|
||||
case.add_score(pin_downs)
|
||||
return
|
||||
|
||||
def score(self):
|
||||
return sum([case.score() for case in self._score])
|
||||
@@ -0,0 +1,27 @@
|
||||
from tdd.bowling import BowlingGame
|
||||
|
||||
|
||||
def test_new_game():
|
||||
bowling_game = BowlingGame()
|
||||
|
||||
|
||||
def test_new_game_trivial():
|
||||
bowling_game = BowlingGame()
|
||||
bowling_game.throw(5)
|
||||
assert bowling_game.score() == 5
|
||||
|
||||
|
||||
def test_new_game_trivial_2():
|
||||
bowling_game = BowlingGame()
|
||||
bowling_game.throw(5)
|
||||
bowling_game.throw(3)
|
||||
assert bowling_game.score() == 8
|
||||
|
||||
|
||||
def test_new_game_trivial_3():
|
||||
bowling_game = BowlingGame()
|
||||
bowling_game.throw(6)
|
||||
bowling_game.throw(4)
|
||||
|
||||
bowling_game.throw(3)
|
||||
assert bowling_game.score() == 16
|
||||
Reference in new issue
Block a user