-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.lua
93 lines (69 loc) · 1.83 KB
/
ai.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
local tobito = require "tobito"
local pack_fmt = "<L"
local function load_all_data(data_str)
local win = {}
local index = 1
while index < #data_str do
local int, next_index = string.unpack(pack_fmt, data_str, index)
local w = int >> 30
local state = int & 0x3fffffff
win[state] = (w == tobito.Top) and 1 or (w == tobito.Bottom) and -1 or 0
index = next_index
end
return win
end
local function max_for(elements, func)
local max = nil
local best = {}
for _,elem in ipairs(elements) do
local value = func(elem)
if #best == 0 or value > max then
best = {elem}
max = value
elseif value == max then
table.insert(best, elem)
end
end
return best
end
local function state_score(int, player)
local s = tobito.int_to_state(int)
local sum = 0
for cell = 0,tobito.MaxCell do
if s[cell] == player then
local row = cell // 3
if player == tobito.Top then
sum = sum + row
else
sum = sum + (4 - row)
end
end
end
return sum
end
local function decide_state(int, win, strategy, exclude)
local state = tobito.int_to_state(int)
local player = state.next_player
local other = player == tobito.Top and tobito.Bottom or tobito.Top
local m = (player == tobito.Top) and 1 or -1
local funcs = {}
funcs.aggressive = function(s)
return 10000 * m * (win[s] or 0) + 100 * state_score(s, player) - 1 * state_score(s, other)
end
funcs.balanced = function(s)
return 10000 * m * (win[s] or 0) + 100 * state_score(s, player) - 100 * state_score(s, other)
end
funcs.prudent = function(s)
return 10000 * m * (win[s] or 0) + 1 * state_score(s, player) - 100 * state_score(s, other)
end
local children = {}
for move,child in tobito.valid_moves(state, exclude) do
table.insert(children, child)
end
return max_for(children, funcs[strategy])
end
return
{
pack_fmt = pack_fmt,
decide_state = decide_state
}