TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Solved: Day 25 Challenge - Rock Paper Scissors

Forums > 100 Days of SwiftUI

Hi, I wanted to share my solution with you. I didn't spend too much time on visuals but creating the enum structure was a bit time consuming.

Happy to hear your feedback, thanks!

app

Here is the code:


import SwiftUI

enum Shapes: String, CaseIterable {
    case rock, paper, scissors

    var image: some View {
        switch self {
            case .rock: return Image("rock").resizable().scaledToFit()
            case .paper: return Image("paper").resizable().scaledToFit()
            case .scissors: return Image("scissors").resizable().scaledToFit()
        }
    }
}

struct ContentView: View {

    let choices = [Shapes.rock, Shapes.paper, Shapes.scissors] // order of array matters
    let outcomes = ["Lose", "Win"]

    @State private var totalScore = 0
    @State private var gameEnd = false

    @State private var randomShapeIndex = Int.random(in: 0...2)
    @State private var randomDesiredOutcomeIndex = Int.random(in: 0...1)

    @State private var totalTurns = 0

    var shapeApp: Shapes {
        choices[randomShapeIndex]
    }

    var desiredOutcome: String {
        outcomes[randomDesiredOutcomeIndex]
    }

    var body: some View {
        ZStack {
            LinearGradient(gradient: Gradient(colors: [Color.teal, Color(red: 0.2, green: 0.2, blue: 0.5)]), startPoint: .top, endPoint: .bottom).edgesIgnoringSafeArea(.all)

            VStack {
                Spacer()
                Spacer()
                Section {
                    Text("I choose \(shapeApp.rawValue)")
                        .font(.title2)
                        .colorInvert()
                    Text("I want you to \(desiredOutcome)")
                        .font(.title2)
                        .colorInvert()
                } header: {
                    Text("Let's Play a Game").font(.largeTitle)
                        .padding()
                        .background(.ultraThinMaterial)
                }
                Spacer()

                Section {
                    HStack{
                        Button{
                            evaluateResults(Shapes.rock)
                        } label: {Shapes.rock.image}
                        Button{
                            evaluateResults(Shapes.paper)
                        } label: {Shapes.paper.image}
                        Button{
                            evaluateResults(Shapes.scissors)
                        } label: {Shapes.scissors.image}
                    }
                } header: {
                    Text("Make Your Selection:")
                        .padding()
                    //                    .background(.teal)
                        .foregroundColor(.primary)
                }

                Form{
                    Section {
                        Text("\(totalScore)")
                            .font(.largeTitle)
                    } header: {
                        Text("Your Score")
                    }

                } .frame(minHeight: 0, maxHeight: 200)

            }
            .alert("Game Ended! ", isPresented: $gameEnd){
                Button("Continue", action: resetGame)
            } message: {
                Text("Your score is \(totalScore)")
            }

        }
    }

    func evaluateResults(_ shapeUser: Shapes){
        totalTurns += 1
        var outcome: String
        switch (shapeUser, shapeApp) {
        case (.rock, .rock): outcome = "Draw"
        case (.rock, .paper): outcome = "Lose"
        case (.rock, .scissors): outcome = "Win"
        case (.paper, .paper): outcome = "Draw"
        case (.paper, .rock): outcome = "Win"
        case (.paper, .scissors): outcome = "Lose"
        case (.scissors, .scissors): outcome = "Draw"
        case (.scissors, .paper): outcome = "Win"
        case (.scissors, .rock): outcome = "Lose"
        }

        if desiredOutcome == "Win" && outcome == "Win" {
            totalScore += 1
        } else if desiredOutcome == "Lose" && outcome == "Lose" {
            totalScore += 1
        }

        if totalTurns == 10 {
            gameEnd = true
        }
        randomShapeIndex = Int.random(in: 0...2)
        randomDesiredOutcomeIndex = Int.random(in: 0...1)
        print("user: \(shapeUser), app: \(shapeApp), outcome: \(outcome), desired: \(desiredOutcome), totalScore: \(totalScore)")

    }

    func resetGame(){
        totalScore = 0
        randomShapeIndex = Int.random(in: 0...2)
        randomDesiredOutcomeIndex = Int.random(in: 0...1)
    }
}

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

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.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

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.