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

SOLVED: Rock Scissor Paper. Project Crash.

Forums > 100 Days of SwiftUI

@onqun  

Hello I am doing Rock Paper Scissor project, I have an error, I dont know How to solve errors.

and is it possible to do it another way? I am having difficulty :( is there a way to code like rock beats scissor?

"Thread 1: EXC_BAD_ACCESS (code=2, address=0x16ae53fe0)"

import SwiftUI

struct ContentView: View {

    struct BlueButton: ButtonStyle {
        func makeBody(configuration: Configuration) -> some View {
            configuration.label
                .padding()
                .background(Color(red: 0, green: 0, blue: 0.5))
                .foregroundColor(.white)
                .clipShape(Capsule())
        }
    }

    var options = ["Rock", "Paper","Scissors"]
    @State private var AIdecision: Int = Int.random (in: 0...2)
    @State private var yourFaith = Bool.random()
    @State private var Score: Int = 0
    @State private var user: Int = 0
    @State private var userDecision: Int = 0

    var goodAnswer: Int {
        if yourFaith == true {
            if AIdecision < user {
                return Score+1
            }

            return Score+1
        }
        return 0 
    }

    var body: some View {
        VStack {

            Text("Your Score is \(Score)")
                .fontWeight(.black)
                .foregroundColor(.black)
                .underline()

                .padding()

            Text("Computer has chosen '\(options[AIdecision])'. Do you want to win or lose?")
               .multilineTextAlignment(.center)
            Text("Your Faith has been decided. You\(yourFaith ? "Win" : "Lose")!!")
                .multilineTextAlignment(.center)

            padding()

            HStack {
                Button("Rock") {
                    user = 0
                    print("User has chosen \(options[0])")
                }
                //.buttonStyle(BlueButton())

                Button("Paper"){
                    user = 1
                    print("User has chosen \(options[1])")

                }
                //.buttonStyle(BlueButton())

                Button("Scissors") {
                    user = 2
                    print("User has chosen \(options[2])")
                }
                //.buttonStyle(BlueButton())
            }
        }
            // end of View
        }
        //end of content view
    }

3      

Right after your two Text elements, you have this line:

            padding()

You forgot the leading .

4      

in answer to your other question, have a look at my reply

You could use an enum to define the choices available.

enum choice {
    case rock, paper, scissors
}

Then following on from the other formnum quesition, define the condition for when the player wins (and similar one for when they need to lose the to get the points.)

func didWin(computer: choice, player: choice) -> Bool {
    switch computer {   // player wins with the choices below
    case .rock:
        return player == choice.paper
    case .paper:
        return player == choice.scissors
    case .scissors:
        return player == choice.rock
    }
}

3      

I would even go so far as to really fill out your enum:

enum Choice: Int {
    case rock
    case paper
    case scissors

    func random() -> Choice {
        Choice(rawValue: Int.random(in: 0...2))!
    }

    var beatenBy: Choice {
        switch self {
        case .rock: return .paper
        case .paper: return .scissors
        case .scissors: return .rock
        }
    }

    var beats: Choice {
        switch self {
        case .rock: return .scissors
        case .paper: return .rock
        case .scissors: return .paper
        }
    }

    func wins(against otherChoice: Choice) -> Bool {
        otherChoice == self.beats
    }

    func loses(against otherChoice: Choice) -> Bool {
        otherChoice == self.beatenBy
    }
}

That way, the game logic is all contained within the enum rather than being in the View, and it allows you to keep the logic in the View much simpler:

if userDecision.wins(against: aiDecision) {
    //player wins
} else {
    //AI wins
}

I find that more expressive and easier to understand and eaxch part of the code is only concerned with what it needs to be concerned with.

4      

@onqun  

Thank you very much everyone I really appreciate.

I looked at your reply link. It looked very complicated for me :( . I need to practice more.

3      

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.