Skip to content

Instantly share code, notes, and snippets.

@bobdel
Created June 6, 2020 00:52
Show Gist options
  • Select an option

  • Save bobdel/d14ccb7c37676e9e78186c2297108cb7 to your computer and use it in GitHub Desktop.

Select an option

Save bobdel/d14ccb7c37676e9e78186c2297108cb7 to your computer and use it in GitHub Desktop.
Bonus Time code for Stanford CS193p Spring 2020 Memorize animation lecture
// place this code block inside the Card struct
// MARK: - Bonus Time
// this could give matching bonus points
// if the user matches the card
// before a certain amount of time passes during which the card is face up
// can be zero which means "no bonus available" for this card
var bonusTimeLimit: TimeInterval = 6
// how long this card has ever been face up
private var faceUpTime: TimeInterval {
if let lastFaceUpDate = self.lastFaceUpDate {
return pastFaceUpTime + Date().timeIntervalSince(lastFaceUpDate)
} else {
return pastFaceUpTime
}
}
// the last time this card was turned face up (and is still face up)
var lastFaceUpDate: Date?
// the accumulated time this card has been face up in the past
// (i.e. not including the current time it's been face up if it is currently so)
var pastFaceUpTime: TimeInterval = 0
// how much time left before the bonus opportunity runs out
var bonusTimeRemaining: TimeInterval {
max(0, bonusTimeLimit - faceUpTime)
}
// percentage of the bonus time remaining
var bonusRemaining: Double {
(bonusTimeLimit > 0 && bonusTimeRemaining > 0) ? bonusTimeRemaining/bonusTimeLimit : 0
}
// whether the card was matched during the bonus time period
var hasEarnedBonus: Bool {
isMatched && bonusTimeRemaining > 0
}
// whether we are currently face up, unmatched and have not yet used up the bonus window
var isConsumingBonusTime: Bool {
isFaceUp && !isMatched && bonusTimeRemaining > 0
}
// called when the card transitions to face up state
private mutating func startUsingBonusTime() {
if isConsumingBonusTime, lastFaceUpDate == nil {
lastFaceUpDate = Date()
}
}
// called when the card goes back face down (or gets matched)
private mutating func stopUsingBonusTime() {
pastFaceUpTime = faceUpTime
self.lastFaceUpDate = nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment