-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameLogic.go
71 lines (64 loc) · 1.24 KB
/
GameLogic.go
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
package main
func Terminal(S [][]string) bool {
if WinX(S) {
return true
}
if WinO(S) {
return true
}
if Draw(S) {
return true
}
return false
}
func Turn(turn int) int {
return 1 - turn
}
func WinX(S [][]string) bool {
for i := 0; i < len(S); i++ {
if S[i][0] == S[i][1] && S[i][1] == S[i][2] && S[i][0] == "X" {
return true
}
}
for j := 0; j < len(S); j++ {
if S[0][j] == S[1][j] && S[1][j] == S[2][j] && S[0][j] == "X" {
return true
}
}
if S[0][0] == S[1][1] && S[1][1] == S[2][2] && S[0][0] == "X" {
return true
}
if S[0][2] == S[1][1] && S[1][1] == S[2][0] && S[0][2] == "X" {
return true
}
return false
}
func WinO(S [][]string) bool {
for i := 0; i < len(S); i++ {
if S[i][0] == S[i][1] && S[i][1] == S[i][2] && S[i][0] == "O" {
return true
}
}
for j := 0; j < len(S); j++ {
if S[0][j] == S[1][j] && S[1][j] == S[2][j] && S[0][j] == "O" {
return true
}
}
if S[0][0] == S[1][1] && S[1][1] == S[2][2] && S[0][0] == "O" {
return true
}
if S[0][2] == S[1][1] && S[1][1] == S[2][0] && S[0][2] == "O" {
return true
}
return false
}
func Draw(S [][]string) bool {
for i := 0; i < len(S); i++ {
for j := 0; j < len(S[0]); j++ {
if S[i][j] == "." {
return false
}
}
}
return true
}