-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgobang-ai-fast.js
196 lines (184 loc) · 6.24 KB
/
gobang-ai-fast.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var AI = function() {
var root = this;
var AI = function() {}
var switchPlayer = function(crt) {
return 1 - crt;
}
var maxList = [0x7fffffff + 1];
for (var i = 1; i < 8; i++) {
maxList.push(maxList[i - 1] / 8);
}
var genRules = function(crt) {
var otr = switchPlayer(crt);
return [
[[crt, 5, 0], maxList[0]],
[[crt, 5, 1], maxList[0]],
[[crt, 5, 2], maxList[0]],
[[otr, 5, 0], -maxList[1]],
[[otr, 5, 1], -maxList[1]],
[[otr, 5, 2], -maxList[1]],
[[crt, 4, 1], maxList[2]],
[[crt, 4, 2], maxList[2]],
[[otr, 4, 2], -maxList[3]],
[[crt, 3, 2], maxList[4]],
[[otr, 3, 2], -maxList[5]],
[[otr, 4, 1], -maxList[5]],
[[crt, 2, 2], maxList[6]],
[[crt, 3, 1], maxList[6]],
[[otr, 2, 2], -maxList[6]],
[[otr, 3, 1], -maxList[6]],
[[crt, 2, 1], maxList[7]],
[[otr, 2, 1], -maxList[7]]
];
};
var rules = [];
rules[0] = genRules(0);
rules[1] = genRules(1);
var orgs = function(grid) {
var row = grid.length;
var col = grid[0].length;
return [
[_.map(_.range(0, col), function(v) {return [0, v];}),
[[1, 0], [1, 1], [1, -1]]],
[_.map(_.range(0, col), function(v) {return [row - 1, v];}),
[[-1, 1], [-1, -1]]],
[_.map(_.range(0, row), function(v) {return [v, 0];}),
[[0, 1]]]
];
}
var getRow = function(grid, org, dir) {
var row = grid.length;
var col = grid[0].length;
var i = org[0];
var j = org[1];
var ret = [[]];
var cnt = 0;
while (i < row && j < col && i >= 0 && j >= 0) {
if (_.isEmpty(ret[cnt]) || _.last(ret[cnt]) == grid[i][j])
ret[cnt].push(grid[i][j]);
else {
cnt += 1;
ret[cnt] = [];
ret[cnt].push(grid[i][j]);
}
i += dir[0];
j += dir[1];
}
return ret;
}
var calcState = function(grid) {
var ret = {};
var createOrIncrease = function(ctr, l, h) {
if (!ret[ctr]) ret[ctr] = {};
if (!ret[ctr][l]) ret[ctr][l] = {};
if (!ret[ctr][l][h])
ret[ctr][l][h] = 1;
else
ret[ctr][l][h]++;
}
_.each(orgs(grid), function(org) {
for (var i = 0; i < org[0].length; i++) { // orgs
for (var j = 0; j < org[1].length; j++) { // dirs
var rows = getRow(grid, org[0][i], org[1][j]);
for (var k = 0; k < rows.length; k++) {
if (rows[k][0] == -1 || rows[k].length < 2) continue;
var head = 0;
if (k != 0 && rows[k - 1][0] == -1) head += 1;
if (k != rows.length - 1 && rows[k + 1][0] == -1) head += 1;
createOrIncrease(rows[k][0], rows[k].length, head);
}
}
}
});
return ret;
}
var chance = function(grid, crt) {
var state = calcState(grid);
var getInState = function(r) {
var tmp = state[r[0]];
if (!tmp) return 0;
tmp = tmp[r[1]];
if (!tmp) return 0;
tmp = tmp[r[2]];
if (!tmp) return 0;
return tmp;
};
var ret = 0;
_.each(rules[crt], function(rule) {
ret += getInState(rule[0]) * rule[1];
});
return ret;
}
var isOver = function(grid, cell) {
var dirs = [
[0, 1], [1, 0],
[1, 1], [-1, 1]
];
var crt = grid[cell[0]][cell[1]];
var row = grid.length;
var col = grid[0].length;
var rets = _.map(dirs, function(dir) {
var cnt = 1;
for (var i = -1; i <= 1; i += 2) {
var sy = cell[0] + i * dir[0];
var sx = cell[1] + i * dir[1];
while (sy < row && sy >= 0 && sx < col && sx >=0 &&
grid[sy][sx] == crt) {
cnt++;
sy += i * dir[0];
sx += i * dir[1];
}
}
return cnt;
});
if (_.max(rets) >= 5)
return true;
else
return false;
}
AI.play = function(grid, crtPlayer, depth, maxChance) {
if (depth == 0)
return [chance(grid, crtPlayer), null];
var row = grid.length;
var col = grid[0].length;
var notAlone = function(grid, r, c) {
for (var i = -1; i <= 1; i++)
for (var j = -1; j <= 1; j++) {
var newi = i + r;
var newj = j + c;
if (newi < 0 || newj < 0 || newi >= grid.length || newj >= grid[0].length)
continue;
if ((i != 0 || j != 0) &&
(grid[i + r][j + c] == 0 || grid[i + r][j + c] == 1))
return true;
}
return false;
}
var ret = [-0x7fffffff * 100, null];
for (var i = 0; i < row; i++) {
for (var j = 0; j < col; j++) {
if (grid[i][j] != -1) continue;
if (!notAlone(grid, i, j)) continue;
grid[i][j] = crtPlayer;
if (depth != 1) {
if (isOver(grid, [i, j])) {
grid[i][j] = -1;
return [0x7fffffff * 10, [i, j]];
}
}
var enm_ret = AI.play(grid, switchPlayer(crtPlayer), depth - 1, -ret[0]);
grid[i][j] = -1;
var enm_cs = -enm_ret[0];
if (maxChance && enm_cs > maxChance)
return [enm_cs, [i, j]];
if (enm_cs > ret[0])
ret = [enm_cs, [i, j]];
else if (enm_cs == ret[0] && Math.random() < 0.5) {
ret = [enm_cs, [i, j]];
}
}
}
return ret;
}
return AI;
}();