Skip to content

Instantly share code, notes, and snippets.

@jasonnoble
Created February 12, 2016 01:39
Show Gist options
  • Select an option

  • Save jasonnoble/75a380adfe3200e68cb5 to your computer and use it in GitHub Desktop.

Select an option

Save jasonnoble/75a380adfe3200e68cb5 to your computer and use it in GitHub Desktop.
class Player {
var name:String
var activeRound:Round
init(name: String) {
self.name = name
self.activeRound = Round()
}
}
class Round {
var scores:[Int:HoleScore]
init() {
self.scores = [Int:HoleScore]()
}
func recordStrokes(strokes:Int, forHole hole:Int) -> Void {
self.scores[hole] = HoleScore(strokes)
}
func isComplete() -> Bool {
return scores.values.count == 18
}
func totalScore() -> Int {
var sum = 0
for score in scores.values {
sum += score.strokeCount
}
return sum
}
}
class HoleScore {
var strokeCount:Int
init(_ strokes:Int) {
self.strokeCount = strokes
}
}
let me = Player(name: "Brooks")
me.activeRound.isComplete()
me.activeRound.totalScore()
me.activeRound.recordStrokes(4, forHole: 1)
me.activeRound.totalScore()
for holeNumber in 2...18 {
me.activeRound.recordStrokes(5, forHole: holeNumber)
}
me.activeRound.totalScore()
me.activeRound.isComplete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment