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

Day 25 - My version of the final working app

Forums > 100 Days of SwiftUI

Hi, guys. First time posting here.

I thought Day 25 project was very challenging but interesting, and you could make it in many different ways.

Here is my version. And I hope you can tell me your opinion about it.

import SwiftUI

struct ContentView: View {
    let moves = ["Rock", "Paper", "Scissors"]
    @State private var choice = Int.random(in: 0...2)
    @State private var winOr = Bool.random()
    @State private var userScore = 0

    @State private var showingFinish = false
    @State private var loopTimes = 0

    var body: some View {
        NavigationView {
            ZStack {
                VStack(spacing: 20) {
                    Text("Player's Score: \(userScore)")
                    Text("Computer's Move: ")
                    Text("You need to \(winOr == true ? "Win" : "Lose") against \(moves[choice])")

                    ForEach(0 ..< 3) {number in
                        Button(action: {
                            self.buttonTapped(number)
                        }) {
                            Text(moves[number])
                        }
                    }
                    Spacer()
                }
                .alert(isPresented: $showingFinish) {
                    Alert(title: Text("Good game!"), message:
                          Text("The game is over. Your final score is: \(userScore)"),
                          dismissButton: .default(Text("Play again!")) {
                            userScore = 0
                          })
                }
            }

            .navigationTitle("Brain Train Game")
        }
    }

    func buttonTapped(_ number: Int) {
        loopTimes += 1

        if winOr == true && winLogic(ai: moves[choice], player: moves[number]) == true {
            userScore += 1
        } else if winOr == false && winLogic(ai: moves[choice], player: moves[number]) == false {
            userScore += 1
        } else {
            userScore -= 1
        }
        reDo()

        if loopTimes >= 10 {
            loopTimes = 0
            showingFinish = true
        }
    }

    func winLogic(ai: String, player: String) -> Bool? {
        if ai == player {
            return nil
        } else if ai == "Rock" && player == "Scissors" {
            return false
        } else if ai == "Paper" && player == "Rock" {
            return false
        } else if ai == "Scissors" && player == "Paper" {
            return false
        } else {
            return true
        }
    }

    func reDo() {
        choice = Int.random(in: 0...2)
        winOr = Bool.random()
    }
}

2      

If someone knows a better way to make logic for this game (the actual rock/paper/scissors part), please share :)

2      

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.

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.