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

SOLVED: Why the score doesn't go back to 0? Milestone 1-3 Rock Paper Scissors

Forums > 100 Days of SwiftUI

Hello, I'm trying to resolve the first milestone but I'm stuck on two problems.

I created a reset function to reset variables to their initial value. The rounds variables goes back to 1 but the score variables becomes 1 instead of 0. Why?

Could you give me some advices to how implement the Shouldwin variable and how to write better code?

This is the code:

import SwiftUI

struct moveModifier: ViewModifier {
    func body(content: Content) -> some View {
        content
            .font(.system(size: 50))
    }
}

extension View {
    func moveStyle() -> some View {
        modifier(moveModifier())
    }
}

struct ContentView: View {
    let moves: [String] = ["✊", "🀚", "✌️"]
    @State private var computerMove = Int.random(in: 0...2)
    @State private var playerMove: String = ""
    @State private var score: Int = 0
    @State private var rounds: Int = 1
    @State private var text = ""
    @State private var resetAlert = false

    var body: some View {
        VStack{
            VStack(spacing: 5) {
                Text("Score: \(score)")
                Text("Computer move: \(moves[computerMove])")
                Text(text)
            }
            .padding()
            HStack{
                Button(action: {
                    playerMove = "✊"
                    round()
                }, label: {
                    Text("✊")
                        .moveStyle()
                })

                Button(action: {
                    playerMove = "🀚"
                    round()
                }, label: {
                    Text("🀚")
                        .moveStyle()
                })

                Button(action: {
                    playerMove = "✌️"
                    round()
                }, label: {
                    Text("✌️")
                        .moveStyle()
                })
            }
        }
        .alert("Final score", isPresented: $resetAlert) {
            Button("Reset", action: reset)
        } message: {
            Text("Your final score is \(score)!")
        }
    }
    func game() {
        if playerMove == "✊" && moves[computerMove] == "🀚" || playerMove == "🀚" && moves[computerMove] == "✌️" || playerMove == "✌️" && moves[computerMove] == "✊" {
            text = "Wrong"
            score -= 1
        }

        else if playerMove == "✊" && moves[computerMove] == "✌️" || playerMove == "🀚" && moves[computerMove] == "✊" || playerMove == "✌️" && moves[computerMove] == "🀚" {
            text = "Correct"
            score += 1
        }
        else {
            text = "draw"
            score += 0
        }
    }

    func round() {
        if rounds <= 10 {
            game()
            computerMove = Int.random(in: 0...2)
            rounds += 1
        }
        else{
            resetAlert = true
        }
    }

    func reset() {
        score = 0
        rounds = 1
        round()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

2      

When posting code to these forums, please place three backticks ``` on the line before your code and three backticks ``` on the line after your code so that it will be formatted properly. You can also highlight an entire code block and click the </> button on the toolbar to wrap the block for you.

This makes it far easier to read and also makes it easier for other posters to copy/paste the code in order to test solutions and such.

Doing this will ensure that you end up with something like this:

func printSomething(_ thing: String?) {
    if let thing = thing {
        print(thing)
    } else {
        print("nothing there")
    }
}

instead of this:

func printSomething(_ thing: String?) { if let thing = thing { print(thing) } else { print("nothing there") } }

2      

You should also reset playerMove when you reset the game.

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.