-
Notifications
You must be signed in to change notification settings - Fork 12
/
GameView.swift
154 lines (138 loc) · 4.69 KB
/
GameView.swift
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import SwiftUI
struct GameView: View {
@ObservedObject var viewModel = GameViewModel()
@State private var showingGameOver = false
@State private var showingWinAlert = false
var body: some View {
VStack {
Text("2048")
.font(.largeTitle)
.fontWeight(.bold)
.padding(.top)
Text("Join the numbers and get to the 2048 tile!")
.font(.subheadline)
.padding(.bottom)
Spacer(minLength: 20)
VStack {
VStack {
Text("Score: \(viewModel.score)")
.font(.title)
.bold()
Text("High Score: \(viewModel.highScore)")
.font(.title2)
}
.padding()
VStack(spacing: 5) {
ForEach(0..<viewModel.gridSize, id: \.self) { row in
HStack(spacing: 5) {
ForEach(0..<viewModel.gridSize, id: \.self) { column in
CellView(number: viewModel.grid[row][column])
.animation(.easeInOut, value: viewModel.grid[row][column])
}
}
}
}
.padding()
.background(RoundedRectangle(cornerRadius: 10).fill(Color(.systemGray6)))
.shadow(radius: 5)
}
.padding()
Spacer()
Spacer()
}
.gesture(
DragGesture()
.onEnded { gesture in
let horizontalAmount = gesture.translation.width as CGFloat
let verticalAmount = gesture.translation.height as CGFloat
if abs(horizontalAmount) > abs(verticalAmount) {
viewModel.swipe(direction: horizontalAmount > 0 ? .right : .left)
}
else {
viewModel.swipe(direction: verticalAmount > 0 ? .down : .up)
}
if viewModel.isGameOver() {
showingGameOver = true
}
if viewModel.grid.contains(where: { $0.contains(2048) }) {
showingWinAlert = true
}
}
)
.alert(isPresented: $showingGameOver) {
Alert(
title: Text("Game Over"),
message: Text("Your score: \(viewModel.score)"),
dismissButton: .default(Text("Restart")) {
restartGame()
}
)
}
.alert("Congratulations!", isPresented: $showingWinAlert) {
Button("Continue", role: .cancel) { showingWinAlert = false }
} message: {
Text("You've reached 2048!")
}
.background(Color(.systemBackground))
}
private func restartGame() {
viewModel.grid = Array(repeating: Array(repeating: 0, count: viewModel.gridSize), count: viewModel.gridSize)
viewModel.score = 0
viewModel.addNewNumber()
viewModel.addNewNumber()
showingGameOver = false
}
}
struct CellView: View {
let number: Int
var body: some View {
Text(number == 0 ? "" : "\(number)")
.frame(width: 70, height: 70)
.background(colorForValue(number))
.foregroundColor(.white)
.font(.title.bold())
.cornerRadius(10)
.scaleEffect(number > 0 ? 1.0 : 0.8)
.opacity(number > 0 ? 1 : 0.5)
}
private func colorForValue(_ value: Int) -> Color {
switch value {
case 2:
return Color.yellow
case 4:
return Color.orange
case 8:
return Color.pink
case 16:
return Color.purple
case 32:
return Color.red
case 64:
return Color.red.opacity(0.8)
case 128:
return Color.blue
case 256:
return Color.blue.opacity(0.8)
case 512:
return Color.green
case 1024:
return Color.green.opacity(0.8)
case 2048:
return Color.gold
case 4096:
return Color.indigo
case 8192:
return Color.purple.opacity(0.5)
default:
return Color.gray.opacity(0.3)
}
}
}
extension Color {
static let gold = Color(red: 1, green: 215/255, blue: 0)
}
struct GameView_Previews: PreviewProvider {
static var previews: some View {
GameView()
}
}