Skip to content

Instantly share code, notes, and snippets.

@RonJeffries
Created June 8, 2014 15:01
Show Gist options
  • Save RonJeffries/b0ce26229e668f122fc8 to your computer and use it in GitHub Desktop.
Save RonJeffries/b0ce26229e668f122fc8 to your computer and use it in GitHub Desktop.
Swift Lang tuples another way
// 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)
}
}
func score() -> Int {
var sum = 0
var index = 0
for frame in 1...10 {
let frameInfo = frameScore(index)
sum += frameInfo.score
index += frameInfo.step
}
return sum
}
@jbrains
Copy link

jbrains commented Jun 8, 2014

Use the tuples in score, why not?

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