Skip to content

Instantly share code, notes, and snippets.

@too
Last active November 14, 2024 20:10
Show Gist options
  • Save too/008c8dd69f68428cfdac to your computer and use it in GitHub Desktop.
Save too/008c8dd69f68428cfdac to your computer and use it in GitHub Desktop.
Bowling Game Kata by Python
# -*- coding: utf-8 -*- #
class Game(object):
def __init__(self):
self.rolls = []
def roll(self, pins):
self.rolls.append(pins)
def total_score(self):
score = 0
roll_index = 0
for frame in range(10):
if self._is_strike(roll_index):
score += 10 + self.rolls[roll_index+1] + self.rolls[roll_index+2]
roll_index += 1
elif self._is_spare(roll_index):
score += 10 + self.rolls[roll_index+2]
roll_index += 2
else:
score += self.rolls[roll_index] + self.rolls[roll_index+1]
roll_index += 2
return score
def _is_spare(self, roll_index):
return self.rolls[roll_index] + self.rolls[roll_index+1] == 10
def _is_strike(self, roll_index):
return self.rolls[roll_index] == 10
# -*- coding: utf-8 -*- #
import unittest
from bowling_game import Game
class GameTest(unittest.TestCase):
def setUp(self):
self.game = Game()
def test_gutter_game(self):
self._roll_many(0, 20)
self.assertEqual(0, self.game.total_score())
def test_all_ones(self):
self._roll_many(1, 20)
self.assertEqual(20, self.game.total_score())
def test_one_spare(self):
self._roll_spare()
self.game.roll(3)
self._roll_many(0, 17)
self.assertEqual(16, self.game.total_score())
def test_one_strike(self):
self.game.roll(10)
self.game.roll(3)
self.game.roll(4)
self._roll_many(0, 16)
self.assertEqual(24, self.game.total_score())
def test_perfect_game(self):
self._roll_many(10, 12)
self.assertEqual(300, self.game.total_score())
def test_simple_game(self):
for pins in [1, 4, 4, 5, 6, 4, 5, 5,
10, 0, 1, 7, 3, 6, 4, 10, 2, 8, 6]:
self.game.roll(pins)
self.assertEqual(133, self.game.total_score())
def _roll_many(self, pins, num):
for i in range(num):
self.game.roll(pins)
def _roll_spare(self):
self.game.roll(5)
self.game.roll(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment