Created
November 29, 2016 05:12
-
-
Save DonerKebab/811d0b36a896d8f2a9315249811e4803 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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