-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentView.swift
63 lines (60 loc) · 2.54 KB
/
ContentView.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
import SwiftUI
struct ContentView: View {
@ObservedObject var game = SwiftToeGame()
@ObservedObject var audioManager = AudioManager.shared
@Environment(\.presentationMode) var presentationMode
var body: some View {
GeometryReader { geometry in
let tileSize = min(geometry.size.width, geometry.size.height) / 3.5
VStack(spacing: 10) {
ForEach(0..<3) { i in
HStack(spacing: 10) {
ForEach(0..<3) { j in
Button(action: {
withAnimation(.easeInOut(duration: 0.15)) {
game.makeMove(x: i, y: j)
}
}) {
Text(game.board[i][j])
.font(.system(size: tileSize * 0.3))
.frame(width: tileSize, height: tileSize)
.background(game.board[i][j] == "X" ? Color.blue.opacity(0.6) : game.board[i][j] == "O" ? Color.orange.opacity(0.6) : Color.gray)
.foregroundColor(game.board[i][j] == "X" ? .white : game.board[i][j] == "O" ? .black : .white)
}
.scaleEffect(game.pressedTile?.x == i && game.pressedTile?.y == j ? 1.1 : 1)
}
}
}
Spacer()
Text("Score: X - \(game.scoreX) | O - \(game.scoreO)")
Text(game.message)
Spacer()
Text("Current Player: \(game.currentPlayer)")
}
.padding()
.alert(isPresented: $game.showAlert) {
Alert(title: Text(game.message),
message: Text("Do you want to play again?"),
primaryButton: .default(Text("Yes"), action: {
game.reset()
}),
secondaryButton: .cancel(Text("No"), action: {
presentationMode.wrappedValue.dismiss()
})
)
}
}
.onAppear {
game.newGame()
audioManager.playBackgroundMusic()
}
.navigationBarItems(trailing:
Button(action: {
audioManager.toggleMute()
}) {
Image(systemName: audioManager.isMuted ? "speaker.slash.fill" : "speaker.fill")
.padding()
}
)
}
}