Day 21 - 100 Days of Swift

3 minute read

Project 2 (part 3)

Day 21 is the third part of the second project. He gives you a couple of stretch challenges to work through on your own. You add the ability to display the current score in the title. You keep track of how many questions the user has answered and end the game after ten turns with a final alert. And, when someone picks the wrong flag, you change the alert to tell them what flag they picked.

The first step was pretty easy. I just pulled setting the title out to its own function, and added the current score with string interpolation:

1
2
3
private func updateTitle() {
    title = "\(countries[correctAnswer].uppercased()) – Current Score: \(score)"
    }

Then I added a property observer to the score, to insure that the title was always up to date:

1
2
3
4
5
var score = 0 {
    didSet {
        updateTitle()
    }
}

For the second part, I added a variable to hold the number of questions answered:

1
var answered = 0

Then I added a check to the beginning of askQuestion, to see how many questions had been answered:

1
2
3
4
guard answered < 10 else {
    presentEndOfGameAlert()
    return
}

The presentEndOfGameAlert function looks like this:

1
2
3
4
5
6
7
8
9
10
private func presentEndOfGameAlert() {
    let ac = UIAlertController(title: "Game over!",
                               message: "Your score is \(score).",
        preferredStyle: .alert)
    let action = UIAlertAction(title: "New Game",
                               style: .default,
                               handler: resetGame)
    ac.addAction(action)
    present(ac, animated: true)
}

And the resetGame function looks like this:

1
2
3
4
5
private func resetGame(action: UIAlertAction! = nil) {
    answered = 0
    score = 0
    askQuestion()
}

Finally, for the third part, I added a presentWrongAnswerAlert that looks like this:

1
2
3
4
5
6
7
8
9
10
private func presentWrongAnswerAlert(country: String) {
    let ac = UIAlertController(title: "Wrong!",
                               message: "That was \(country).",
        preferredStyle: .alert)
    let action = UIAlertAction(title: "Continue",
                               style: .default,
                               handler: askQuestion)
    ac.addAction(action)
    present(ac, animated: true)
}

And tweaked the pickedFlag function to only show that alert if the user picks the wrong answer:

1
2
3
4
5
6
7
8
9
10
@IBAction func pickFlag(_ sender: UIButton) {
    answered += 1
    if sender.tag == correctAnswer {
        score += 1
        askQuestion()
    } else {
        score -= 1
        presentWrongAnswerAlert(country: countries[sender.tag].uppercased())
    }
}

All pretty simple and small changes, but it adds a little bit of stakes to the game and gives it an end condition. It isn’t like super fun, but I’d say it meets the minimum qualifications of a game.

Screenshot of working app

You can find my version of the project at the end of Day 21 on Github here.

Reflections

I’m really enjoying these days where we get to write our own code. The challenges weren’t super difficult, but it was good practice in figuring out where to put stuff and how to organize my code. It was also good practice in thinking through the whole cycle of the game. Thinking about what should cause the game to end, what should happen when it ends, and what needs to happen to reset it and start a new game.