-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomCPU.java
More file actions
561 lines (503 loc) · 15.7 KB
/
RandomCPU.java
File metadata and controls
561 lines (503 loc) · 15.7 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
import java.util.*;
class Rev_Status{
// 開放度判定
public int[] pos ={-1,-1}; // 最善手
public int openval = 0; // 配置
public Rev_Status(){
int[] tmp = {-1,-1};
pos = tmp;
openval = -1;
}
}
public class RandomCPU {
int color; //BLACK or WHITE
Opening open = new Opening();
int WT1[][]= //OSERO ウエイトテーブル
{ { 30 ,-12, 0, -1, -1, 0,-12,30 },
{-12,-15, -3, -3, -3, -3,-15,-12 },
{ 0 , -3, 0, -1, -1, 0, -3, 0 },
{ -1, -3, -1, -1, -1, -1, -3, -1 },
{ -1, -3, -1, -1, -1, -1, -3, -1 },
{ 0, -3, 0, -1, -1, 0, -3, 0 },
{-12,-15, -3, -3, -3, -3,-15,-12 },
{ 30,-12, 0, -1, -1, 0,-12,30 },
};
public RandomCPU(){
color = -1;
}
void Tedomari(GameState state){
int[] sumicnt = new int[4];
// Area0
for(int y=0; y<=1; y++){
for(int x=0; x<=1; x++){
if(state.data[x][y] != 0)
sumicnt[0]++;
}
}
//Area1
for(int y=6; y<=7; y++){
for(int x=0; x<=1; x++){
if(state.data[x][y] != 0)
sumicnt[1]++;
}
}
//Area2
for(int y=0; y<=1; y++){
for(int x=6; x<=7; x++){
if(state.data[x][y] != 0)
sumicnt[2]++;
}
}
//Area3
for(int y=6; y<=7; y++){
for(int x=6; x<=7; x++){
if(state.data[x][y] != 0)
sumicnt[3]++;
}
}
if(sumicnt[0] >= 3 ){
WT1[0][0] = 50;
WT1[0][1] = 50;
WT1[1][0] = 50;
WT1[1][1] = 50;
}
if(sumicnt[1] >= 3 ){
WT1[0][6] = 50;
WT1[0][7] = 50;
WT1[1][6] = 50;
WT1[1][7] = 50;
}
if(sumicnt[2] >= 3 ){
WT1[6][0] = 50;
WT1[7][0] = 50;
WT1[6][1] = 50;
WT1[7][1] = 50;
}
if(sumicnt[3] >= 3 ){
WT1[6][6] = 50;
WT1[6][7] = 50;
WT1[7][6] = 50;
WT1[7][7] = 50;
}
/* System.out.printf("---------WT1----------%n");
for(int y=0; y<8; y++){
for(int x=0; x<8; x++){
System.out.printf("%3d",WT1[x][y]);
}
System.out.printf("%n");
}
*/
}
int[] decide(GameState state,int cpulevel){
ArrayList<int[]> array = new ArrayList<int[]>();
//盤面の空マスを置けるかチェック
for(int y=0; y<8; y++){
for(int x=0; x<8; x++){
//すでに駒があるときはパス
if(state.data[x][y] != 0)
continue;
//置けるマスのとき、候補として記憶
if(state.canReverse(x, y) == true){
int pos[] = {x,y};
array.add(pos);
}
}
}
//おける場所がない場合
if(array.size() <= 0){
int pos[] = {-1, -1};
return pos;
}
// 定石判定
if( cpulevel == 2 && state.turn <= 10 ){
int opening[] = Check_Opening(state, state.data); // 定石で求めた位置
if(opening != null ) return opening;
}
Tedomari(state); //手どまりでスミの重み変更
int index = 0;
//////////////////////////////////////////////
//0:ランダムで打ち手を選択
//1:盤の重みの最大となる打ち手を選択
//2:開放度理論
//////////////////////////////////////////////
if(cpulevel == 0){
//ランダム選択
Random rnd = new Random();
index = rnd.nextInt(array.size());
return array.get(index);
}else if(cpulevel == 1){
//打ち込み可能場所の先頭の重み保存(max探索の為)
int postmp[] = array.get(0);
int x = postmp[0]; int y = postmp[1];
int max = WT1[x][y];
//最大重み探索
for(int i=0;i<array.size();i++){
int pos[] = array.get(i);
x = pos[0]; y = pos[1];
if(max <= WT1[x][y] ){
max = WT1[x][y]; //重みの最大値更新
index = i; //重み最大のインデックス更新
}
}
return array.get(index);
}else if(cpulevel == 3){
///////////////1手後の盤面作成///////////////////////
GameState[] nextstate = new GameState[array.size()];
for(int i=0;i<array.size();i++){
nextstate[i] = new GameState();
}
for(int ban=0;ban<array.size();ban++){
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
nextstate[ban].data[i][j] = state.data[i][j];
}
}
}
//次の盤面状態
// System.out.printf("次の盤面の可能性%n");
for(int ban=0;ban<array.size();ban++){
int pos[] = array.get(ban);
int x = pos[0]; int y = pos[1];
nextstate[ban].player *= -1; //プレーヤー反転
nextstate[ban].put(x,y); //候補手を置く
// System.out.printf("-------%d-------%n",ban);
for(int j=0;j<8;j++){
for(int i=0;i<8;i++){
// System.out.printf("%d,",nextstate[ban].data[i][j]);
}
// System.out.printf("%n");
}
}
///////////////次の手で反転するコマ/////////////////////
Rev_Status[][] ReList = new Rev_Status[array.size()][18];
for(int j=0;j<18;j++){
for(int i=0;i<array.size();i++){
ReList[i][j] = new Rev_Status();
}
}
//反転したコマの位置保存
for(int ban=0;ban<array.size();ban++){
// System.out.printf("-------%d-------%n",ban);
int revcnt=0;
for(int y=0; y<8; y++){
for(int x=0; x<8; x++){
//反転した場所を保存
int put[] = array.get(ban); //置いた場所
int tx=put[0]; int ty=put[1];
//反転してかつ置いた場所以外の場所保存
if(nextstate[ban].data[x][y] != state.data[x][y] && (x!=put[0] || y!=put[1]) ){
int pos[] = {x,y};
ReList[ban][revcnt].pos = pos;
revcnt++;
}
}
}
}
//////////////////////開放度算出部/////////////////////
// System.out.printf("--------開放値算出部-----------%n");
int[] opval = new int[array.size()];
for(int a=0;a<array.size();a++){
int pos[] = array.get(a);
// System.out.printf("置いたマス--------(%d,%d)-------------------%n",pos[0],pos[1]);
for(int b=0;ReList[a][b].pos[0]!=-1;b++){
int px = ReList[a][b].pos[0]; int py = ReList[a][b].pos[1];
// System.out.printf(" 反転したマス(x,y) = (%d,%d) %n",px,py);
for(int i=px-1;i<=px+1;i++){
for(int j=py-1;j<=py+1;j++){
if(0<=i && i <=7 &&0 <= j && j<=7)
if( nextstate[a].data[i][j] == 0){
opval[a]+=1;
// System.out.printf(" 開放マス(x,y) = (%d,%d) %n",i,j);
}
}
}
}
// System.out.printf("openval=%d%n",opval[a]);
}
//開放度の最小となる位置のindexを求める
int min = opval[0];
ArrayList<int[]> minList = new ArrayList<int[]>();
//開放度の最小値算出
for(int i=0;i<array.size();i++){
if(min > opval[i]){
min = opval[i];
}
}
//開放度が最小となる位置保存
for(int i=0;i<array.size();i++){
if(min == opval[i]){
int pos[] = array.get(i);
minList.add(pos);
}
}
///////////////重み最大の位置保存//////////////////
int postmp[] = minList.get(0);
int x = postmp[0]; int y = postmp[1];
int max = WT1[x][y];
//最大重み探索
for(int i=0;i<minList.size();i++){
int pos[] = minList.get(i);
x = pos[0]; y = pos[1];
if(max <= WT1[x][y] ){
max = WT1[x][y]; //重みの最大値更新
index = i; //重み最大のインデックス更新
}
}
}else if(cpulevel == 2){
///////////////1手後の盤面作成///////////////////////
//打ち込み可能場所の先頭の重み保存(max探索の為)
int ptmp[] = array.get(0);
int cx = ptmp[0]; int cy = ptmp[1];
int maxval = WT1[cx][cy];
//最大重み探索
for(int i=0;i<array.size();i++){
int pos[] = array.get(i);
cx = pos[0]; cy = pos[1];
if(maxval <= WT1[cx][cy] ){
maxval = WT1[cx][cy]; //重みの最大値更新
}
}
//重み最大の位置保存
ArrayList<int[]> maxList = new ArrayList<int[]>();
for(int i=0;i<array.size();i++){
int pos[] = array.get(i);
int x = pos[0]; int y = pos[1];
if(maxval == WT1[x][y] ){
int tpos[] = {x,y};
maxList.add(tpos);
}
}
//次の盤面作成
GameState[] nextstate = new GameState[maxList.size()];
for(int i=0;i<maxList.size();i++){
nextstate[i] = new GameState();
}
for(int ban=0;ban<maxList.size();ban++){
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
nextstate[ban].data[i][j] = state.data[i][j];
}
}
}
//次の盤面状態
// System.out.printf("次の盤面の可能性%n");
for(int ban=0;ban<maxList.size();ban++){
int pos[] = maxList.get(ban);
int x = pos[0]; int y = pos[1];
nextstate[ban].player *= -1; //プレーヤー反転
nextstate[ban].put(x,y); //候補手を置く
// System.out.printf("-------%d-------%n",ban);
// System.out.printf("b %d %d %n",x,y);
for(int j=0;j<8;j++){
for(int i=0;i<8;i++){
// System.out.printf("%d,",nextstate[ban].data[i][j]);
}
// System.out.printf("%n");
}
}
///////////////次の手で反転するコマ/////////////////////
Rev_Status[][] ReList = new Rev_Status[maxList.size()][18];
for(int j=0;j<18;j++){
for(int i=0;i<maxList.size();i++){
ReList[i][j] = new Rev_Status();
}
}
//反転したコマの位置保存
for(int ban=0;ban<maxList.size();ban++){
int revcnt=0;
for(int y=0; y<8; y++){
for(int x=0; x<8; x++){
//反転した場所を保存
int put[] = maxList.get(ban); //置いた場所
int tx=put[0]; int ty=put[1];
//反転してかつ置いた場所以外の場所保存
// System.out.printf("c %d %d %n",tx,ty);
if(nextstate[ban].data[x][y] != state.data[x][y] && (x!=put[0] || y!=put[1]) ){
int pos[] = {x,y};
ReList[ban][revcnt].pos = pos;
revcnt++;
}
}
}
}
//////////////////////開放度算出部/////////////////////
// System.out.printf("--------開放値算出部-----------%n");
int[] opval = new int[maxList.size()];
for(int a=0;a<maxList.size();a++){
int pos[] = maxList.get(a);
// System.out.printf("置いたマス--------(%d,%d)-------------------%n",pos[0],pos[1]);
for(int b=0;ReList[a][b].pos[0]!=-1;b++){
int px = ReList[a][b].pos[0]; int py = ReList[a][b].pos[1];
// System.out.printf(" 反転したマス(x,y) = (%d,%d) %n",px,py);
for(int i=px-1;i<=px+1;i++){
for(int j=py-1;j<=py+1;j++){
if(0<=i && i <=7 &&0 <= j && j<=7)
if( nextstate[a].data[i][j] == 0){
opval[a]+=1;
// System.out.printf(" 開放マス(x,y) = (%d,%d) %n",i,j);
}
}
}
}
}
//開放度の最小となる位置のindexを求める
int min = opval[0];
ArrayList<int[]> minList = new ArrayList<int[]>();
//開放度の最小値算出
for(int i=0;i<maxList.size();i++){
if(min > opval[i]){
min = opval[i];
index = i;
}
}
return maxList.get(index); //評価値が高く、開放度の低い場所を返す
}
return array.get(index);
}
// 配列90度回転
int[][] Rotation(int[][] block){
int[][] temp = new int[8][8];
for(int i=0; i<8; i++){
for(int j=0; j<8; j++){
temp[i][j] = block[j][7-i]; // 回転
}
}
return temp;
}
// 白黒反転
int[][] AllRevers(int[][] block){
int[][] temp = new int[8][8];
for(int i=0; i<8; i++){
for(int j=0; j<8; j++){
temp[i][j] = block[i][j]*-1; // 反転
}
}
return temp;
}
// 定石チェック_メイン
int[] Check_Opening(GameState state,int[][] block){
int[] put = new int[2]; // 定石 指す位置
int[] put_t = new int[2]; //転置定石 指す位置
int[] put_r = new int[2]; //白黒反転定石 指す位置
int[][] op = new int[8][8]; // 定石
int[][] op_trans = new int[8][8]; // 定石の転置
int[][] op_revers = new int[8][8]; // 定石の白黒反転
// 定石チェック開始
// System.out.println("-- Check Start Opening --");
//View(block);
/*
** forループより定石構造体をチェック
** 定石構造体:open.Pt[] (Openingクラスで実装)
**
** ☆チェック手順
** 1.定石を回転してチェック
** 2.転置定石を回転してチェック
** 3.白黒反転定石を回転してチェック
*/
for(int i=0; i<open.MAX_OP; i++){
// 定石初期化
//System.out.println("-- Initialise Opening : "+ i);
op = open.Pt[i].opening; // 定石
op_trans = open.TransposedMatrix(op); // 転置
op_revers = AllRevers(op); // 白黒反転
// 定石 回転チェック
put = Check_OpRotation(i,block,op);
if( put != null ){
// 定石ヒットした場合
//System.out.println("-- Hit Opening original --"+ i);
// ヒットした定石が置けるかチェック
if(state.canReverse(put[0], put[1]) == true)
return put;
else{
// System.out.println("-- Can't put Opening --");
return null;
}
// 定石ヒットしない -> 転置定石 回転チェック
}else if(Check_OpRotation(i,block,op_trans)!=null){
// System.out.println("-- Hit Opening trans --"+ i);
int[] temp = Check_OpRotation(i,block,op_trans);
// System.out.println("temp : " + Arrays.toString(temp));
// 最善手の転置
put_t[0] = temp[1];
put_t[1] = temp[0];
// ヒットした定石が置けるかチェック
if(state.canReverse(put_t[0], put_t[1]) == true)
return put_t;
else{
System.out.println("-- Can't put Opening --");
return null;
}
// 定石,転置定石ヒットしない -> 白黒反転定石 回転チェック
}else if(Check_OpRotation(i,block,op_revers)!=null){
// System.out.println("-- Hit Opening revers --"+ i);
put_r = Check_OpRotation(i,block,op_revers);
// ヒットした定石が置けるかチェック
if(state.canReverse(put_r[0], put_r[1]) == true)
return put_r;
else{
// System.out.println("-- Can't out Opening --");
return null;
}
}else {
// i番目の定石は ヒットなし
// System.out.println("-- Not Opening : "+ i);
}
}
// System.out.println("-- Not All Opening ");
return null;
}
// 定石回転チェック
int[] Check_OpRotation(int num, int[][] block, int[][] check){
int put[]={0,0}; // 定石により求めた指す位置
int[][] op = new int[8][8]; // 定石
int[][] op_revers = new int[8][8]; // 定石の白黒反転
int[][] op_90 = new int[8][8]; // 定石の90度回転
int[][] op_180 = new int[8][8]; // 定石の180度回転
int[][] op_270 = new int[8][8]; // 定石の270度回転
//System.out.println("|| Check OpRotation ||");
// 定石初期化
//System.out.println("|| Initialise OpRotation ||");
op = check; // 定石
op_90 = Rotation(op); // 90回転
op_180 = Rotation(op_90); // 180回転
op_270 = Rotation(op_180); // 270回転
// 比較 盤面:定石
if(Arrays.deepEquals(block,op)){
// System.out.println("|| Hit Opening ||");
put = open.Pt[num].pos;
// System.out.println("Best Pos : " + open.Pt[num].name
// + Arrays.toString(put));
return put;
// 比較 盤面:定石90
} else if(Arrays.deepEquals(block,op_90)){
// System.out.println("|| Hit Opening90 ||");
// put 90回転
put[0] = 7-open.Pt[num].pos[1];
put[1] = open.Pt[num].pos[0];
// System.out.println("Best Pos : " +open.Pt[num].name
// + Arrays.toString(put));
return put;
// 比較 盤面:定石180
} else if(Arrays.deepEquals(block,op_180)){
// System.out.println("|| Hit Opening180 ||");
// put 180回転
put[0] = 7-open.Pt[num].pos[0];
put[1] = 7-open.Pt[num].pos[1];
// System.out.println("Best Pos : " +open.Pt[num].name
// + Arrays.toString(put));
return put;
// 比較 盤面:定石270
} else if(Arrays.deepEquals(block,op_270)){
// System.out.println("|| Hit Opening270 ||");
// put 270回転
put[0] = open.Pt[num].pos[1];
put[1] = 7-open.Pt[num].pos[0];
// System.out.println("Best Pos : " +open.Pt[num].name
// + Arrays.toString(put));
return put;
} else {
//System.out.println("|| Not OpRotation ||");
return null;
}
}
}