-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.lua
143 lines (128 loc) · 3.42 KB
/
logic.lua
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
function is_spot_filled()
return STONES[CURRENT.x][CURRENT.y]
end
function on_board(x, y)
return x > 0 and x <= SIZE and y > 0 and y <= SIZE
end
function is_ko()
if #RECORD > 3 then
result = RECORD[#RECORD].state == RECORD[#RECORD - 2].state
end
return false or result
end
function adjacent(x, y)
local to_return = {}
local directions = {
{ x = 0, y = 1 },
{ x = 1, y = 0 },
{ x = 0, y = -1 },
{ x = -1, y = 0 },
}
for _, direction in ipairs(directions) do
local new_x = x + direction.x
local new_y = y + direction.y
table.insert(to_return, { x = new_x, y = new_y })
end
return to_return
end
function place_stone()
if is_spot_filled() then
love.audio.play(ILLEGAL_PLACEMENT_SOUND)
return
end
local directions = adjacent(CURRENT.x, CURRENT.y)
to_remove = {}
for _, direction in ipairs(directions) do
local x, y = direction.x, direction.y
if on_board(x, y) and STONES[x][y] ~= nil and STONES[x][y].color ~= TO_PLAY then
table.insert(to_remove, capture(STONES[x][y], -TO_PLAY, CURRENT))
end
end
is_captured = false
for _, group in pairs(to_remove) do
is_captured = unplace_stones(group) or is_captured
end
-- liberty check
for _, direction in ipairs(directions) do
local x, y = direction.x, direction.y
if on_board(x, y) and STONES[x][y] == nil then
placement()
return
end
end
-- self-atari check
for _, direction in ipairs(directions) do
local x, y = direction.x, direction.y
if
on_board(x, y)
and STONES[x][y] ~= nil
and STONES[x][y].color == TO_PLAY
and not capture(STONES[x][y], TO_PLAY, CURRENT)
then
placement()
return
end
end
love.audio.play(ILLEGAL_PLACEMENT_SOUND)
end
function placement()
STONES:add(TO_PLAY, CURRENT.x, CURRENT.y)
RECORD:add()
str_pretty_print(stones_to_str(STONES))
JUST_PLAYED = STONES[CURRENT.x][CURRENT.y] -- used by GTP engine, replace
--
local random_index = math.random(1, #STONE_PLACEMENT_SOUND)
TO_PLAY = -TO_PLAY
DRAW_AFTER_PLACE = false
if is_ko() then
RECORD:undo()
love.audio.play(ILLEGAL_PLACEMENT_SOUND)
return
end
if is_captured then
random_index = math.random(1, #STONE_CAPTURE_SOUND)
love.audio.play(STONE_CAPTURE_SOUND[random_index])
else
love.audio.play(STONE_PLACEMENT_SOUND[random_index])
end
end
function unplace_stones(group)
if not group then
return false
end
for _, stone in pairs(group) do
local x = stone.x
local y = stone.y
STONES[x][y] = nil
end
return true
end
function capture(start, color, placed)
STONES[placed.x][placed.y] = placed
local to_check = { start }
local to_remove = {}
local checked = {}
while #to_check > 0 do
local curr = table.remove(to_check)
local x, y = curr.x, curr.y
checked[tostring(x .. "-" .. y)] = curr
if on_board(x, y) and STONES[x][y] == nil then
STONES[CURRENT.x][CURRENT.y] = nil
return false
elseif STONES[x][y].color ~= color then
goto continue
elseif STONES[x][y].color == color then
to_remove[curr] = curr
local directions = adjacent(x, y)
for _, direction in ipairs(directions) do
x, y = direction.x, direction.y
if on_board(x, y) and not checked[tostring(x .. "-" .. y)] then
table.insert(to_check, { x = x, y = y })
end
end
end
::continue::
end
STONES[CURRENT.x][CURRENT.y] = nil
return to_remove
end