From 26f824fd06634b2827dae7e6f6706a8f80c29ed1 Mon Sep 17 00:00:00 2001 From: didier amyot Date: Wed, 20 Dec 2023 14:46:07 -0500 Subject: [PATCH] added bowling example --- tdd/bowling.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ tdd/bowling_test.py | 27 +++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 tdd/bowling.py create mode 100644 tdd/bowling_test.py diff --git a/tdd/bowling.py b/tdd/bowling.py new file mode 100644 index 0000000..4b01d2a --- /dev/null +++ b/tdd/bowling.py @@ -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]) diff --git a/tdd/bowling_test.py b/tdd/bowling_test.py new file mode 100644 index 0000000..a7d8f9b --- /dev/null +++ b/tdd/bowling_test.py @@ -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