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

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

    }

}

2      

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.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free 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.