diff --git a/02-guess-the-flag/Guess the Flag/Guess the Flag/Base.lproj/Main.storyboard b/02-guess-the-flag/Guess the Flag/Guess the Flag/Base.lproj/Main.storyboard
index d7893f1..549968b 100644
--- a/02-guess-the-flag/Guess the Flag/Guess the Flag/Base.lproj/Main.storyboard
+++ b/02-guess-the-flag/Guess the Flag/Guess the Flag/Base.lproj/Main.storyboard
@@ -24,21 +24,30 @@
+
+
+
-
+
+
+
+
-
+
+
+
+
diff --git a/02-guess-the-flag/Guess the Flag/Guess the Flag/ViewController.swift b/02-guess-the-flag/Guess the Flag/Guess the Flag/ViewController.swift
index a5bfdc3..d3528de 100644
--- a/02-guess-the-flag/Guess the Flag/Guess the Flag/ViewController.swift
+++ b/02-guess-the-flag/Guess the Flag/Guess the Flag/ViewController.swift
@@ -30,7 +30,8 @@ class ViewController: UIViewController {
@IBOutlet var button3: UIButton!
var currentScore = 0
-
+ var flagChoiceKeys = [String]()
+ var correctFlagKey: String?
override func viewDidLoad() {
super.viewDidLoad()
@@ -42,15 +43,18 @@ class ViewController: UIViewController {
func askQuestion() {
- let flagKeys = flagFilePathsAndNames.keys.shuffled()[..<3]
- let flagToGuess = flagFilePathsAndNames[flagKeys.randomElement() ?? flagKeys[0]]
+ flagChoiceKeys = Array(flagFilePathsAndNames.keys.shuffled()[..<3])
+ correctFlagKey = flagChoiceKeys.randomElement()
- if let _flagToGuess = flagToGuess {
- title = "Which flag belongs to \(_flagToGuess)"
+ if let _key = correctFlagKey {
+ if let flagName = flagFilePathsAndNames[_key] {
+ // 📝 If we didn't get here, that would be a serious problem that needed more robust error handling
+ title = "Which flag belongs to \(flagName)?"
+ }
}
for (index, button) in [button1, button2, button3].enumerated() {
- button?.setImage(UIImage(named: flagKeys[index]), for: .normal)
+ button?.setImage(UIImage(named: flagChoiceKeys[index]), for: .normal)
}
}
@@ -61,5 +65,45 @@ class ViewController: UIViewController {
button?.layer.borderColor = UIColor(red: 1.00, green: 0.28, blue: 0.38, alpha: 1.00).cgColor
}
}
+
+
+ func handleChoice(wasCorrect: Bool) {
+ if wasCorrect {
+ title = "Correct!"
+ currentScore += 1
+ } else {
+ title = "Incorrect!"
+ currentScore = min(currentScore - 3, 0)
+ }
+
+ let responseMessage = "Your score is now \(currentScore)."
+ let alertController = UIAlertController(title: title, message: responseMessage, preferredStyle: .alert)
+
+ alertController.addAction(
+ UIAlertAction(title: "Continue", style: .default, handler: _askAnotherQuestion)
+ )
+
+ present(alertController, animated: true)
+ }
+
+
+ @IBAction func buttonTapped(_ sender: UIButton) {
+ let flagKeyChosen = flagChoiceKeys[sender.tag]
+
+ if flagKeyChosen == correctFlagKey {
+ handleChoice(wasCorrect: true)
+ } else {
+ handleChoice(wasCorrect: false)
+ }
+ }
+
+ /// callback for the UIAlertAction that conforms to the signature it expects
+ /// (that is, taking a UIAlertAction argument).
+ ///
+ /// All we really want to do, though, is ask another quesstion, so this function
+ /// just hides the handler implementation detail away from `askQuestion`
+ func _askAnotherQuestion(action: UIAlertAction) {
+ askQuestion()
+ }
}