Skip to content

Instantly share code, notes, and snippets.

@DonerKebab
Created November 29, 2016 05:12
Show Gist options
  • Save DonerKebab/811d0b36a896d8f2a9315249811e4803 to your computer and use it in GitHub Desktop.
Save DonerKebab/811d0b36a896d8f2a9315249811e4803 to your computer and use it in GitHub Desktop.
import unittest
class Game(object):
def __init__(self):
self.rolls = [0] * 21
self.currentRoll = 0
def roll(self, pins):
self.rolls[self.currentRoll] = pins
self.currentRoll += 1
def caculateTotalScore(self):
score = 0
frameIndex = 0
for i in range(0, 10):
if self.isStrike(frameIndex):
score += 10 + self.strikeBonus(frameIndex)
frameIndex += 1
elif self.isSpare(frameIndex):
score += 10 + self.spareBonus(frameIndex)
frameIndex += 2
else:
score += self.caculateScoreOfFrame(frameIndex)
frameIndex += 2
return score
def isStrike(self, frameIndex):
return self.rolls[frameIndex] == 10
def isSpare(self, frameIndex):
return self.rolls[frameIndex] + self.rolls[frameIndex + 1] == 10
def caculateScoreOfFrame(self, frameIndex):
return self.rolls[frameIndex] + self.rolls[frameIndex + 1]
def spareBonus(self, frameIndex):
return self.rolls[frameIndex + 2]
def strikeBonus(self, frameIndex):
return self.rolls[frameIndex + 1] + self.rolls[frameIndex + 2]
class BownlingGameTest(unittest.TestCase):
def test_game(self):
pins = [1,4,4,5,6,4, 5,5,10,0,1,7,3,6,4,10,2,8,6]
game = Game()
for pin in pins:
game.roll(pin)
result = game.caculateTotalScore()
self.assertEqual(result, 133)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment