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:
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'