UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Guess the Flag, only two flags show up

Forums > 100 Days of SwiftUI

Hi there -

Sometimes I only get two flags. Here is my code.

struct ContentView: View {
    @State private var countries = ["Estonia", "France", "Germany", "Ireland", "Italy", "Nigeria", "Poland", "Spain", "Great Britian", "Ukraine", "US"].shuffled()
    @State private var correctAnswer = Int.random(in: 0...2)
    @State private var showingScore = false
    @State private var scoreTitle = ""
    @State private var playerScore = 0
    @State private var selectedFlag = 0
    @State private var questionCount = 1

    func flagTapped(_ number: Int){
        if number == correctAnswer {
            scoreTitle = "Correct"
            playerScore += 1
        } else {
            scoreTitle = "Wrong"
            selectedFlag = number
        }
        showingScore = true
    }

    func askQuestion(){
        countries.shuffle()
        correctAnswer = Int.random(in: 0...2)
        questionCount += 1
    }

    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: 700)
                .ignoresSafeArea()
            VStack{
                Spacer()
                Text("Guess the Flag")
                    .font(.largeTitle.weight(.bold))
                    .foregroundStyle(.white)
                VStack(spacing: 15){
                    VStack{
                        Text("Tap the flag of:")
                            .font(.subheadline.weight(.heavy))
                            .foregroundStyle(.secondary)
                        Text(countries[correctAnswer])
                            .font(.largeTitle.weight(.semibold))
                    }

                    ForEach(0..<3){ number in
                        Button{
                            flagTapped(number)
                        } label: {
                            Image(countries[number])
                                .clipShape(.capsule)
                                .shadow(radius: 15)
                        }
                    }
                }
                .frame(maxWidth: .infinity)
                .padding(.vertical, 20)
                .background(.regularMaterial)
                .clipShape(.rect(cornerRadius: 20))
                Spacer()
                Spacer()
                Text("Score: \(playerScore)")
                    .foregroundStyle(.white)
                    .font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/.bold())
                Spacer()
                Text("Question Count: \(questionCount)")
                    .foregroundStyle(.white)
            }
            .padding()

        }
        .alert(scoreTitle, isPresented: $showingScore){
            Button("Continue", action: askQuestion)
        } message: {
            if scoreTitle == "Correct" {
                Text("Your Score is \(playerScore)")
            } else {
                Text("You chose the flag of \(countries[selectedFlag])")
                Text("Your Score is still \(playerScore)")
            }
        }
    }
}

I don't know how to post a picture of it, I'll work on that.

   

HINT: I would bet one whole British farthing that you never see the flag of the British empire.

Keep Coding

Investigate! Then return with your findings.

   

I don't know how to post a picture of it, I'll work on that.

A forum regular posted these great hints

See-> How to include images in forum posts
See-> Tutorial: Inserting Images into Forum Posts

Note for @twoStraws


If @twoStraws is reading this:

This would be a great project for PinkerStraws!

Hello Paul! Have you considered adding a video in the first 5 days of your 100 days course informing students how to post a question to the forums, including some basic rules and hints. A bonus video might explain how to add a screen shot to the forums! Might even be useful to include how to post code snips. Also students might be interested to view your thoughts on how to post their progress by using ONE message and updating it with daily progress and questions.

With fondness, gratitude, and admiration,

Your HackingWithSwift fans!

   

@Obelix

Thanks for pointing me in the right direction.

I was not thinking through how everything connected. I did rename UK to Great Britian, forgetting that I did not rename the flag file names as well.

Thanks for the tips about adding pictures. I'll have to figure something out.

   

Also, if you're going to spell it out, try remembering this school house rhyme:

B-r-👁️-
T-a-👁️-
N!

It's A before I,
else in the dungeons you'll die. 🪦💀

Aaaaah. Grade school. Good memories. 🏰 👻

   

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.