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

Day 25 - Rock, Paper, Scissors

Forums > 100 Days of SwiftUI

Hi All!

Hope you're all doing well :)

Its been a few weeks longer than it should have been but work... kids.. pets...

I'd love some feedback on my attempt at Rock, Paper, Scissors..

I have jazzed it a bit by starting at 10 points and slowly decreasing the points you can get with a timer, when the timer hits 0 you loose that round and another starts - if you get it wrong you loose 5 points, if you are very quick and correct you could win 10 points!

As always feedback is very appreciated! (I know that I have just ripped off the radialgradient from the flag game :)

import SwiftUI

let possibleMoves = ["Rock", "Paper", "Scissors"]
let possibleMovesEmoji = ["πŸͺ¨", "πŸ“„", "βœ‚οΈ"]
let winningMoves = ["Scissors", "Rock", "Paper"]

struct ContentView: View {

    @State private var shouldWin = Bool.random()
    @State private var move = Int.random(in:0...2)
    @State private var scoreTitle = ""
    @State private var score = 0
    @State private var points = 10
    @State private var turns = 0
    @State private var showingGameOver = false

    var body: some View {

        ZStack {
            RadialGradient(stops: [
                .init(color: Color(red: 0.1, green: 0.2, blue: 0.45), location: 0.3),
                .init(color: Color(red: 0.76, green: 0.15, blue: 0.26), location: 0.3)], center:
                .top, startRadius: 200, endRadius: 700).ignoresSafeArea()
            VStack {
                Spacer()
                Text("\(shouldWin ? "Win" : "Lose") ")
                    .font(.system(size: 100))
                Text("Against")
                Text("\(possibleMoves[move])")
                    .font(.system(size: 50))
                HStack {
                    ForEach(0..<3) { number in
                        Button {
                            moveTapped(number)
                        } label : {
                            Text(possibleMovesEmoji[number])
                                .font(.system(size: 70))
                        }
                    }
                }
                Text(scoreTitle)
                Text("Current Score : \(score)")
                Text("Playing for \(points) points!")
                    .onReceive(timer) { _ in
                        if points > 0 && showingGameOver == false {
                            points -= 1
                        }
                        if points == 0 {
                            nextTurn()
                        }
                    }
                Spacer()
                Spacer()
            }
            .alert("Game Over", isPresented: $showingGameOver)
            {
                Button("Play Again", action: resetGame)
            } message: {
                Text("You scored \(score)")    }
        }
    }

    let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()

    func moveTapped(_ number: Int)
    {
        if possibleMoves[move] == winningMoves[number] {
            shouldWin ? correct() : incorrect() }
        else {
            shouldWin ? incorrect() : correct()
        }
        nextTurn()
    }

    func nextTurn() {
        move = Int.random(in:0...2)
        shouldWin.toggle()
        turns += 1
        points = 10
        if turns > 10 {
            showingGameOver = true
        }
    }

    func correct()
    {
        scoreTitle = "Correct"
        score += points
    }

    func incorrect()
    {
        scoreTitle = "Wrong"
        score -= 5
        if score < 0 {
            score = 0
        }
    }

    func resetGame()
    {
        score = 0
        turns = 0
        scoreTitle = ""
    }
}

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

1      

@Bnerd  

Hi!

Nice approach on completing the challenge! I have a question, where do you handle the case that the user picks the same ? My logic func is as per attached below :

func checkAnswer(answer: Int) {
        questionCount += 1
        let winOrLoseCheck = shouldWin
        var scoreAmend = 0
        if winOrLoseCheck {
            scoreAmend += 1
        } else {
            scoreAmend -= 1
        }
       let oldScore = yourScore

        if moves[moveNumber] == "πŸͺ¨" && winningMoves[answer] == "πŸ“ƒ" {
            yourScore += scoreAmend
        } else if moves[moveNumber] == "πŸͺ¨" && winningMoves[answer] == "βœ‚οΈ" {
            yourScore -= scoreAmend
        } else if moves[moveNumber] == "πŸͺ¨" && winningMoves[answer] == "πŸͺ¨" {
            yourScore += 0
        } else if moves[moveNumber] == "πŸ“ƒ" && winningMoves[answer] == "βœ‚οΈ" {
            yourScore += scoreAmend
        } else if moves[moveNumber] == "πŸ“ƒ" && winningMoves[answer] == "πŸͺ¨" {
            yourScore -= scoreAmend
        } else if moves[moveNumber] == "πŸ“ƒ" && winningMoves[answer] == "πŸ“ƒ" {
            yourScore += 0
        } else if moves[moveNumber] == "βœ‚οΈ" && winningMoves[answer] == "πŸͺ¨" {
            yourScore += scoreAmend
        } else if moves[moveNumber] == "βœ‚οΈ" && winningMoves[answer] == "πŸ“ƒ" {
            yourScore -= scoreAmend
        } else if moves[moveNumber] == "βœ‚οΈ" && winningMoves[answer] == "βœ‚οΈ" {
            yourScore += 0
        }

        //Was the Answer correct or not.
        if oldScore < yourScore {
            wasCorrectAnswer = true
        } else {
            wasCorrectAnswer = false
        }
        showRectangle = false
        newGame()
    }

1      

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.