-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.jl
155 lines (142 loc) · 3.72 KB
/
game.jl
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
155
mutable struct Game
states::Array{Int8, 2}
turn::Int
size::Int
inarow::Int
isrunning::Bool
function Game(size::Int; row=3)
return new(zeros(Int8, size, size), 0, size, row, true)
end
function Game(states::Array{Int8, 2}, turn::Int, size::Int, row::Int, active=true)
return new(states, turn, size, row, active)
end
end
Base.show(io::IO, g::Game) = print(io, "Game<$(g.size)x$(g.size)> $(g.isrunning ? "running" : "gameover")…")
function makecopy(g::Game)
return Game(copy(g.states), g.turn, g.size, g.inarow, g.isrunning)
end
function nodemove(g::Game, pos::Int; with_check=true)
for i in 1:length(g.states)
mv = 0
for i in 1:length(g.states)
if g.states[i] == 0
mv += 1
(mv == pos) && (return legalmove(g, i, with_check=with_check))
end
end
end
end
# NOTE: Tested
function legalmove(g::Game, pos::Int; with_check=true)
g.states[pos] += g.turn % 2 == 0 ? 1 : -1
g.turn += 1
(with_check) && (return check(g))
nothing
end
# NOTE: Tested
# Shortly named utility function (SHOW BOARD)
function sh(g::Game)
println("Board")
for i in 1:length(g.states)
if g.states[i] == 1
print("X ")
elseif g.states[i] == -1
print("O ")
else
print("- ")
end
(i % g.size == 0) && println()
end
end
# NOTE: Tested
function diagCheckAsc(g::Game, x::Int, y::Int)
inarow = 1
while x < g.size && y > 1
if g.states[x, y] == g.states[x + 1, y - 1] != 0
inarow += 1
(inarow >= g.inarow) && (return g.states[x, y])
else
inarow = 1
end
x += 1
y -= 1
end
return 0
end
# NOTE: Tested
function diagCheckDesc(g::Game, x::Int, y::Int)
inarow = 1
while x < g.size && y < g.size
if g.states[x, y] == g.states[x + 1, y + 1] != 0
inarow += 1
(inarow >= g.inarow) && (return g.states[x, y])
else
inarow = 1
end
x += 1
y += 1
end
return 0
end
# NOTE: Tested
function diagcheck(b::Game)
ksize = b.size - b.inarow + 1
# descending
res = diagCheckDesc(b, 1, 1)
res != 0 && return res
for j in 2:ksize
res = diagCheckDesc(b, 1, j)
res != 0 && return res
res = diagCheckDesc(b, j, 1)
res != 0 && return res
end
# ascending
for j in 0:(ksize-2)
res = diagCheckAsc(b, 1, b.inarow + j)
res != 0 && return res
res = diagCheckAsc(b, 2 + j, b.size)
res != 0 && return res
end
res = diagCheckAsc(b, 1, b.size)
res != 0 && return res
# else
return 0
end
# NOTE: Tested
function check(board::Game)::Int8
# disable isrunning flag
board.isrunning = false
# check horizontal/vertical wins
for j in 1:(board.size)
σ = 1
ρ = 1
for i in 1:(board.size - 1)
# rows
if (board.states[i, j] == board.states[i + 1, j]) && (board.states[i, j] != 0)
σ += 1
if σ >= board.inarow
return board.states[i, j]
end
else
σ = 1
end
# columns
if (board.states[j, i] == board.states[j, i + 1]) && (board.states[j, i] != 0)
ρ += 1
if ρ >= board.inarow
return board.states[j, i]
end
else
ρ = 1
end
end
end
diagres = diagcheck(board)
diagres != 0 && return diagres
if board.turn >= board.size ^ 2
return 0
end
# re-enable isrunning flag
board.isrunning = true
return 0
end