forked from Tsunehiko511/Cpp_Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
353 lines (328 loc) · 8.23 KB
/
main.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <iostream>
#include <iomanip>
#include <map>
#include <unistd.h> // usleep関数
#include <time.h> // for clock()
using namespace std;
#define BOARD_SIZE 9
#define W_SIZE 11
// 石を打ったときの処理
#define SUCCESS 0 // 打てる
#define KILL 1 // 自殺手
#define KO 2 // 劫
#define ME 3 // 眼
#define MISS 4 // すでに石がある
#define PASS 5 // パス
// 盤上の種類
#define SPACE 0
#define BLACK 1
#define WHITE 2
#define WALL 3
// 戦略
#define RANDOM 1
#define MONTE_CARLO 2
// 真偽値
#define FALSE 0
#define TRUE 1
// 座標
typedef struct{
int y;
int x;
} point;
const char *visual[4] = {"・","🔴 ","⚪️ ", "◼️"};
void getNeighbors(point center, point *neighbors){
// printf("getNeighbors\n");
neighbors[0] = (point){center.y-1,center.x};
neighbors[1] = (point){center.y+1,center.x};
neighbors[2] = (point){center.y,center.x-1};
neighbors[3] = (point){center.y,center.x+1};
}
class Board{
private:
int data[W_SIZE][W_SIZE];
public:
point ko;
Board(){
for(int y = 0; y<W_SIZE; y++){
for (int x = 0; x<W_SIZE; x++)
{
this->data[y][x] = SPACE;
}
}
for(int i=0; i<W_SIZE; i++){
this->data[0][i] = this->data[W_SIZE-1][i] = this->data[i][0] = this->data[i][W_SIZE-1] = WALL;
}
this->ko = (point){1000,1000};
}
// 石の設置と取得
void set(point position, int stone){
// printf("set\n");
this->data[position.y][position.x] = stone;
}
int get(point position){
return this->data[position.y][position.x];
}
// 取り除く
void remove(point position){
// printf("remove\n");
set(position, SPACE);
}
// 碁盤描画
void draw(void) const{
printf(" ");
for (int x = 1; x<W_SIZE-1; x++) printf("%d ", x);
printf("\n");
for (int y = 1; y<W_SIZE-1; y++){
printf("%d ", y);
for (int x = 1; x<W_SIZE-1; x++){
printf("%s",visual[this->data[y][x]]);
}
printf("\n");
}
}
vector<point> getSpaces(){
// printf("getSpaces\n");
vector<point> space_array;
for(int y = 1; y<10;y++){
for(int x = 1; x<10;x++){
point position = (point){y,x};
if(get(position) == SPACE){
space_array.push_back(position);
}
}
}
return space_array;
}
};
void count_around(int checked[9][9], Board *board, point position, int color, int* joined, int* liberty);
void count_joined_liberty(Board *board, point position, int color, int* joined, int* liberty);
class Player{
private:
public:
int color;
int un_color;
int tact;
point posi;
Player(int color){
this->color = color;
un_color = 3 - this->color;
//this->tact = tactics(strategy);
}
int play(Board *board){
return SUCCESS;
}
// 相手の石を取る
void capture(Board *board, point position){
// printf("capture\n");
board->remove(position);
point neighbors[4];
getNeighbors(position,neighbors);
for(int i=0; i<4; i++){
point neighbor = neighbors[i];
if(board->get(neighbor) == this->un_color){
capture(board, neighbor);
}
}
}
int move(Board *board, point position){
// printf("move\n");
if (position.y == 0 && position.x == 0){
return PASS;
}
// すでに石がある
if(board->get(position) != SPACE){
// printf("すでに石がある\n");
return MISS;
}
// positionに対して四方向の [連石, 呼吸点, 色]を調べる
int joineds[4] = {0,0,0,0};
int libertys[4] = {0,0,0,0};
int colors[4] = {0,0,0,0};
int space = 0;
int wall = 0;
int mikata_safe = 0;
int take_sum = 0;
point ko = {0,0};
point neighbors[4] = {0,0,0,0};
getNeighbors(position,neighbors);
// 打つ前の4方向をしらべる
for(int i=0; i<4; i++){
colors[i] = board->get(neighbors[i]);
if (colors[i] == SPACE){
space += 1;
continue;
}
if (colors[i] == WALL){
wall += 1;
continue;
}
// 連石と呼吸点の数を数える
count_joined_liberty(board, neighbors[i], colors[i], &joineds[i], &libertys[i]);
if (colors[i] == this->un_color && libertys[i] == 1){
take_sum += joineds[i];
ko = neighbors[i];
}
if (colors[i] == this->color && libertys[i] >= 2){
mikata_safe += 1;
}
}
// ルール違反
if (take_sum == 0 && space == 0 && mikata_safe == 0){
return KILL;
}
if (position.y == board->ko.y && position.x == board->ko.x){
return KO;
}
if(wall + mikata_safe == 4){
return ME;
}
// 石を取る
point neighbors2[4] = {0,0,0,0};
getNeighbors(position,neighbors2);
for (int i = 0; i < 4; ++i){
if (colors[i] == this->un_color && libertys[i] == 1){
capture(board, neighbors2[i]);
}
}
// 石を打つ
// printf("%s (%d,%d)\n", visual[this->color], position.y, position.x);
board->set(position, this->color);
int joined = 0;
int liberty = 0;
count_joined_liberty(board, position, this->color, &joined, &liberty);
// printf("エラーチェック1\n");
if (take_sum == 1 && joined == 1 && liberty == 1){
board->ko = ko;
// printf("エラーチェック2\n");
}
else{
// printf("エラーチェック3\n");
board->ko = (point){10000,10000};
}
// printf("エラーチェック4\n");
return SUCCESS;
}
int random_choice(Board *board){
// printf("random_choice\n");
vector<point> spaces = board->getSpaces();
int l = spaces.size();
while(l>0){
int n = rand()%l;
point position = spaces[n];
int result = move(board, position);
//printf("random_choice_select\n");
// board->draw();
if(result == SUCCESS){
posi = position;
return SUCCESS;
}
// printf("l=%d\n", l);
spaces[n] = spaces[l-1];
l -=1;
}
return PASS;
}
};
void count_around(int checked[11][11], Board *board, point position, int color, int* joined, int* liberty){
int y = position.y;
int x = position.x;
// printf("count (%d,%d)\n", y, x);
checked[y][x] = TRUE;
*joined +=1;
// 周辺を調べる
point neighbors[4] = {(point){y-1,x}, (point){y+1,x}, (point){y,x-1}, (point){y,x+1}};
for(int i = 0; i<4; i++){
point neighbor = neighbors[i];
if(checked[neighbor.y][neighbor.x]==TRUE){
continue;
}
int data = board->get(neighbor);
if(data==SPACE){
checked[neighbor.y][neighbor.x] = TRUE;
*liberty += 1;
}
else if(data == color){
// printf("count繰り返し\n");
count_around(checked, board, neighbor, data, joined, liberty);
}
}
}
void count_joined_liberty(Board *board, point position, int color, int* joined, int* liberty){
int checked[11][11] = {{FALSE}};
count_around(checked, board, position, color, joined, liberty);
// printf("count_joined_liberty END\n");
}
// 二次元配列を受け取り変更して返す
int(* double_array(int array[][9]))[9]{
for(int i = 0; i<10; i++){
for(int j = 0; j<10;j++){
array[i][j] = 1;
}
}
return array;
}
double run(std::vector<Board>& history){
clock_t start = clock();
// 碁盤の作成
Board board;
// プレイヤー
Player black = Player(BLACK);
Player white = Player(WHITE);
Player player = black;
// 先手
int length = 0;
int passed = 0;
// 対局開始
while(passed < 2){
int result = player.random_choice(&board);
if(result==SUCCESS){
history.push_back(board);
// board.draw();
// usleep(100000); // 1000000=1sec
}
// パス判定
if (result==PASS){
passed += 1;
}
else{
passed = 0;
}
if(player.color == BLACK){
player = white;
}
else{
player = black;
}
}
clock_t end = clock();
//board.draw();
history.push_back(board);
double elap = (double)(end-start)/CLOCKS_PER_SEC;
//printf("Time : %f(sec)\n", elap);
//printf("%f(playout/sec)\n", (double)CLOCKS_PER_SEC/(end-start));
return elap;
}
int main()
{
double sum = 0.0;
size_t loop_count = 1000;
srand((unsigned) time(NULL));
for(size_t i = 0; i < loop_count; ++i) {
std::vector<Board> history;
history.reserve(100);
sum += run(history);
/*
for(const auto& h : history) {
h.draw();
}
*/
}
sum /= static_cast<double>(loop_count);
std::cout << "average time " << sum << " sec. " << std::endl;
std::cout << "average playout " << std::setprecision(8) << (double)CLOCKS_PER_SEC / (sum * CLOCKS_PER_SEC) << std::endl;
return 0;
}