-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.js
More file actions
328 lines (277 loc) · 6.31 KB
/
Copy pathalgorithm.js
File metadata and controls
328 lines (277 loc) · 6.31 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* AI 算法类
* @param row 棋盘的大小
* @constructor 初始化 AI 算法类
*/
function Algorithm(row) {
this.ROW = row;
this.blankBoard = [];
this.blackBoard = [];
this.whiteBoard = [];
this.weightTable = {
/**
* 权重表
* - 我方将要拥有的棋子数量
* + 对方将要拥有的棋子数量
*/
'2-': 10,
'2+': 1000,
'3-': 1500,
'3+': 10000,
'4-': 15000,
'4+': 100000,
'5-': 900000,
'5+': 1000000
};
/**
* 初始化参数
* @returns {Algorithm} 返回当前对象
*/
this.init = function() {
var i, j, key;
for (i = 0; i < this.ROW; i++) {
for (j = 0; j < this.ROW; j++) {
key = i + '_' + j;
this.blankBoard[key] = {
x: i,
y: j
};
}
}
this.blackBoard = [];
this.whiteBoard = [];
return this;
};
/**
* AI 下棋的坐标
* @returns {*} 返回一个坐标对象
*/
this.start = function() {
var badIndex, goodIndex; // 拦截棋位置,优势棋位置
var badNum = -1, // 威胁数值,优势数值
goodNum = -1;
var black = this.blackBoard, //黑棋和白棋对象
white = this.whiteBoard;
//求出需拦截的位置和优势的位置
for (var blank in this.blankBoard) {
var x = this.blankBoard[blank].x,
y = this.blankBoard[blank].y;
var bad = this.evaluate(x, y, black, white);
if (bad > badNum) {
badNum = bad;
badIndex = blank;
}
var good = this.evaluate(x, y, white, black);
if (good > goodNum) {
goodNum = good;
goodIndex = blank;
}
}
//判断是否需要拦截
if (goodNum > badNum) {
return this.blankBoard[goodIndex];
} else {
return this.blankBoard[badIndex];
}
};
/**
* 坐标合成字符串
* @param x 坐标 x
* @param y 坐标 y
* @returns {string} 返回一个字符串 x_y
*/
this.coordsToString = function(x, y) {
return x + '_' + y;
};
/**
* AI 胜率评估
* @param posX 空白位置的 x 轴
* @param posY 空白位置的 y 轴
* @param other 对方棋子
* @param we 我方棋子
* @returns {number} 胜率指数
*/
this.evaluate = function(posX, posY, other, we) {
//威胁权重
var table = {
'2-': 0,
'2+': 0,
'3-': 0,
'3+': 0,
'4-': 0,
'4+': 0,
'5-': 0,
'5+': 0
};
var x = posX, y = posY,
horizon = 1,
vertical = 1,
slash = 1,
backslash = 1;
//判断对方横坐标所有棋子
while (other[this.coordsToString(x, y + 1)]) {
horizon++;
y++;
}
x = posX;
y = posY;
while (other[this.coordsToString(x, y - 1)]) {
horizon++;
y--;
}
//我方棋子的统计
if (we[this.coordsToString(x, y + 1)] || we[this.coordsToString(x, y - 1)]) {
table[horizon + '-']++;
} else {
table[horizon + '+']++;
}
x = posX;
y = posY;
//判断对方竖坐标所有棋子
while (other[this.coordsToString(x + 1, y)]) {
vertical++;
x++;
}
x = posX;
y = posY;
while (other[this.coordsToString(x - 1, y)]) {
vertical++;
x--;
}
if (we[this.coordsToString(x + 1, y)] || we[this.coordsToString(x - 1, y)]) {
table[vertical + '-']++;
} else {
table[vertical + '+']++;
}
x = posX;
y = posY;
//判断对方斜线坐标所有棋子
while (other[this.coordsToString(x + 1, y + 1)]) {
slash++;
x++;
y++;
}
x = posX;
y = posY;
while (other[this.coordsToString(x - 1, y - 1)]) {
slash++;
x--;
y--;
}
if (we[this.coordsToString(x + 1, y + 1)] || we[this.coordsToString(x - 1, y - 1)]) {
table[slash + '-']++;
} else {
table[slash + '+']++;
}
x = posX;
y = posY;
//判断对方反斜线坐标所有棋子
while (other[this.coordsToString(x + 1, y - 1)]) {
backslash++;
x++;
y--;
}
x = posX;
y = posY;
while (other[this.coordsToString(x - 1, y + 1)]) {
backslash++;
x--;
y++;
}
if (we[this.coordsToString(x + 1, y - 1)] || we[this.coordsToString(x - 1, y + 1)]) {
table[backslash + '-']++;
} else {
table[backslash + '+']++;
}
delete table['1+'];
delete table['1-'];
//求出权重值
var score = 0;
for (var key in table) {
score += this.weightTable[key] * table[key];
}
return score;
};
/**
* 判断胜负
* @param we 指定需要判断的角色,true 是自己,false 是 AI
* @param posX 棋子的 x 轴
* @param posY 棋子的 y 轴
* @returns {boolean} 判断是否可以连成 5 个棋子
*/
this.isWinner = function(we, posX, posY) {
posX = Number(posX);
posY = Number(posY);
var index = this.coordsToString(posX, posY);
var role = null;
var data = {
x: posX,
y: posY
};
delete this.blankBoard[index];
//判断角色
if (we) {
role = this.blackBoard;
role[index] = data;
} else {
role = this.whiteBoard;
role[index] = data;
}
var x = posX, y = posY,
horizon = 1,
vertical = 1,
slash = 1,
backslash = 1;
while (role[this.coordsToString(x, y + 1)]) {
horizon++;
y++;
}
x = posX;
y = posY;
while (role[this.coordsToString(x, y - 1)]) {
horizon++;
y--;
}
x = posX;
y = posY;
while (role[this.coordsToString(x + 1, y)]) {
vertical++;
x++;
}
x = posX;
y = posY;
while (role[this.coordsToString(x - 1, y)]) {
vertical++;
x--;
}
x = posX;
y = posY;
while (role[this.coordsToString(x + 1, y + 1)]) {
slash++;
x++;
y++;
}
x = posX;
y = posY;
while (role[this.coordsToString(x - 1, y - 1)]) {
slash++;
x--;
y--;
}
x = posX;
y = posY;
while (role[this.coordsToString(x + 1, y - 1)]) {
backslash++;
x++;
y--;
}
x = posX;
y = posY;
while (role[this.coordsToString(x - 1, y + 1)]) {
backslash++;
x--;
y++;
}
return Math.max(horizon, vertical, slash, backslash) >= 5;
};
}