Skip to content

Instantly share code, notes, and snippets.

@RonJeffries
Created June 9, 2014 02:13
Show Gist options
  • Save RonJeffries/19b2a374baf8bf894b03 to your computer and use it in GitHub Desktop.
Save RonJeffries/19b2a374baf8bf894b03 to your computer and use it in GitHub Desktop.
// 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")
}
// resulting in easy score()
func score() -> Int {
return expandRolls().reduce(0,{$0+$1})
}
// but expand isn't all that nice ...
func expandRolls() -> Int[] {
var result = Int[]()
var i = 0;
while i < rolls.count {
result += rolls[i]
result += rolls[i+1]
if ( rolls[i] == 10 ) {
result += rolls[i+2]
i++
} else if rolls[i] + rolls[i+1] == 10 {
result += rolls[i+2]
i += 2
} else {
i += 2
}
}
return result
}
@vielmetti
Copy link

expandRolls looks like it works until the last frame, which you will need to special case further? e.g. a strike in the first roll of frame 10 needs to be handled properly

@RonJeffries
Copy link
Author

Hi Ed, thanks ...

Well, I haven't tested it here so I could believe it wouldn't work. Offhand, it looks to me as if it might subscript off the end of the array. Mostly what I wanted to know was whether I liked the expand rolls idea. So as soon as I saw what it had to do, I knew I wanted to go another way, and stopped working this thread.

I've not posted the one I went with, which uses a framing object instead. I'll do that if I get a chance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment