TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

@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
    }

}

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.