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