-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentView.swift
More file actions
91 lines (77 loc) · 2.27 KB
/
ContentView.swift
File metadata and controls
91 lines (77 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// ContentView.swift
// WarCardGame
//
// Created by Sanjith Ponnusamy on 1/2/24.
//
import SwiftUI
struct ContentView: View {
@State var playerCard = "card7"
@State var cpuCard = "card13"
@State var playerScore = 0
@State var cpuScore = 0
var body: some View {
ZStack {
Spacer()
Image("background-plain").resizable().ignoresSafeArea()
Spacer()
VStack {
Spacer()
Image("logo")
Spacer()
HStack {
Spacer()
Image(playerCard)
Spacer()
Image(cpuCard)
Spacer()
}
Spacer()
Button {
deal()
} label: {
Image("button")
}
Spacer()
HStack {
Spacer()
VStack {
Text("Player")
.font(.headline)
.padding(.bottom, 10)
Text(String(playerScore))
.font(.largeTitle)
}
Spacer()
VStack {
Text("CPU")
.font(.headline)
.padding(.bottom, 10)
Text(String(cpuScore))
.font(.largeTitle)
}
Spacer()
}
.foregroundColor(.white)
Spacer()
}
}
}
func deal() {
/// Randomize the players card
let playerCardValue = Int.random(in: 2...14)
playerCard = "card" + String(playerCardValue)
/// Randomize the cpu's card
let cpuCardValue = Int.random(in: 2...14)
cpuCard = "card" + String(cpuCardValue)
/// update the scores
if playerCardValue > cpuCardValue {
playerScore += 1
} else if cpuCardValue > playerCardValue {
cpuScore += 1
}
}
}
#Preview {
ContentView()
}