Last active
July 25, 2020 15:35
-
-
Save apatronl/bfe7d8cea035b70676c20012fb406073 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Foundation | |
// Model | |
struct GuessingGame<GuessElement: Equatable> { | |
enum GameStatus { | |
case won | |
case lost | |
case playing | |
} | |
var elementToGuess: GuessElement | |
var maxGuesses: Int | |
private(set) var gameStatus: GameStatus | |
private var numberOfGuesses: Int | |
init(elementToGuess: GuessElement, maxGuesses: Int) { | |
self.elementToGuess = elementToGuess | |
self.maxGuesses = maxGuesses | |
gameStatus = .playing | |
numberOfGuesses = 0 | |
} | |
mutating func makeGuess(with element: GuessElement) { | |
numberOfGuesses += 1 | |
if element == elementToGuess { | |
gameStatus = .won | |
} else if numberOfGuesses >= maxGuesses { | |
gameStatus = .lost | |
} else { | |
gameStatus = .playing | |
} | |
} | |
mutating func reset(with elementToGuess: GuessElement) { | |
self.elementToGuess = elementToGuess | |
numberOfGuesses = 0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment