-
Notifications
You must be signed in to change notification settings - Fork 0
/
xboard.cpp
188 lines (171 loc) · 4.79 KB
/
xboard.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
#include "xboard.h"
class IDDFS_logger : public IDDFS_callback_class {
FILE *logf;
public:
IDDFS_logger(FILE *logf) : logf(logf) {}
void operator()(int depth, int score, int nodes) const {
fprintf(logf, "%d %d %d\n", depth, score, nodes);
}
};
class IDDFS_xboard_printer : public IDDFS_callback_class {
public:
void operator()(int depth, int score, int nodes) const {
printf("%d %d %d %d ?\n", depth, score, 0/*time*/, nodes);
}
};
void XPlay(int normal_max_depth, Board &board)
{
int opponent = 0;
char cbuffer[128];
string buffer;
Openings Op;
Op.InitOpenings("openings.bk");
FILE *logf = fopen("BlitzKibitz-xboard.log", "wt");
FILE *loga = fopen("BlitzKibitz-oponent.log", "wt");
setvbuf(logf, 0, _IONBF, 0);
setvbuf(loga, 0, _IONBF, 0);
bool send_iddfs = false;
const IDDFS_xboard_printer IDDFS_to_xboard;
// disable buffer use
setvbuf(stdin, 0, _IONBF, 0);
setvbuf(stdout, 0, _IONBF, 0);
int moveNr = 1;
fprintf(logf, "Begin - max depth %d\n", normal_max_depth);
printf("feature san=1\n");
printf("feature sigint=0\n");
printf("feature sigterm=0\n");
printf("feature name=0\n");
printf("feature time=0\n");
printf("feature draw=0\n");
printf("feature reuse=0\n");
printf("feature analyze=0\n");
printf("feature myname=\"BlitzKibitz\"\n");
printf("feature done=1\n");
printf("Begin\n");
while (1) {
Move m;
if (board.player == opponent || opponent == -1) {
fgets(cbuffer, 128, stdin); cbuffer[strlen(cbuffer)-1] = 0;
buffer = cbuffer;
fprintf(logf, "cbuffer: `%s`\n", cbuffer);
m.flags = 0;
m = DecodeACN(cbuffer, board);
if (m.check == MATE) {
printf("resign\n");
}
else if (m.flags & ERROR) {
if (buffer == "quit") {
fgets(cbuffer, 128, stdin); // make sure there is nothing more to get
break;
}
else if (buffer == "new") {
// new game
// TODO: test it some more!
board.InitChessboard();
opponent = 0;
}
else if (buffer == "xboard") {
// ok..
}
else if (buffer == "force") {
opponent = -1;
}
else if (buffer == "go") {
//opponent = !board.player;
}
else if (buffer == "white") {
opponent = 1;
}
else if (buffer == "black") {
opponent = 0;
}
else if (buffer == "go") {
opponent = !board.player;
}
// make sure we don't consider valid xboard commands as errors
else if (buffer.find("accepted")!=string::npos
|| buffer.find("random")!=string::npos) {
}
else if (buffer.find("level")!=string::npos
|| buffer.find("hard")!=string::npos) {
}
else if (buffer.find("time")!=string::npos
|| buffer.find("otim")!=string::npos) {
}
else if (buffer == "post") {
send_iddfs = true;
}
else if (buffer == "nopost") {
send_iddfs = false;
}
else {
fprintf(logf, "error :|\n");
board.PrintBoard(logf);
fflush(logf);
}
}
else {
fprintf(loga, "%s\n", cbuffer);
fflush(loga);
}
}
else {
fprintf(logf, "-->My turn!\n");
moveNr += 2;
m = Op.GetMoveFromDB(board);
if (m.flags == ERROR) {
int score = 0;
int max_depth = normal_max_depth;
if (moveNr < 8) {
max_depth = max_depth + 1;
}
if (board.GetPieceCount() < 11) {
max_depth = max_depth + 1;
}
fprintf(logf, "Playing at %d plies\n", max_depth);
if (send_iddfs) {
score = IDDFS(board, max_depth, IDDFS_to_xboard);
} else {
score = IDDFS(board, max_depth, IDDFS_empty_callback());
}
m = BestMove;
fprintf(logf, "Move #%d\n", moveNr);
fprintf(logf, "BestMove has score %d\n", score);
}
else {
fprintf(logf, "Move #%d found in DB!\n", moveNr);
}
if (m.flags & DRAW) {
// I can't make any move
printf("1/2-1/2 {Stalemate}");
fprintf(logf, "Stalemate\n");
board.player = !board.player;
}
else {
string enc = EncodeACN(m, board);
fprintf(logf, "%s\n", enc.c_str());
printf("move %s\n", enc.c_str());
if (m.check == MATE) {
printf("checkmate");
board.MakeMove(m);
board.player = !board.player;
board.PrintBoard(logf);
}
}
}
if (!((m.flags & ERROR) || m.check == MATE || (m.flags & DRAW))) {
board.MakeMove(m);
board.player = !board.player;
board.PrintBoard(logf);
fprintf(logf, "Current board has score %d\n", CalculateScore(board));
fprintf(logf, "FEN: %s\n", toFEN(board));
fprintf(logf, "Total hash hits: %d\n", hits);
fprintf(logf, "Total hash hits with depth diffference >=2 : %d\n", hd2);
fprintf(logf, "Total hash hits with depth diffference >=3 : %d\n", hd3);
}
fprintf(logf, "\n");
}
fprintf(logf, "I'm done!\n");
fclose(logf);
fclose(loga);
}