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

day 25 project rock paper scissors questions

Forums > SwiftUI

Hi there,

been really struggling with this one. at this point i'm just working on the rock, paper scissors as if they are all win, such that the answers should be: rock = paper paper = scissors scissors = rock

so my issue is at func gameTapped(_number: Int) {

it works where i ask the user to tap the correct move for Rock and Paper, due to the following code:

''' if number == correctAnswer + 1 { scoreTitle = "Correct" score += 100 '''

but it comes apart here...

''' if number == 3 { number == correctAnswer - 2 scoreTitle = "Correct" score += 100 '''

   i am trying to write that if the number is 3 (scissors), then this should map to rock

   Many thanks in advance for any guidance you can give me

 ==========================

my full code is as follows:

'''

import SwiftUI

struct ContentView: View {

@State private var game = ["Rock", "Paper", "Scissors"] //.shuffled()

@State private var correctAnswer = Int.random(in: 0...2)

@State private var winLose = [" Win", " Lose"]

@State private var correctAnswer1 = Int.random(in: 0...2)

@State private var showingScore = false // tells us if alert is showing or not

@State private var scoreTitle = "" // title inside the alert

@State private var score = 0

var body: some View {

    ZStack {

        LinearGradient(gradient: Gradient(colors: [.blue, .black]),

                       startPoint: .top, endPoint: .bottom)

        VStack(spacing: 30) {

            VStack {

                Text("Tap the correct move")

                    .foregroundColor(.white)

                Text(game[correctAnswer] + winLose[correctAnswer1])

                    .foregroundColor(.white)

                    .font(.largeTitle)

                    .fontWeight(.black)

            }

            ForEach(0 ..< 3) { number in

                Button(action: {

                    self.gameTapped(number)

                }) {

                    Text(self.game[number])

                        .foregroundColor(.white)

                        .clipShape(Capsule())

                        .overlay(Capsule().stroke(Color.black, lineWidth: 1))

                        .shadow(color: .black, radius: 2)

                }

            }

            Spacer()

        }

    }

    .alert(isPresented: $showingScore) {

        Alert(title: Text(scoreTitle), message: Text("Your score is \(score)"), dismissButton: .default(Text("Continue")) {

            self.askQuestion()

        })

    }

}

func gameTapped(_ number: Int) {

    if number == correctAnswer +1 {

         scoreTitle = "Correct"

             score += 100

       if number == 3 {

           number == correctAnswer - 2    // this is telling me result of operator == is unused

              scoreTitle = "Correct"

                score += 100

            }
    }

    else {

        scoreTitle = "Wrong. This was..... \(game[number])"

        score -= 100

    }

    showingScore = true

}

func askQuestion() {

    //game.shuffle()

    correctAnswer = Int.random(in: 0...2)

}

}

struct ContentView_Previews: PreviewProvider {

static var previews: some View {

    ContentView()

}

}

'''

2      

@State private var winLose = [" Win", " Lose"]
@State private var correctAnswer1 = Int.random(in: 0...2)

Noticed that your winLose array has 2 elements, however your correctAnswer1 has a range 0...2, so 3 elements. This will cause a runtime index out of range error on the winLose array.

Change to

@State private var correctAnswer1 = Int.random(in: 0...1)

to prevent a problem with this line

Text(game[correctAnswer] + winLose[correctAnswer1])

Also in your code you have

ForEach(0 ..< 3) { number in

so number will never be 3.

2      

thanks,

much apprec

reviewing your above, specifically the ForEach(0 ..< 3) { number in...........really helped me breakthrough the conundrem.

I rewrote it as follows, and it's now working, so thanks for the help....Now I can work on the next part of the code!

i kept the:

''' ForEach(0 ..< 3) { number in '''

and re-wrote the rest of the code as follows:

''' func gameTapped(_ number: Int) {

   if number <= 2, number == correctAnswer + 1 {

        scoreTitle = "Correct"

           score += 100

   } else if number == correctAnswer - 2 {

          scoreTitle = "Correct"

             score += 100

    }

        else {

        scoreTitle = "Wrong. This was..... \(game[number])"

        score -= 100

    }

    showingScore = true

}

'''

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.