-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentView.swift
More file actions
102 lines (89 loc) · 3.16 KB
/
ContentView.swift
File metadata and controls
102 lines (89 loc) · 3.16 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
92
93
94
95
96
97
98
99
100
101
102
// ContentView.swift(修正済み)
import SwiftUI
struct ContentView: View {
@StateObject private var board = Board()
@State private var selectedColor: Stone.Color = .black
@State private var assistEnabled = true
var body: some View {
VStack {
HStack {
Toggle("Assist", isOn: $assistEnabled)
.padding()
Spacer()
Button(action: {
board.reset()
}) {
Text("Reset")
.padding()
}
}
Grid(Board.size) { row, col in
let stone = board.stones[row][col]
ZStack {
Circle()
.stroke(Color.gray, lineWidth: 1)
.frame(width: 40, height: 40)
if let stoneColor = stone?.color {
Circle()
.fill(stoneColor == .black ? Color.black : Color.white)
.shadow(radius: 2)
.frame(width: 36, height: 36)
.opacity(stoneColor == .black ? 1.0 : 0.8)
.scaleEffect(1.1)
.animation(.easeIn(duration: 0.2), value: stone)
}
if assistEnabled, let assistInfo = board.assistInfo[row][col] {
Circle()
.stroke(assistInfo.color, lineWidth: 2)
.opacity(assistInfo.description == "Winning line" ? 1.0 : 0.6)
.frame(width: 42, height: 42)
.animation(
.easeInOut(duration: 0.5)
.repeatForever(autoreverses: true),
value: assistInfo.description
)
}
}
.onTapGesture {
if stone == nil {
withAnimation {
board.placeStone(at: (row, col), color: selectedColor)
selectedColor = selectedColor == .black ? .white : .black
}
}
}
}
.frame(width: 600, height: 600)
Text(board.gameStatus)
.font(.headline)
.padding()
}
.onAppear {
board.analyzeBoard()
}
}
}
struct Grid<Content: View>: View {
let size: Int
let content: (Int, Int) -> Content
init(_ size: Int, @ViewBuilder content: @escaping (Int, Int) -> Content) {
self.size = size
self.content = content
}
var body: some View {
VStack(spacing: 0) {
ForEach(0..<size, id: \.self) { row in
HStack(spacing: 0) {
ForEach(0..<size, id: \.self) { col in
content(row, col)
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}