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

Day 25 Project 4 Not Pretty but does the job.

Forums > 100 Days of SwiftUI

I had a tough time with project 4. I tried to make it as pleasing to the eye as possible but I was so stuck on getting buttons to work and getting the logic right was right I had no steam left to make the app look pretty.

Rock Paper Scissors

//
//  ContentView.swift
//  PaperRockScissorsProj4

import SwiftUI

struct ContentView: View
{

    let possibleMoves = ["🗒️", "🪨", "✂️"]
    let possibleWinMoves = ["🗒️", "🪨", "✂️"]
    @State private var currentChoice = ""
    @State private var currentChoiceNumber = 0
    @State private var winOrLose = false
    @State private var appCurrentChoice = ""
    @State private var appCurrentChoiceNumber = 0
    @State private var shouldWin = true
    @State private var shouldWindToggle = false
    @State private var computerSelected = false
    @State private var score = 0

    var body: some View
    {
        NavigationView
        {
            ZStack(alignment: .top)
            {
                AngularGradient(gradient: Gradient(colors: [.black, .blue, .black, .black, .black]), center: .center)
                    .ignoresSafeArea()
                ZStack
                {
                    VStack
                    {
                        Text("Paper, Rock, Scissors")
                            .foregroundStyle(.white)
                            .font(.largeTitle)
                        Spacer()
                        Text("Select Paper Rock Scissors")
                            .foregroundStyle(.white)
                            .font(.title)
                            .shadow(color: .yellow, radius: 10)
                            .shadow(color: .yellow, radius: 25)

                        Spacer()
                        HStack
                        {
                            Spacer()
                            ForEach(0..<3)
                            { number in
                                Button(possibleMoves[number])
                                {
                                    currentChoiceNumber = number
                                    currentChoice = possibleMoves[currentChoiceNumber]
                                    print(currentChoice)
                                    selectMove()
                                    print(appCurrentChoice)
                                    playerScore()
                                    computerSelected = true
                                }
                                .buttonStyle(BorderlessButtonStyle())
                                Spacer()
                            }
                        } .font(.system(size: 50))
                        Spacer()
                        Spacer()

                        Section
                        {
                            Text("Your selection is \(currentChoice)")
                                .prominentMyText()
                            Spacer()
                            Text("Player's score \(score)")
                                .prominentMyText()

                            Spacer()

                            Text(computerSelected ? "Computer selection is \(appCurrentChoice)" : "Waiting for computer selection")
                                .prominentMyText()
                            Spacer()
                            Text(shouldWin ? "Should win" : "Should lose")
                                .prominentMyText()
                        }
                    }
                }
            }
        }
    }

    func moveTapped(_ number: Int)
    {
        currentChoice = possibleMoves[number]
        print(currentChoice)
    }

    func selectMove()
    {

        appCurrentChoiceNumber = Int.random(in: 0..<possibleMoves.count)

        appCurrentChoice = possibleMoves[appCurrentChoiceNumber]

        if shouldWindToggle
        {
            shouldWin.toggle()
        }
        else
        {
            shouldWin = Bool.random()
            shouldWindToggle = true
        }

    }
    func playerScore()
    {

        if (currentChoiceNumber == appCurrentChoiceNumber) && shouldWin
        {
            print("Tie but should win")
            print("currentChoice \(currentChoiceNumber)\(currentChoice) and appCurrentChoice \(appCurrentChoiceNumber)\(appCurrentChoice)")
            score += 0
        } //Tie but should win
        else if (currentChoiceNumber == appCurrentChoiceNumber) && shouldWin == false
        {
            print("Tie but should lose")
            print("currentChoice \(currentChoiceNumber)\(currentChoice) and appCurrentChoice \(appCurrentChoiceNumber)\(appCurrentChoice)")
            score += 0
        } //Tie but should lose
        else if (currentChoiceNumber == 0 && appCurrentChoiceNumber == 1 || currentChoiceNumber == 1 && appCurrentChoiceNumber == 2 || currentChoiceNumber == 2 && appCurrentChoiceNumber == 0) && shouldWin == false
        {

            score -= 1
            print("Win but should lose")
            print("currentChoice \(currentChoiceNumber)\(currentChoice) and appCurrentChoice \(appCurrentChoiceNumber)\(appCurrentChoice)")
        } // Win but should lose
        else if (currentChoiceNumber == 0 && appCurrentChoiceNumber == 1 || currentChoiceNumber == 1 && appCurrentChoiceNumber == 2 || currentChoiceNumber == 2 && appCurrentChoiceNumber == 0) && shouldWin == true
        {

            score += 1
            print("Win but should win")
            print("currentChoice \(currentChoiceNumber)\(currentChoice) and appCurrentChoice \(appCurrentChoiceNumber)\(appCurrentChoice)")
        } // Win but should win
        //        let possibleMoves = ["🗒️", "🪨", "✂️"]
        //        let possibleWinMoves = ["🗒️", "🪨", "✂️"]
        else if (currentChoiceNumber == 0 && appCurrentChoiceNumber == 2 || currentChoiceNumber == 1 && appCurrentChoiceNumber == 0 || currentChoiceNumber == 2 && appCurrentChoiceNumber == 1) && shouldWin == false
        {
            score += 1
            print("Lose but should lose")
            print("currentChoice \(currentChoiceNumber)\(currentChoice) and appCurrentChoice \(appCurrentChoiceNumber)\(appCurrentChoice)")
        } //Lose but should lose

        else if (currentChoiceNumber == 0 && appCurrentChoiceNumber == 2 || currentChoiceNumber == 1 && appCurrentChoiceNumber == 0 || currentChoiceNumber == 2 && appCurrentChoiceNumber == 1) && shouldWin
        {
            score -= 1
            print("Lose but should win")
            print("currentChoice \(currentChoiceNumber)\(currentChoice) and appCurrentChoice \(appCurrentChoiceNumber)\(appCurrentChoice)")
        } //Lose but should win

    }
    /*let possibleMoves = ["🗒️", "🪨", "✂️"]
    let possibleWinMoves = ["🪨", "✂️", "🗒️"]*/
}

struct MyText: ViewModifier
{
    func body(content: Content) -> some View
    {
        content
            .foregroundStyle(.white)
            .font(.largeTitle)
            .shadow(color: .black, radius: /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/)
            .shadow(color: .black, radius: 10)
    }
}

extension View
{
    func prominentMyText() -> some View
    {
        modifier(MyText())
    }
}

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

2      

I would change the order in which things are shown on the screen. Now the score is in the middle and what I need to do at the bottom of the screen. if I choose the same option as the computer, the score doesn't change. I also see this back in your code "score += 0". Is this on on purpose? I would think that the user will loose if they tie since they needed to win or lose the game. Overall the code does seems to work well!

2      

I reordered the app to make more sense. However I want to add a reset alert button and I am getting all kinds of errors.

Here is a pic of my current app:

Paper Rock Scissors

Here is the code:

//
//  ContentView.swift
//  PaperRockScissorsProj4
//
//

import SwiftUI

struct ContentView: View
{

    let possibleMoves = ["🗒️", "🪨", "✂️"]
    @State private var currentChoice = ""
    @State private var currentChoiceNumber = 0
    @State private var appCurrentChoice = ""
    @State private var appCurrentChoiceNumber = 0
    @State private var shouldWin = true
    @State private var shouldWindToggle = false
    @State private var computerSelected = false
    @State private var score = 0
    @State private var shouldReset = false

    var body: some View
    {
        NavigationView
        {
            ZStack(alignment: .top)
            {
                AngularGradient(gradient: Gradient(colors: [.black, .blue, .black, .black, .black]), center: .center)
                    .ignoresSafeArea()
                VStack
                {
                    Section
                    {
                        Text("Paper, Rock, Scissors")
                            .foregroundStyle(.white)
                            .font(.largeTitle)
                        Spacer()
                        Spacer()

                        Text("Select Paper Rock Scissors")
                            .foregroundStyle(.white)
                            .font(.title)
                            .shadow(color: .yellow, radius: 10)
                            .shadow(color: .yellow, radius: 25)

                        Spacer()
                        Spacer()
                        Spacer()
                    }
                    Section
                    {
                        HStack
                        {
                            Spacer()
                            ForEach(0..<3)
                            { number in
                                Button(possibleMoves[number])
                                {
                                    currentChoiceNumber = number
                                    currentChoice = possibleMoves[currentChoiceNumber]
                                    print(currentChoice)
                                    selectMove()
                                    print(appCurrentChoice)
                                    playerScore()
                                    computerSelected = true
                                }
                                .buttonStyle(BorderlessButtonStyle())
                                Spacer()
                            }
                        } .font(.system(size: 50))
                    }
                    Spacer()
                    Spacer()

                    Section
                    {
                        Spacer()
                        Text("Player \(currentChoice)")
                            .prominentMyText()

                        Spacer()

                        Text(computerSelected ? "Computer  \(appCurrentChoice)" : "Computer")
                            .prominentMyText()
                        Spacer()
                        Text(shouldWin ? "Should win" : "Should lose")
                            .prominentMyText()

                        Spacer()
                        Text("Score \(score)")
                            .prominentMyText()

                        Spacer()
                        Button("Reset")
                        {
                            shouldReset = true
                        }
                        .alert("Reset the Game", isPresented: $shouldReset)
                        {
                            Button("OK", role: .destructive, "Cancel", role: .cancel)
                            {
                                resetGame()
                            }
                        }
                        Spacer()
                    }
                }
            }
        }
    }

    func moveTapped(_ number: Int)
    {
        currentChoice = possibleMoves[number]
        print(currentChoice)
    }

    func selectMove()
    {

        appCurrentChoiceNumber = Int.random(in: 0..<possibleMoves.count)

        appCurrentChoice = possibleMoves[appCurrentChoiceNumber]

        if shouldWindToggle
        {
            shouldWin.toggle()
        }
        else
        {
            shouldWin = Bool.random()
            shouldWindToggle = true
        }

    }
    func playerScore()
    {

        if (currentChoiceNumber == appCurrentChoiceNumber) && shouldWin
        {
            print("Tie but should win")
            print("currentChoice \(currentChoiceNumber)\(currentChoice) and appCurrentChoice \(appCurrentChoiceNumber)\(appCurrentChoice)")
            score -= 1
        } //Tie but should win
        else if (currentChoiceNumber == appCurrentChoiceNumber) && shouldWin == false
        {
            print("Tie but should lose")
            print("currentChoice \(currentChoiceNumber)\(currentChoice) and appCurrentChoice \(appCurrentChoiceNumber)\(appCurrentChoice)")
            score -= 1
        } //Tie but should lose
        else if (currentChoiceNumber == 0 && appCurrentChoiceNumber == 1 || currentChoiceNumber == 1 && appCurrentChoiceNumber == 2 || currentChoiceNumber == 2 && appCurrentChoiceNumber == 0) && shouldWin == false
        {

            score -= 1
            print("Win but should lose")
            print("currentChoice \(currentChoiceNumber)\(currentChoice) and appCurrentChoice \(appCurrentChoiceNumber)\(appCurrentChoice)")
        } // Win but should lose
        else if (currentChoiceNumber == 0 && appCurrentChoiceNumber == 1 || currentChoiceNumber == 1 && appCurrentChoiceNumber == 2 || currentChoiceNumber == 2 && appCurrentChoiceNumber == 0) && shouldWin == true
        {

            score += 1
            print("Win but should win")
            print("currentChoice \(currentChoiceNumber)\(currentChoice) and appCurrentChoice \(appCurrentChoiceNumber)\(appCurrentChoice)")
        } // Win but should win
        else if (currentChoiceNumber == 0 && appCurrentChoiceNumber == 2 || currentChoiceNumber == 1 && appCurrentChoiceNumber == 0 || currentChoiceNumber == 2 && appCurrentChoiceNumber == 1) && shouldWin == false
        {
            score += 1
            print("Lose but should lose")
            print("currentChoice \(currentChoiceNumber)\(currentChoice) and appCurrentChoice \(appCurrentChoiceNumber)\(appCurrentChoice)")
        } //Lose but should lose

        else if (currentChoiceNumber == 0 && appCurrentChoiceNumber == 2 || currentChoiceNumber == 1 && appCurrentChoiceNumber == 0 || currentChoiceNumber == 2 && appCurrentChoiceNumber == 1) && shouldWin
        {
            score -= 1
            print("Lose but should win")
            print("currentChoice \(currentChoiceNumber)\(currentChoice) and appCurrentChoice \(appCurrentChoiceNumber)\(appCurrentChoice)")
        } //Lose but should win

    }

    func resetGame()
    {
        currentChoice = ""
        currentChoiceNumber = 0
        appCurrentChoice = ""
        appCurrentChoiceNumber = 0
        shouldWin = true
        shouldWindToggle = false
        computerSelected = false
        score = 0
    }
}

struct MyText: ViewModifier
{
    func body(content: Content) -> some View
    {
        content
            .foregroundStyle(.white)
            .font(.largeTitle)
            .shadow(color: .black, radius: /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/)
            .shadow(color: .black, radius: 10)
    }
}

extension View
{
    func prominentMyText() -> some View
    {
        modifier(MyText())
    }
}

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

Here is the code I'm adding:

 Spacer()
                        Button("Reset")
                        {
                            shouldReset = true
                        }
                        .alert("Reset the Game", isPresented: $shouldReset)
                        {
                            Button("OK", role: .destructive, "Cancel", role: .cancel)
                            {
                                resetGame()
                            }
                        }
                        Spacer()

Here is the errors I am getting: PaperRockScissorsProj4/PaperRockScissorsProj4/ContentView.swift:77:21 Struct 'ViewBuilder' requires that 'EmptyTableRowContent<V>' conform to 'View'

And this is the other one: PaperRockScissorsProj4/PaperRockScissorsProj4/ContentView.swift:79:25 Static method 'buildExpression' requires that 'Spacer' conform to 'TableRowContent'

2      

So I manage to figure it out. Its about placement in the code. Now it works. Paper Rock Scissors

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.