-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.js
315 lines (298 loc) · 9.98 KB
/
robot.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
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
const attackFactor = 1;
const defenseFactor = 0.5;
/**
* 电脑评价棋面,获得最佳落子位置
* @param {*} squares
* @param {*} rowSize
* @param {*} colSize
* @param {*} targetCount
* @param {*} mySymbol
*/
function getBestEvaluatePoint(squares, rowSize, colSize, targetCount, mySymbol){
const bestPoints = [];
const pointScores = {};
let bestScore = Number.NEGATIVE_INFINITY;
for(let r = 0; r < rowSize; r++){
for(let c = 0; c < colSize; c++){
const score = evaluate(squares, r, c, rowSize, colSize, targetCount, mySymbol);
//console.log(r + ',' + c + ': ' + score);
if(score >= bestScore){
bestScore = score;
if(!pointScores['score-' + bestScore]){
pointScores['score-' + bestScore] = [];
}
pointScores['score-' + bestScore].push({r:r,c:c});
}
}
}
//从最佳位置中随机选择一个
return randomItem(pointScores['score-' + bestScore]);
}
/**
* 评价某一位置落子的棋面得分
* @param {*} squares
* @param {*} r 落子位置所在行,从0开始
* @param {*} c 落子位置所在列,从0开始
* @param {*} rowSize
* @param {*} colSize
* @param {*} targetCount
* @param {*} mySymbol
*/
function evaluate(squares, r, c, rowSize, colSize, targetCount, mySymbol){
if(squares[r][c]){
return Number.NEGATIVE_INFINITY;
}
return evaluateAttack(squares, r, c, rowSize, colSize, targetCount, mySymbol) * attackFactor +
evaluateDefense(squares, r, c, rowSize, colSize, targetCount, mySymbol) * defenseFactor;
}
/**
* 评价某一位置落子的棋面攻击方面的得分
* @param {*} squares
* @param {*} r
* @param {*} c
* @param {*} rowSize
* @param {*} colSize
* @param {*} targetCount
* @param {*} mySymbol
*/
function evaluateAttack(squares, r, c, rowSize, colSize, targetCount, mySymbol){
const myRowSeria = getMyRowSeria(squares, r, c, rowSize, colSize, targetCount, mySymbol);
const myColSeria = getMyColSeria(squares, r, c, rowSize, colSize, targetCount, mySymbol);
const myDiaLeftSeria = getMyDiaLeftSeria(squares, r, c, rowSize, colSize, targetCount, mySymbol);
const myDiaRightSeria = getMyDiaRightSeria(squares, r, c, rowSize, colSize, targetCount, mySymbol);
return evaluateAttackInSeria(myRowSeria.seria, myRowSeria.myIndex, targetCount, mySymbol) +
evaluateAttackInSeria(myColSeria.seria, myColSeria.myIndex, targetCount, mySymbol) +
evaluateAttackInSeria(myDiaLeftSeria.seria, myDiaLeftSeria.myIndex, targetCount, mySymbol) +
evaluateAttackInSeria(myDiaRightSeria.seria, myDiaRightSeria.myIndex, targetCount, mySymbol);
}
/**
* 评价某一位置落子的棋面防守方面的得分,这里简单处理为“等于若对方在此落子的攻击得分”
* @param {*} squares
* @param {*} r
* @param {*} c
* @param {*} rowSize
* @param {*} colSize
* @param {*} targetCount
* @param {*} mySymbol
*/
function evaluateDefense(squares, r, c, rowSize, colSize, targetCount, mySymbol){
const oppSymbol = mySymbol === 'X' ? 'O' : 'X';
return evaluateAttack(squares, r, c, rowSize, colSize, targetCount, oppSymbol);
}
/**
* 获取指定棋格所在行的所有棋格,及本身在此数组中的位置
* @param {*} squares
* @param {*} r
* @param {*} c
* @param {*} rowSize
* @param {*} colSize
* @param {*} targetCount
* @param {*} mySymbol
*/
function getMyRowSeria(squares, r, c, rowSize, colSize, targetCount, mySymbol){
const seria = [squares[r][c]];
let myIndex = 0;
for(let i = c - 1; i >= 0; i--){
seria.splice(0, 0, squares[r][i]);
myIndex++;
}
for(let i = c + 1; i < colSize; i++){
seria.push(squares[r][i]);
}
return {seria:seria, myIndex:myIndex};
}
/**
* 获取指定棋格所在列的所有棋格,及本身在此数组中的位置
* @param {*} squares
* @param {*} r
* @param {*} c
* @param {*} rowSize
* @param {*} colSize
* @param {*} targetCount
* @param {*} mySymbol
*/
function getMyColSeria(squares, r, c, rowSize, colSize, targetCount, mySymbol){
const seria = [squares[r][c]];
let myIndex = 0;
for(let i = r - 1; i >= 0; i--){
seria.splice(0, 0, squares[i][c]);
myIndex++;
}
for(let i = r + 1; i < rowSize; i++){
seria.push(squares[i][c]);
}
return {seria:seria, myIndex:myIndex};
}
/**
* 获取指定棋格所在左斜线的所有棋格,及本身在此数组中的位置
* @param {*} squares
* @param {*} r
* @param {*} c
* @param {*} rowSize
* @param {*} colSize
* @param {*} targetCount
* @param {*} mySymbol
*/
function getMyDiaLeftSeria(squares, r, c, rowSize, colSize, targetCount, mySymbol){
const seria = [squares[r][c]];
let myIndex = 0;
for(let i = r - 1, j = c + 1; i >= 0 && j < colSize; i--, j++){
seria.splice(0, 0, squares[i][j]);
myIndex++;
}
for(let i = r + 1, j = c - 1; i < rowSize && j >= 0; i++, j--){
seria.push(squares[i][j]);
}
return {seria:seria, myIndex:myIndex};
}
/**
* 获取指定棋格所在右斜线的所有棋格,及本身在此数组中的位置
* @param {*} squares
* @param {*} r
* @param {*} c
* @param {*} rowSize
* @param {*} colSize
* @param {*} targetCount
* @param {*} mySymbol
*/
function getMyDiaRightSeria(squares, r, c, rowSize, colSize, targetCount, mySymbol){
const seria = [squares[r][c]];
let myIndex = 0;
for(let i = r - 1, j = c - 1; i >= 0 && j >= 0; i--, j--){
seria.splice(0, 0, squares[i][j]);
myIndex++;
}
for(let i = r + 1, j = c + 1; i < rowSize && j < colSize; i++, j++){
seria.push(squares[i][j]);
}
return {seria:seria, myIndex:myIndex};
}
/**
* 计算棋格在所在行、列或斜线中的得分
* @param {*} seria
* @param {*} myIndex
* @param {*} targetCount
* @param {*} mySymbol
*/
function evaluateAttackInSeria(seria, myIndex, targetCount, mySymbol){
let mySymbolLength = 1, vMySymbolLength = 1;
let mySymbolStartStop = false, mySymbolEndStop = false;
let stopFactor = [1, 0.3, 0], stopCount = 0;
let continuityStartIndex = myIndex, continuityEndIndex = myIndex;
if(myIndex == 0){
stopCount++;
}
if(myIndex == seria.length - 1){
stopCount++;
}
for(let i = myIndex - 1; i >= 0; i--){
if(seria[i] === mySymbol){
if(!mySymbolStartStop){
mySymbolLength++;
continuityStartIndex--;
}
vMySymbolLength++;
}else if(!seria[i]){
mySymbolStartStop = true;
vMySymbolLength++;
}else{
if(!mySymbolStartStop){
stopCount++;
}
mySymbolStartStop = true;
break;
}
}
for(let i = myIndex + 1; i < seria.length; i++){
if(seria[i] === mySymbol){
if(!mySymbolEndStop){
mySymbolLength++;
continuityEndIndex++;
}
vMySymbolLength++;
}else if(!seria[i]){
mySymbolEndStop = true;
vMySymbolLength++;
}else{
if(!mySymbolEndStop){
stopCount++;
}
mySymbolEndStop = true;
break;
}
}
if(mySymbolLength == targetCount){
return 9999999999;
}
let score = 0;
if(vMySymbolLength >= targetCount){
//尝试评价不连续的情况
const discontinuityLikeCount = evaluateDisContinue(seria, mySymbol, targetCount, mySymbolLength, continuityStartIndex, continuityEndIndex);
const avgSymbolCount = mySymbolLength > discontinuityLikeCount ? mySymbolLength : discontinuityLikeCount;
score = Math.pow(targetCount, avgSymbolCount) * (avgSymbolCount * stopFactor[stopCount]);
}
return score;
}
/**
* 评价棋格在所在行、列或斜线中的“不连续情况”,转化为“相当于连续的几颗棋子”
* @param {*} seria
* @param {*} mySymbol
* @param {*} targetCount
* @param {*} mySymbolLength
* @param {*} continuityStartIndex
* @param {*} continuityEndIndex
*/
function evaluateDisContinue(seria, mySymbol, targetCount, mySymbolLength, continuityStartIndex, continuityEndIndex){
if(mySymbolLength == targetCount - 1){
return mySymbolLength;
}
let discontinuityLikeCount = 0;
const nullFactor = 0.7;
let discontinuityLikeStartOrientCount = 0;
let discontinuityLikeEndOrientCount = 0;
let flySymbolCount = 0;
let nullCount = 0;
let stopFactor = 1;
for(let i = 1; i <= targetCount - mySymbolLength; i++){
if(continuityStartIndex - i < 0){
stopFactor = 0;
break;
}else if(!seria[continuityStartIndex - i]){
nullCount++;
}else if(seria[continuityStartIndex - i] === mySymbol){
flySymbolCount++;
}else{
stopFactor = 0;
break;
}
}
if(flySymbolCount > 0){
discontinuityLikeStartOrientCount = flySymbolCount * stopFactor * Math.pow(nullFactor, nullCount);
}
flySymbolCount = 0;
nullCount = 0;
stopFactor = 1;
for(let i = 1; i <= targetCount - mySymbolLength; i++){
if(continuityEndIndex + i >= seria.length){
stopFactor = 0;
break;
}else if(!seria[continuityEndIndex + i]){
nullCount++;
}else if(seria[continuityEndIndex + i] === mySymbol){
flySymbolCount++;
}else{
stopFactor = 0;
break;
}
}
if(flySymbolCount > 0){
discontinuityLikeEndOrientCount = flySymbolCount * stopFactor * Math.pow(nullFactor, nullCount);
}
discontinuityLikeCount = discontinuityLikeStartOrientCount > discontinuityLikeEndOrientCount ?
discontinuityLikeStartOrientCount : discontinuityLikeEndOrientCount;
return mySymbolLength + discontinuityLikeCount;
}
function randomItem(array){
const randomIndex = Math.floor(Math.random()*array.length);
return array[randomIndex];
}