Skip to content

Commit 87cdd43

Browse files
committed
initial commit
1 parent 71ae372 commit 87cdd43

File tree

1 file changed

+119
-2
lines changed

1 file changed

+119
-2
lines changed

rockPaperScissor/ContentView.swift

+119-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,132 @@
77

88
import SwiftUI
99

10+
struct ButtonModifier: ViewModifier {
11+
func body(content: Content) -> some View {
12+
content
13+
.frame(maxWidth: 300)
14+
.padding(.vertical, 20)
15+
.background(.thickMaterial)
16+
.clipShape(RoundedRectangle(cornerRadius: 20))
17+
.foregroundColor(.secondary)
18+
}
19+
}
20+
21+
extension View {
22+
func buttonstyle() -> some View {
23+
modifier(ButtonModifier())
24+
}
25+
}
26+
1027
struct ContentView: View {
28+
let moves = ["rock","paper","scissor"]
29+
@State private var shouldWin = Bool.random()
30+
@State private var appSelection = Int.random(in: 0...2)
31+
32+
@State private var showingScore = false
33+
@State private var score = 0
34+
@State private var currentTurn = 0
35+
@State private var scoreTitle = ""
36+
1137
var body: some View {
12-
Text("Hello, world!")
13-
.padding()
38+
ZStack {
39+
RadialGradient(gradient: Gradient(colors: [.blue, .black]), center: .center, startRadius: 2, endRadius: 650)
40+
.ignoresSafeArea()
41+
VStack(spacing: 30) {
42+
Spacer()
43+
Text("The Player should \(winOrLose(shouldWin))")
44+
.font(.largeTitle.weight(.bold))
45+
.foregroundColor(.white)
46+
47+
Text("The System Selected \(moves[appSelection])")
48+
.font(.subheadline.weight(.heavy))
49+
50+
Text("Select from the options Below")
51+
.font(.subheadline.weight(.heavy))
52+
53+
Spacer()
54+
Button("Rock 👊") {
55+
if shouldWin && moves[appSelection] == "scissor" {
56+
score += 1
57+
scoreTitle = "Correct"
58+
} else if !shouldWin && moves[appSelection] != "scissor" {
59+
score += 1
60+
scoreTitle = "Correct"
61+
}else {
62+
scoreTitle = "Wrong"
63+
}
64+
refreshOptions()
65+
66+
}.buttonstyle()
67+
68+
Button("Paper ✋") {
69+
if shouldWin && moves[appSelection] == "rock" {
70+
score += 1
71+
scoreTitle = "Correct"
72+
} else if !shouldWin && moves[appSelection] != "rock" {
73+
score += 1
74+
scoreTitle = "Correct"
75+
}else {
76+
scoreTitle = "Wrong"
77+
}
78+
refreshOptions()
79+
80+
}.buttonstyle()
81+
82+
83+
Button("Scissocr ✌️") {
84+
if shouldWin && moves[appSelection] == "paper" {
85+
score += 1
86+
scoreTitle = "Correct"
87+
} else if !shouldWin && moves[appSelection] != "paper" {
88+
score += 1
89+
scoreTitle = "Correct"
90+
}else {
91+
scoreTitle = "Wrong"
92+
}
93+
refreshOptions()
94+
}.buttonstyle()
95+
Spacer()
96+
97+
Text("Score : \(score)")
98+
.padding()
99+
}
100+
} .alert(scoreTitle, isPresented: $showingScore) {
101+
Button("Continue", action: askQuestion)
102+
}message : {
103+
if scoreTitle == "Wrong" {
104+
Text("You didn't choose the right option")
105+
}
106+
Text("Your Score is \(score)")
107+
}
108+
}
109+
110+
func winOrLose(_ status : Bool) -> String {
111+
if status == true {
112+
return "win"
113+
}
114+
return "lose"
115+
}
116+
117+
func refreshOptions() {
118+
showingScore = true
119+
120+
currentTurn += 1
121+
if currentTurn == 10 {
122+
score = 0
123+
currentTurn = 0
124+
}
125+
}
126+
func askQuestion() {
127+
shouldWin.toggle()
128+
appSelection = Int.random(in: 0...2)
14129
}
130+
15131
}
16132

17133
struct ContentView_Previews: PreviewProvider {
18134
static var previews: some View {
19135
ContentView()
136+
.preferredColorScheme(.dark)
20137
}
21138
}

0 commit comments

Comments
 (0)