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

Day25 Project

Forums > 100 Days of SwiftUI

@Dyver  

Heya, i just started with the rock-paper-scissors project on Day 25.

I looked back at the GuessTheFlag Project i created earlier on. But this time i have no idea where to start. At first i did this: @State private var RPS = ["Scissors", "Rock", "Paper"] @State private var GameMove = Int.random(in: 0...2) I have no idea how to continue and what to do. Can you guys maybe give me some advice how to set this up. It would be super nice if you wouldn't give me like the whole code, so i have to think for myself.

Thank you very much!

3      

Here is a minimal version of the game code. Some of the button logic is missing. See if you can first finish the logic to correctly calculate the score and perhaps later improve the game layout. Good luck.

struct ContentView: View {
    var things = ["Rock","Paper","Scissors"]
    @State private var choice = "Paper"
    @State private var shouldWin = false
    @State private var score = 0

    func nextRound() {
        choice = things[Int.random(in: 0...2)]
        shouldWin = Bool.random()
    }

    var body: some View {
        VStack {
            Text("My Choice is \(choice)")
            Text("You must \(shouldWin ? "WIN" : "LOSE")")
            Button(things[0]) {
                if self.choice == self.things[2] && self.shouldWin || self.choice == self.things[1] && !self.shouldWin {
                    self.score += 1
                } else {
                    self.score -= 1
                }
                self.nextRound()
            }
            Button(things[1]) {
                // add calculate score logic here

                self.nextRound()
            }
            Button(things[2]) {
                // add calculate score logic here

                self.nextRound()
            }
            Text("Your score is \(score)")
        }
    }
}

4      

If you want some ideas here my code on github

https://github.com/NigelGee/RockPaperScissors

3      

Answering @NigelGee... I looked at your code on GitHub, but I think you have not completely understand the app's purpose. The challenge asks to create not a standard app of "Rock, Scissor Sister" but a brain training game where the app asks users to guess if they're gonna lose or win. Every time, for 10 loops, the game have to ask the player if he's gonna lose or win and, only then, will ask for "rock, scissor, paper". For example:

First Loop: The Game randomly pick-up TRUE and ROCK, and then ask player: "You think you're gonna WIN or LOSE"? If the player select WIN, then he have to select Paper to get a +1 score. But, if he select LOSE, he have to select SCISSOR to get a +1 score.

Second Loop: The Game randomly pick-up TRUE and SCISSOR, and then ask player: "You think you're gonna WIN or LOSE"? If the player select WIN, then he have to select Rock to get a +1 score. But, if he select LOSE, he have to select Paper to get a +1 score.

Three, Four, and so on..

This is the difficulty :\

3      

Thanks to @wieniek for the starting point. I adjusted the code to take care of situations where the random choice is the same as your pick. Also, the func should run before the button is tapped. Now, the time is to count the ten games and than stop. Plus create somw UI. Feel free to play around and good luck.

Example:

Button(options[0]) {
    self.nextRound()
    if self.choise == self.options[2] && self.shouldWin || self.choise == self.options[1] && !self.shouldWin {
        self.score += 1
    } else if self.choise == self.options[0] && self.shouldWin || self.choise == self.options[0] && !self.shouldWin {
         self.score += 0
     } else {
          self.score -= 1
     }           
 }

3      

Guys, here is my full code. I know isn't perfect, but might help to somebody. Any feedback is much appreciated. Thanks.

//
//  ContentView.swift
//  RockPaperScissors
//
//  Created by Marek Tarkos on 23/01/2021.
//

import SwiftUI

struct ContentView: View {

    var options = ["Rock", "Paper", "Scissors"]
    @State private var shouldWin = false
    @State private var score = 0
    @State private var choice = "Rock"

    @State private var showingAlert = false
    @State private var alertTitle = ""
    @State private var roundCount = 0
    @State private var roundCountView = false

    var body: some View {
        ZStack {
            LinearGradient(gradient: Gradient(colors: [.black, Color(.systemTeal)]), startPoint: .top, endPoint: .bottom).edgesIgnoringSafeArea(.all)

            VStack {

                VStack(spacing: 20) {
                    Text("Next round you must")
                    Text(shouldWin ? "Win".uppercased() : "Loose".uppercased())
                        .font(.title)
                        .fontWeight(.bold)
                }
                .foregroundColor(.white)

                Spacer()
                // Rock selection button
                HStack(spacing: 20) {
                    Button(action: {
                        rockWasSelected()
                    }) {
                        Text("\(options[0])")
                            .buttonDesign()
                    }
                    // Paper selection
                    Button(action: {
                        paperWasSelected()
                    }) {
                        Text("\(options[1])")
                            .buttonDesign()
                    }
                    // Scissors selection
                    Button(action: {
                        scissorsWasSelected()
                    }) {
                        Text("\(options[2])")
                            .buttonDesign()
                    }
                }
                Spacer()

                Text(roundCountView ? "Score \(score)" : "")
                    .font(.title)
                    .foregroundColor(.black)
            }
        }
        .alert(isPresented: $showingAlert) {
            Alert(title: Text(roundCountView ? "GAME OVER!" : alertTitle), message: Text("Siri picked \(choice)"), dismissButton: .default(Text(roundCountView ? "Click for next game" : "Next round")){
                nextRound()
            })
        }
    }

    // start a new round and restart the whole game
    func nextRound() {
        choice = options[Int.random(in: 0...2)]
        shouldWin = Bool.random()
        roundCount += 1
        if roundCount < 11 {
            roundCountView = false
        } else {
            roundCountView = true
            roundCount = 0
        }
    }

    // game logic when Rock is selected
    func rockWasSelected() {
        if choice == options[2] && shouldWin || choice == options[1] && !shouldWin {
            score += 1
            alertTitle = "You Won :)"
        } else if choice == options[0] && shouldWin || choice == options[0] && !shouldWin {
            score += 0
            alertTitle = "We're equal :/"
        } else {
            score -= 1
            alertTitle = "You lost :("
        }
        showingAlert = true
    }

    // game logic when Paper is selected
    func paperWasSelected() {
        if choice == options[0] && shouldWin || choice == options[2] && !shouldWin {
            score += 1
            alertTitle = "You Won :)"
        } else if choice == options[1] && shouldWin || choice == options[1] && !shouldWin {
            score += 0
            alertTitle = "We're equal :/"
        } else {
            score -= 1
            alertTitle = "You lost :("
        }
        showingAlert = true
    }

    // game logic when Scissors is selected
    func scissorsWasSelected() {
        if choice == options[1] && shouldWin || choice == options[0] && !shouldWin {
            score += 1
            alertTitle = "You Won :)"
        } else if choice == options[2] && shouldWin || choice == options[2] && !shouldWin {
            score += 0
            alertTitle = "We're equal :/"
        } else {
            score -= 1
            alertTitle = "You lost :("
        }
        showingAlert = true
    }
}

// button design
struct ButtonDesign: ViewModifier {
    func body(content: Content) -> some View {
        content
            .frame(width: 100, height: 70, alignment: .center)
            .foregroundColor(.white)
            .background(Color.black)
            .font(.system(size: 20, weight: .bold, design: .rounded))
            .cornerRadius(15)
    }
}

extension View {
    func buttonDesign() -> some View {
        modifier(ButtonDesign())
    }
}

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

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.