forked from chaozc/AI-Project-Computer-Go
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
613 lines (522 loc) · 15.1 KB
/
Copy pathmain.cpp
File metadata and controls
613 lines (522 loc) · 15.1 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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* This is Brown, a simple go program. *
* *
* Copyright 2003 and 2004 by Gunnar Farnebäck. *
* *
* Permission is hereby granted, free of charge, to any person *
* obtaining a copy of this file gtp.c, to deal in the Software *
* without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, and/or *
* sell copies of the Software, and to permit persons to whom *
* the Software is furnished to do so, provided that the above *
* copyright notice(s) and this permission notice appear in all *
* copies of the Software and that both the above copyright *
* notice(s) and this permission notice appear in supporting *
* documentation. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY *
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE *
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR *
* PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO *
* EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS *
* NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR *
* CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING *
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF *
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT *
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS *
* SOFTWARE. *
* *
* Except as contained in this notice, the name of a copyright *
* holder shall not be used in advertising or otherwise to *
* promote the sale, use or other dealings in this Software *
* without prior written authorization of the copyright holder. *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* for rand() and srand() */
#include "GoBoard.h"
#include "FastBoard.h"
#include "brown.h"
#include "UctSearch.h"
#include "gtp.h"
#include "MC_RAVE_Search.h"
/* Forward declarations. */
static int gtp_protocol_version(char *s);
static int gtp_name(char *s);
static int gtp_version(char *s);
static int gtp_known_command(char *s);
static int gtp_list_commands(char *s);
static int gtp_quit(char *s);
static int gtp_boardsize(char *s);
static int gtp_clear_board(char *s);
static int gtp_komi(char *s);
static int gtp_fixed_handicap(char *s);
static int gtp_place_free_handicap(char *s);
static int gtp_set_free_handicap(char *s);
static int gtp_play(char *s);
static int gtp_genmove(char *s);
static int gtp_final_score(char *s);
static int gtp_final_status_list(char *s);
static int gtp_showboard(char *s);
/* List of known commands. */
static struct gtp_command commands[] = {
{ "protocol_version", gtp_protocol_version },
{ "name", gtp_name },
{ "version", gtp_version },
{ "known_command", gtp_known_command },
{ "list_commands", gtp_list_commands },
{ "quit", gtp_quit },
{ "boardsize", gtp_boardsize },
{ "clear_board", gtp_clear_board },
{ "komi", gtp_komi },
{ "fixed_handicap", gtp_fixed_handicap },
{ "place_free_handicap", gtp_place_free_handicap },
{ "set_free_handicap", gtp_set_free_handicap },
{ "play", gtp_play },
{ "genmove", gtp_genmove },
{ "final_score", gtp_final_score },
{ "final_status_list", gtp_final_status_list },
{ "showboard", gtp_showboard },
{ NULL, NULL }
};
#include <fstream>
#include <iostream>
#include <ctime>
using namespace std;
#include <iostream>
int
main(int argc, char **argv)
{
/*int cnt = 0;
while (clock() < 10 * 1000) {
int b[169];
memset(b, 0, sizeof(b));
FastBoard board(b, -1, -1);
board.simulate();
cout << "black win ? " << board.blackWins() << endl;
++cnt;
system("pause");
//if (1 || cnt % 1000 == 0 )cout << cnt << endl;
}
cout << cnt << endl;
system("pause");
*/
/*int a[169];
memset(a, 0, sizeof(a));
FastBoard board(a, -1, -1);
int color = FastBoard::BLACK;
for (int k = 0; k < 18; ++k) {
int pos = board.generateMove(color);
board.playMove(pos, color);
color = board.otherColor(color);
}
MC_RAVE_Search search;
cout << search.MC_RAVE(board, color) << endl;
system("pause");*/
//GoBoard board = GoBoard();
//int i, j;
//int black = GoBoard::BLACK;
//int white = GoBoard::WHITE;
//board.play_move(5, 5, black);
//board.play_move(6, 5, white);
////board.play_move(5, 4, white);
//board.play_move(6, 4, black);
//board.play_move(7, 4, white);
//board.play_move(6, 6, black);
//board.play_move(5, 4, white);
////board.play_move(6, 5, white);
//board.generate_move(&i, &j, black);
//board.show();
//cout << i << ' ' << j << endl;
//system("pause");
//return 0;
ofstream out("compare.txt", std::fstream::app);
init_brown();
/*
for (int number = 0; number < 100000; ++number) {
int b[169];
int num = 0;
int diff[169];
memset(b, 0, sizeof(b));
GoBoard oldBoard = GoBoard();
FastBoard newBoard = FastBoard(b, -1, FastBoard::PASS_MOVE);
int color = GoBoard::BLACK;
for (int k = 0; k < 250; ++k) {
//cout << k << endl;
if (number == 0 && k == 31) {
int kk = 0;
}
int i, j;
num = 0;
bool flag1 = true;
bool flag2 = true;
bool flag3 = true;
for (i = 0; i < 169; ++i) {
if (oldBoard.board[i] != newBoard.board[i]) {
flag1 = false;
}
if (oldBoard.board[i] != EMPTY &&
oldBoard.liberties[i] != newBoard.strings[newBoard.stringNumber[i]].liberties) {
diff[num++] = i;
flag2 = false;
}
}
//out << "Configuration " << k << ": " << oldBoard.li << ' ' << oldBoard.lj << ' ';
if (flag1) {
//out << "true" << endl;
}
else
out << "false" << endl;
// out << "liberty " << k << ": ";
if (flag2) {
//out << "true" << endl;
}
else {
out << "false" << endl << "Different Point:" << endl;
for (i = 0; i < num; ++i)
out << "(" << I(diff[i]) << ", " << J(diff[i]) << ")" << endl;
out << endl;
for (int m = 0; m < 13; ++m) {
for (int n = 0; n < 13; ++n){
if (oldBoard.board[POS(m, n)] == EMPTY) out << ".";
if (oldBoard.board[POS(m, n)] == BLACK) out << "X";
if (oldBoard.board[POS(m, n)] == WHITE) out << "O";
}
out << endl;
}
for (int m = 0; m < 13; ++m) {
for (int n = 0; n < 13; ++n){
if (newBoard.board[POS(m, n)] == EMPTY) out << ".";
if (newBoard.board[POS(m, n)] == BLACK) out << "X";
if (newBoard.board[POS(m, n)] == WHITE) out << "O";
}
out << endl;
}
out << "------------------------------" << endl;
out << "GoBoard liberty: " << endl;
for (int m = 0; m < 13; ++m) {
for (int n = 0; n < 13; ++n){
if (oldBoard.board[POS(m, n)] == EMPTY)
out << ".";
else
out << oldBoard.liberties[POS(m, n)];
}
out << endl;
}
out << "------------------------------" << endl;
out << "FastBoard liberty: " << endl;
for (int m = 0; m < 13; ++m) {
for (int n = 0; n < 13; ++n){
if (newBoard.board[POS(m, n)] == EMPTY)
out << ".";
else
out << newBoard.strings[newBoard.stringNumber[(m * 13 + n)]].liberties;
}
out << endl;
}
out << "------------------------------" << endl;
return 0;
}
//out << "------------------------------" << endl;
int pos = newBoard.generateMove(color);
if (pos == FastBoard::PASS_MOVE) {
break;
}
i = pos / 13, j = pos % 13;
oldBoard.play_move(i, j, color);
newBoard.playMove(pos, color);
color = GoBoard::other_color(color);
}
if (number % 100 == 0) cout << "Yes " << number << endl;
}
//cout << search.MC_RAVE(board, color) << endl;
//UctSearch search(board, color);
//search.selectBySearch(10);
/*
UCT_Simulator uct;
uct.initialize(board, color);
int i, j;
uct.getResult(&i, &j);
std::cout << "over" << std::endl;
*/
unsigned int random_seed = 1;
/* Optionally a random seed can be passed as an argument to the program. */
if (argc > 1)
sscanf(argv[1], "%u", &random_seed);
srand(random_seed);
/* Make sure that stdout is not block buffered. */
setbuf(stdout, NULL);
/* Inform the GTP utility functions about the initial board size. */
gtp_internal_set_boardsize(board_size);
/* Initialize the board. */
init_brown();
/* Process GTP commands. */
gtp_main_loop(commands, stdin, NULL);
return 0;
}
/* We are talking version 2 of the protocol. */
static int
gtp_protocol_version(char *s)
{
return gtp_success("2");
}
static int
gtp_name(char *s)
{
return gtp_success("Brown");
}
static int
gtp_version(char *s)
{
return gtp_success(VERSION_STRING);
}
static int
gtp_known_command(char *s)
{
int i;
char command_name[GTP_BUFSIZE];
/* If no command name supplied, return false (this command never
* fails according to specification).
*/
if (sscanf(s, "%s", command_name) < 1)
return gtp_success("false");
for (i = 0; commands[i].name; i++)
if (!strcmp(command_name, commands[i].name))
return gtp_success("true");
return gtp_success("false");
}
static int
gtp_list_commands(char *s)
{
int i;
gtp_start_response(GTP_SUCCESS);
for (i = 0; commands[i].name; i++)
gtp_printf("%s\n", commands[i].name);
gtp_printf("\n");
return GTP_OK;
}
static int
gtp_quit(char *s)
{
gtp_success("");
return GTP_QUIT;
}
static int
gtp_boardsize(char *s)
{
int boardsize;
if (sscanf(s, "%d", &boardsize) < 1)
return gtp_failure("boardsize not an integer");
if (boardsize < MIN_BOARD || boardsize > MAX_BOARD)
return gtp_failure("unacceptable size");
board_size = boardsize;
gtp_internal_set_boardsize(boardsize);
init_brown();
return gtp_success("");
}
static int
gtp_clear_board(char *s)
{
clear_board();
return gtp_success("");
}
static int
gtp_komi(char *s)
{
if (sscanf(s, "%f", &komi) < 1)
return gtp_failure("komi not a float");
return gtp_success("");
}
/* Common code for fixed_handicap and place_free_handicap. */
static int
place_handicap(char *s, int fixed)
{
int handicap;
int m, n;
int first_stone = 1;
if (!board_empty())
return gtp_failure("board not empty");
if (sscanf(s, "%d", &handicap) < 1)
return gtp_failure("handicap not an integer");
if (handicap < 2)
return gtp_failure("invalid handicap");
if (fixed && !valid_fixed_handicap(handicap))
return gtp_failure("invalid handicap");
if (fixed)
place_fixed_handicap(handicap);
else
place_free_handicap(handicap);
gtp_start_response(GTP_SUCCESS);
for (m = 0; m < board_size; m++)
for (n = 0; n < board_size; n++)
if (get_board(m, n) != EMPTY) {
if (first_stone)
first_stone = 0;
else
gtp_printf(" ");
gtp_mprintf("%m", m, n);
}
return gtp_finish_response();
}
static int
gtp_fixed_handicap(char *s)
{
return place_handicap(s, 1);
}
static int
gtp_place_free_handicap(char *s)
{
return place_handicap(s, 0);
}
static int
gtp_set_free_handicap(char *s)
{
int i, j;
int n;
int handicap = 0;
if (!board_empty())
return gtp_failure("board not empty");
while ((n = gtp_decode_coord(s, &i, &j)) > 0) {
s += n;
if (get_board(i, j) != EMPTY) {
clear_board();
return gtp_failure("repeated vertex");
}
play_move(i, j, BLACK);
handicap++;
}
if (sscanf(s, "%*s") != EOF) {
clear_board();
return gtp_failure("invalid coordinate");
}
if (handicap < 2 || handicap >= board_size * board_size) {
clear_board();
return gtp_failure("invalid handicap");
}
return gtp_success("");
}
static int
gtp_play(char *s)
{
int i, j;
int color = EMPTY;
if (!gtp_decode_move(s, &color, &i, &j))
return gtp_failure("invalid color or coordinate");
if (!legal_move(i, j, color))
return gtp_failure("illegal move");
play_move(i, j, color);
return gtp_success("");
}
static int
gtp_genmove(char *s)
{
int i, j;
int color = EMPTY;
if (!gtp_decode_color(s, &color))
return gtp_failure("invalid color");
generate_move(&i, &j, color);
play_move(i, j, color);
gtp_start_response(GTP_SUCCESS);
gtp_mprintf("%m", i, j);
return gtp_finish_response();
}
/* Compute final score. We use area scoring since that is the only
* option that makes sense for this move generation algorithm.
*/
static int
gtp_final_score(char *s)
{
float score = komi;
int i, j;
compute_final_status();
for (i = 0; i < board_size; i++)
for (j = 0; j < board_size; j++) {
int status = get_final_status(i, j);
if (status == BLACK_TERRITORY)
score--;
else if (status == WHITE_TERRITORY)
score++;
else if ((status == ALIVE) ^ (get_board(i, j) == WHITE))
score--;
else
score++;
}
if (score > 0.0)
return gtp_success("W+%3.1f", score);
if (score < 0.0)
return gtp_success("B+%3.1f", -score);
return gtp_success("0");
}
static int
gtp_final_status_list(char *s)
{
int n;
int i, j;
int status = UNKNOWN;
char status_string[GTP_BUFSIZE];
int first_string;
if (sscanf(s, "%s %n", status_string, &n) != 1)
return gtp_failure("missing status");
if (!strcmp(status_string, "alive"))
status = ALIVE;
else if (!strcmp(status_string, "dead"))
status = DEAD;
else if (!strcmp(status_string, "seki"))
status = SEKI;
else
return gtp_failure("invalid status");
compute_final_status();
gtp_start_response(GTP_SUCCESS);
first_string = 1;
for (i = 0; i < board_size; i++)
for (j = 0; j < board_size; j++)
if (get_final_status(i, j) == status) {
int k;
int stonei[MAX_BOARD * MAX_BOARD];
int stonej[MAX_BOARD * MAX_BOARD];
int num_stones = get_string(i, j, stonei, stonej);
/* Clear the status so we don't find the string again. */
for (k = 0; k < num_stones; k++)
set_final_status(stonei[k], stonej[k], UNKNOWN);
if (first_string)
first_string = 0;
else
gtp_printf("\n");
gtp_print_vertices(num_stones, stonei, stonej);
}
return gtp_finish_response();
}
/* Write a row of letters, skipping 'I'. */
static void
letters(void)
{
int i;
printf(" ");
for (i = 0; i < board_size; i++)
printf(" %c", 'A' + i + (i >= 8));
}
static int
gtp_showboard(char *s)
{
int i, j;
int symbols[3] = { '.', 'O', 'X' };
gtp_start_response(GTP_SUCCESS);
gtp_printf("\n");
letters();
for (i = 0; i < board_size; i++) {
printf("\n%2d", board_size - i);
for (j = 0; j < board_size; j++)
printf(" %c", symbols[get_board(i, j)]);
printf(" %d", board_size - i);
}
printf("\n");
letters();
return gtp_finish_response();
}
/*
* Local Variables:
* tab-width: 8
* c-basic-offset: 2
* End:
*/