forked from norm-ideal/processing-reversi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBan.pde
More file actions
324 lines (290 loc) · 6.85 KB
/
Ban.pde
File metadata and controls
324 lines (290 loc) · 6.85 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
final int KURO = 1;
final int SHIRO = -1;
final int AKI = 0;
final int SOTO = 255;
class Ban {
private int[][] b;
Ban()
{
b = new int[10][10];
for(int y=0; y<10; y++)
{
for(int x=0; x<10; x++)
{
b[x][y] = AKI;
if( x==0 || x==9 || y==0 || y==9 )
{
b[x][y] = SOTO;
}
else
{
b[x][y] = AKI;
}
}
}
b[4][4] = SHIRO;
b[5][5] = SHIRO;
b[4][5] = KURO;
b[5][4] = KURO;
}
Ban( Ban org ) {
// Normally we should call super() here,
// but it requires more time for unused initialization,
// so we call "new int" again here;
b = new int[10][10];
for(int i=0; i<10; i++)
for(int j=0; j<10; j++)
b[i][j] = org.b[i][j];
}
/*
Ban( int[][] value )
{
b = new int[10][10];
for(int i=0; i<10; i++)
for(int j=0; j<10; j++)
b[i][j] = value[i][j];
}
*/
int[][] get()
{
return b;
}
int turnSub(int c, int sx, int sy, int dx, int dy)
{
int teki = -c;
int result = 0;
sx += dx;
sy += dy;
while( b[sx][sy] == teki )
{
sx += dx;
sy += dy;
result++;
}
if( b[sx][sy] == c )
return result;
else
return 0;
}
int turn(int c, int sx, int sy)
{
int result;
result = 0;
if( b[sx][sy] != AKI )
return 0;
result += turnSub(c, sx, sy, 0, 1);
result += turnSub(c, sx, sy, 0, -1);
result += turnSub(c, sx, sy, 1, -1);
result += turnSub(c, sx, sy, 1, 0);
result += turnSub(c, sx, sy, 1, 1);
result += turnSub(c, sx, sy, -1, -1);
result += turnSub(c, sx, sy, -1, 0);
result += turnSub(c, sx, sy, -1, 1);
return result;
}
int putSub(int c, int sx, int sy, int dx, int dy)
{
int count = 0;
if( turnSub(c,sx,sy,dx,dy) == 0 )
return 0;
sx += dx; // sx = sx + dx;
sy += dy;
while( b[sx][sy] == -c )
{
b[sx][sy] = c;
count++;
sx += dx;
sy += dy;
}
return count;
}
int put(int c, int sx, int sy)
{
int result;
result = 0;
if( turn(c,sx,sy) == 0 )
return 0;
b[sx][sy] = c;
result += putSub(c, sx, sy, 0, 1);
result += putSub(c, sx, sy, 0, -1);
result += putSub(c, sx, sy, 1, -1);
result += putSub(c, sx, sy, 1, 0);
result += putSub(c, sx, sy, 1, 1);
result += putSub(c, sx, sy, -1, -1);
result += putSub(c, sx, sy, -1, 0);
result += putSub(c, sx, sy, -1, 1);
return result;
}
boolean isPlacable(int teban)
{
for(int y=1; y<=8; y++)
{
for(int x=1; x<=8; x++)
{
if(turn(teban, x, y) != 0)
{
return true;
}
}
}
return false;
}
int turnCount(int c, int sx, int sy)
{
int result;
result = 0;
if( b[sx][sy] != AKI )
return 0;
if( turnSub(c, sx, sy, 0, 1) > 0 )
result ++;
if( turnSub(c, sx, sy, 0, -1) > 0 )
result ++;
if( turnSub(c, sx, sy, 1, -1) > 0 )
result ++;
if( turnSub(c, sx, sy, 1, 0) > 0 )
result ++;
if( turnSub(c, sx, sy, 1, 1) > 0 )
result ++;
if( turnSub(c, sx, sy, -1, -1) > 0 )
result ++;
if( turnSub(c, sx, sy, -1, 0) > 0 )
result ++;
if( turnSub(c, sx, sy, -1, 1) > 0 )
result ++;
return result;
}
Move getMoveR (int teban, int depth, int passcount ) // R は Recursive の R
{
Move result = new Move();
if( DEBUG )
{
if( depth > MAXDEPTH - 3 )
{
for(int i=0; i<MAXDEPTH-depth; i++)
print("-");
println(passcount);
}
}
if( passcount >= 2 )
{
int kekka = 0;
for( int y=1; y<=8; y++)
for( int x=1; x<=8; x++)
{
if( b[x][y] == teban )
kekka ++;
if( b[x][y] == -teban )
kekka --;
}
if( kekka > 0 )
kekka += 10000;
if( kekka < 0 )
kekka -= 10000;
result.value = kekka;
return result;
}
if( depth == 0 )
{
result.value = banHyouka( teban ); // result の x,y には興味がない
return result; // 強制終了
}
result.value = -99999;
for( int y=1; y<=8; y++)
for( int x=1; x<=8; x++)
{
if( turn(teban, x, y) != 0 ) // 打てる場所が見つかったら
{
Ban nextban = new Ban(this);
nextban.put(teban, x, y); // 次の局面をつくる
Move nextmove = nextban.getMoveR(-teban, depth-1, 0 );
// 次の局面を相手の立場で評価する
nextmove.value = -nextmove.value;
if( depth == MAXDEPTH )
println( depth, x, y, nextmove.value );
if( result.value < nextmove.value )
{
result.value = nextmove.value;
result.x = x;
result.y = y;
}
}
}
if( result.value == -99999 ) // どこもに打てなかったら
{
// 自分は打てないので、相手の手番として盤面を評価する
result = getMoveR(-teban, depth, passcount+1);
// 相手の評価値をプラスマイナスひっくり返して自分の評価にする
result.value = -result.value;
result.value -= 10; // パスを強制されたので評価にペナルティ
}
return result;
}
int[] gameEnd()
{
int wc = 0;
int bc = 0;
for(int y=1; y<=8; y++)
{
for(int x=1; x<=8; x++)
{
if(b[x][y] == KURO)
bc++;
if(b[x][y] == SHIRO)
wc++;
}
}
return new int[] {bc, wc};
}
int banHyouka(int teban )
{
int result = 0;
for( int y=1; y<=8; y++)
for( int x=1; x<=8; x++)
{
if(b[x][y] == teban )
result += tensu[x][y];
if(b[x][y] == -teban )
result -= tensu[x][y];
if( (x == 1 || x == 8) && (y == 1 || y == 8 ))
{
if( b[x][y] == teban )
result += 100;
if( b[x][y] == -teban )
result -= 100;
}
}
return result;
}
int eval(int teban, int mx, int my)
{
Ban nextban = new Ban(this);
int count = 0;
int aitenobasho, bashopoint, muki;
nextban.put(teban, mx, my); // <- Tugi no sekai
int result;
int aitepoint = -999;
for(int y=1; y<=8; y++)
{
for(int x=1; x<=8; x++)
{
int c = nextban.turn(-teban, x, y);
if( c > 0 )
{
count++;
if( aitepoint < tensu[x][y] )
aitepoint = tensu[x][y];
}
}
}
aitepoint = aitepoint * (-5);
if( count == 0 )
aitenobasho = 100;
else
aitenobasho = -count;
bashopoint = tensu[mx][my] * 10;
muki = this.turnCount(teban, mx, my) * 2;
result = aitenobasho + aitepoint + bashopoint + muki;
println( mx, my, aitenobasho, aitepoint, bashopoint, muki, " = ", result);
return result;
}
}