Skip to content

Instantly share code, notes, and snippets.

@balp
Created March 26, 2015 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balp/6a39365fbb9ba278eb74 to your computer and use it in GitHub Desktop.
Save balp/6a39365fbb9ba278eb74 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#
# A Game of python
#
import unittest
class BowlingGame(object):
"""Count Score for a game of bowling.
"""
def __init__(self):
self._rolls = [0] * 21
self._currentIndex = 0
def roll(self, pins):
self._rolls[self._currentIndex] = pins
self._currentIndex += 1
def score(self):
summary = 0
frameIndex = 0
for frame in range(0,10):
if( self._isStrike(frameIndex) ):
summary += 10 + self._frameScore(frameIndex+1)
frameIndex += 1
elif( self._isSpare(frameIndex) ):
summary += 10 + self._rolls[frameIndex+2]
frameIndex += 2
else:
summary += self._frameScore(frameIndex)
frameIndex += 2
return summary
def _isStrike(self, frameIndex):
return 10 == self._rolls[frameIndex]
def _isSpare(self, frameIndex):
return 10 == self._frameScore(frameIndex)
def _frameScore(self, frameIndex):
return self._rolls[frameIndex] + self._rolls[frameIndex+1]
class BowlingGameTest(unittest.TestCase):
def setUp(self):
self._sut = BowlingGame()
def testAllGutterGame(self):
self._rollMany(20, 0)
self.assertEquals(self._sut.score(), 0)
def testAllOneGame(self):
self._rollMany(20, 1)
self.assertEquals(self._sut.score(), 20)
def testOneSpareGame(self):
self._sut.roll(5)
self._sut.roll(5)
self._sut.roll(4)
self._rollMany(17, 0)
self.assertEquals(self._sut.score(), 18)
def testAStrikeGame(self):
self._sut.roll(10)
self._sut.roll(4)
self._sut.roll(3)
self._rollMany(16, 0)
self.assertEquals(self._sut.score(), 24)
def testAPerfectGame(self):
self._rollMany(12, 10)
self.assertEquals(self._sut.score(), 300)
def _rollMany(self, rolls, pins):
for i in range(0,rolls):
self._sut.roll(pins)
if __name__ == "__main__":
unittest.main()
//
// BowlingGame.swift
// BowlingSwift-07
//
// Created by Anders Arnholm on 2015-03-25.
// Copyright (c) 2015 HiQ. All rights reserved.
//
import Foundation
class stuff {
var test = 0
func inc() {
test += 1
}
}
public class BowlingGame {
let myThing = stuff()
private let numberOfFrames = 10
private let maxFrameScore = 10
private var rolls = [Int](count: 21, repeatedValue: 0)
private var currentIndex = 0
public func roll(pins : Int) {
rolls[currentIndex++] = pins
}
public func score() -> Int {
// myThing.inc()
let myThing = stuff()
var summary = 0
var frameIndex = 0
for frame in 1...numberOfFrames {
if ( isStrike(frameIndex)) {
summary += maxFrameScore + frameSummary(frameIndex+1)
frameIndex += 1
} else if ( isSpare(frameIndex) ) {
summary += maxFrameScore + rolls[frameIndex+2]
frameIndex += 2
} else {
summary += frameSummary(frameIndex)
frameIndex += 2
}
}
return summary
}
func frameSummary(frameIndex : Int) -> Int {
return rolls[frameIndex] + rolls[frameIndex+1]
}
func isStrike(frameIndex : Int) -> Bool {
return maxFrameScore == rolls[frameIndex]
}
func isSpare(frameIndex : Int) -> Bool {
return maxFrameScore == rolls[frameIndex] + rolls[frameIndex+1]
}
}
//
// BowlingSwift_07Tests.swift
// BowlingSwift-07Tests
//
// Created by Anders Arnholm on 2015-03-25.
// Copyright (c) 2015 HiQ. All rights reserved.
//
import UIKit
import XCTest
class BowlingSwift_07Tests: XCTestCase {
var sut = BowlingGame()
func testAllGutterGame() {
rollMany(20, pins: 0)
XCTAssertEqual(sut.score(), 0)
}
func testAllOneGame() {
rollMany(20, pins: 1)
XCTAssertEqual(sut.score(), 20)
}
func testASpareGame() {
rollSpare()
sut.roll(4)
rollMany(17, pins: 0)
XCTAssertEqual(sut.score(), 18)
}
func testAStrikeGame() {
rollStrike()
sut.roll(4)
sut.roll(3)
rollMany(16, pins: 0)
XCTAssertEqual(sut.score(), 24)
}
func rollStrike() {
sut.roll(10)
}
func rollSpare() {
sut.roll(5)
sut.roll(5)
}
func testPerfectGame() {
rollMany(12, pins: 10)
XCTAssertEqual(sut.score(), 300)
}
func rollMany(rolls : Int, pins : Int) {
for i in 1...rolls {
sut.roll(pins)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment