BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

@metropol: Day 22 - Guess the Flag Challenge

Forums > 100 Days of SwiftUI

Thought of posting my solution to the challenge in the Guess the Flag: Wrap up.

For resetting the game after 8 turns, I've decided to do it in the same alert and use other state variables to control the content of the alert.

import SwiftUI

struct ContentView: View {
    @State private var showingScore = false
    @State private var score = 0
    @State private var currentGame = 1
    @State private var scoreTitle = ""
    @State private var alertButton = ""
    @State private var scoreType = ""
    @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)

    private let gameNumber = 8

    var body: some View {
        ZStack {
            RadialGradient(stops: [
                .init(color: Color(red: 0.1, green: 0.2, blue: 0.45), location: 0.3),
                .init(color: Color(red: 0.76, green: 0.15, blue: 0.26), location: 0.3),
            ], center: .top, startRadius: 200, endRadius: 400)
            .ignoresSafeArea()

            VStack {

                Spacer()

                Text("Guess the Flag")
                    .font(.largeTitle.weight(.bold))
                    .foregroundColor(.white)

                VStack(spacing: 15) {
                    VStack {
                        Text("Tap the flag of")
                            .foregroundColor(.secondary)
                            .font(.subheadline.weight(.heavy))
                        Text(countries[correctAnswer])
                            .font(.largeTitle.weight(.semibold))
                    }

                    ForEach(0..<3) { number in
                        Button {
                            flagTapped(number)
                        } label: {
                            Image(countries[number])
                                .renderingMode(.original)
                                .clipShape(Capsule())
                                .shadow(radius: 5)
                        }

                    }
                }
                .frame(maxWidth: .infinity)
                .padding(.vertical, 20)
                .background(.regularMaterial)
                .clipShape(RoundedRectangle(cornerRadius: 20))

                Spacer()
                Spacer()

                Text("Score: \(score)")
                    .foregroundColor(.white)
                    .font(.title.bold())
                Text("Game \(currentGame)")
                    .foregroundColor(.white)
                    .font(.subheadline.weight(.bold))

                Spacer()
            }
            .padding()
            .alert(scoreTitle, isPresented: $showingScore) {
                Button(alertButton, action: askQuestion)
            } message: {
                Text("Your \(scoreType) is \(score)")
            }
        }
    }

    func askQuestion() {
        countries.shuffle()
        correctAnswer = Int.random(in: 0...2)
        currentGame = currentGame < gameNumber ? currentGame + 1 : 1
        if currentGame == 1 {
            score = 0
        }
    }

    func flagTapped(_ number: Int) {
        if number == correctAnswer {
            scoreTitle = "Correct 👍🏼"
            score += 1
        } else {
            scoreTitle = "Wrong 👎 That's the flag of \(countries[number])"
            score -= 1
        }

        alertButton = currentGame == gameNumber ? "Restart Game" : "Continue"
        scoreType = currentGame == gameNumber ? "final score" : "score"
        showingScore = true
    }

}

   

Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.