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
// Swift playground experimenting with payroll. | |
// (I have my reasons.) | |
println("Hello") | |
import Foundation | |
// This stuff sets rounding to two places after decimal | |
var mode = NSRoundingMode.RoundPlain |
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
// wanted a version where i could just use reduce but without building a Frame object ... | |
// expands rolls to include bonus values, e.g. | |
func testExpandRolls() { | |
let game = BowlingGame() | |
game.roll(5,4, 10, 5,5, 6,5) | |
var expanded = game.expandRolls() | |
XCTAssertEqualObjects(expanded, [5,4, 10,5,5, 5,5,6, 6,5], "expanded") | |
} |
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
// might like this as better use of Tuple. Not sure. Return Tuple, use its named elements | |
// code is asking for a frame object, i think, but wanted to try Tuples | |
func frameScore(index:Int) -> (score: Int, step: Int) { | |
if isStrike(index) { | |
return (rolls[index] + twoRolls(index+1), 1) | |
} else if isSpare(index) { | |
return (twoRolls(index) + rolls[index+2], 2) | |
} else { | |
return (twoRolls(index), 2) |
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
func score() -> Int { | |
var sum = 0 | |
var index = 0 | |
for frame in 1...10 { | |
var (score, step) = frameScore(index) | |
sum += score | |
index += step | |
} | |
return sum | |
} |