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.