Skip to content

monophosphate/GuessTheFlag

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

GuessTheFlag

A flag quiz project app from Hacking with Swift. To play it, simply choose the correct flag.

Demo

Notes

Under the hood:

Below is a shuffled array to randomize the flags, a random Int generator to choose a random flag as our correct answer, and the variables that decide when our alerts are shown, the content of that alert, and a variable to save your score.

    @State private var countries = ["Estonia", "France", "Germany", "Ireland",
    "Italy", "Nigeria", "Poland", "Russia", "Spain", "UK", "US"].shuffled()
    @State private var correctAnswer = Int.random(in: 0...2)
    
    @State private var showingScore = false
    @State private var scoreTitle = ""
    @State private var scoreMessage = ""
    @State private var scoreCount = 0

This is the ForEach that displays the next 4 shuffled flag from the previous array, a button is then added with the action sending which flag was tapped to the flagTapped function.

    ForEach(0 ..< 4) { number in
        Button(action : { self.flagTapped(number) } ) {
            Image(self.countries[number])
                 .renderingMode(.original)
                 .clipShape(RoundedRectangle(cornerSize: CGSize(width: 20, height: 10)))
                 .shadow(color: .black, radius: 3)
                 .opacity(0.8)
         }
     }

The flagTapped function takes in the tapped flag, and using a conditional statement, decides whether it's correct or not. The score and the alert are the updated to notify the user whether they picked the right or wrong answer.

    func flagTapped(_ number: Int) {
        if number == correctAnswer {
            scoreCount += 5
            scoreTitle = "Correct Answer"
            scoreMessage = ""
        } else {
            scoreCount -= 5
            scoreTitle = "Wrong Answer"
            scoreMessage = "That's the flag of \(countries[number])!\n"
        }
        showingScore = true
    }

The following is the alert we use to notify the user of their result, what flag they tapped on, and their current score.

        .alert(isPresented: $showingScore) {
            Alert(title: Text(scoreTitle), message: Text(scoreMessage + 
            "Current score: \(scoreCount) points"), dismissButton: 
            .default(Text("Continue")) {
                self.askQuestion()
            })
        }

Credit

Check out this project at Hacking with Swift

About

A flag quiz project app from Hacking with Swift

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages