I want to add a notification for a correct answer... I can't get this to work! Any help is appreciated...
import SwiftUI
struct ContentView: View {
let moves = [ "👊🏼", "✋🏾", "✌️"]
@State private var compChoices = Int.random(in: 0..<3)
@State private var shouldWin = Bool.random()
@State private var score = 0
@State private var questionCount = 1
@State private var showingResults = false
@State private var correctAnswer = true
var body: some View {
ZStack {
RadialGradient(stops: [
.init(color: Color(red: 0.12, green: 0.14, blue: 0.12), location: 0.3),
.init(color: .white, location: 0.5),
], center: .top, startRadius: 200, endRadius: 700)
.ignoresSafeArea()
VStack {
Spacer ()
Text("The computer chooses...")
.font(.title)
Text(moves[compChoices])
.font(.system(size: 200))
if shouldWin {
Text("Which one wins?")
.font(.title)
.foregroundColor(.white)
} else {
Text("Which one loses?")
.font(.title)
.foregroundColor(.blue)
}
HStack {
ForEach(0..<3) { number in
Button(moves[number]) {
play(choices: number)
}
.font(.system(size: 80))
}
}
Spacer ()
Text("Rock, Paper, Scissors")
.font(.largeTitle)
Spacer ()
Text("Score \(score)")
.fontWeight(.bold)
Spacer()
}
.padding(2)
.alert("Game Over", isPresented: $showingResults) {
Button("Play Again", action: reset)
} message: {
Text("Your score is \(score)")
}
}
.alert(Text("Correct"), isPresented: $correctAnswer) {
Button("Continue", action: playGame)
} message: {
Text("Your score is \(score).")
.font(.title.bold())
.foregroundColor(.blue)
}
func playGame() {
compChoices = Int.random(in: 0..<3)
shouldWin.toggle()
questionCount += 1
}
func play(choices: Int) {
let winningMoves = [1, 2, 0]
let didWin: Bool
if shouldWin {
didWin = choices == winningMoves[compChoices]
} else {
didWin = winningMoves[choices] == compChoices
}
if didWin {
score += 10
} else {
score -= 10
}
if questionCount == 10 {
showingResults = true
} else {
playGame()
}
}
func reset() {
questionCount = 0
shouldWin = Bool.random()
score = 0
compChoices = Int.random(in: 0..<3)
}
}
}