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

Day 25: Rock Paper Scissors issue

Forums > 100 Days of SwiftUI

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

    }

}

   

func playGame()
func play(choices: Int)
func reset()

need to move them out of the

var body: some View { …
…
}

as these are functions not views. If you move them to above the var body…, or below its closing brace, then that should be OK.

Also check your colours, as reading black text against a dark grey backgorund and white text just about where the gradient transistions to white, is a little too difficult to read.

   

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out 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.