-
Notifications
You must be signed in to change notification settings - Fork 0
/
acn.cpp
252 lines (213 loc) · 5.24 KB
/
acn.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
#include "acn.h"
FILE *flog = fopen("BlitzKibitz-ACN.log", "w");
Move DecodeACN(const char *sMove, Board &board)
{
setvbuf(flog, 0, _IONBF, 0);
fprintf(flog, "\nDecode %s\n", sMove);
Move m;
m.flags = m.piece = m.promote_to = m.check = 0;
m.source = m.destination = 64;
m.player = board.player;
int i;
int xi=64, yi=64, xf=64, yf=64;
i = strlen(sMove)-1;
if (sMove[i] == '+') { // check
m.check |= CHECK;
--i;
}
if (sMove[i] == '#') { // check mate
m.check |= MATE;
--i;
}
if (strcmp(sMove, "O-O-O") == 0) { // queen side castle
m.flags |= QUEEN_SIDE_CASTLE;
return m;
}
if (strcmp(sMove, "O-O") == 0) { // king side castle
m.flags |= KING_SIDE_CASTLE;
return m;
}
// if this moves leads to a promotion
char promovare = 0;
if (IsPiece(sMove[i])) {
promovare = sMove[i];
m.flags |= PROMOTION;
--i;
if (sMove[i] == '=') --i;
}
// destination place
// line and column
if (sMove[i]<'1' || sMove[i]>'8') {
fprintf(flog, "ACN format error!(1)\n");
m.flags = ERROR;
return m;
}
else {
xf = sMove[i--]-'1';
}
if (sMove[i]<'a' || sMove[i]>'h') {
fprintf(flog, "ACN format error!(2)\n");
m.flags = ERROR;
return m;
}
else {
yf = 'h'-sMove[i--];
}
// Check capture
if (sMove[i] == 'x') {
m.flags |= CAPTURE;
--i;
}
// next:
// 1. nothing => pawn
// 2. piece type
// 3. line or column => indicates initial position
// NOTICE:
// needs more testing in all possible combinations!
// try to vary piece type, line, column and capture
if (i<0) {
// 1. nothing => pawn
m.piece = 'P'; // pion
}
else {
if (IsPiece(sMove[i])) {
// 2. piece type
m.piece = sMove[i];
}
else {
// 3. line and/or column
if (sMove[i]>='a' && sMove[i]<='h') {
yi = 'h'-sMove[i--];
}
else {
if (sMove[i]>='0' && sMove[i]<='8') {
xi = sMove[i--]-'1';
// avem si linie?
if (sMove[i]>='a' && sMove[i]<='h') {
yi = 'h'-sMove[i--];
}
}
else {
fprintf(flog, "ACN format error!(3)\n");
m.flags = ERROR;
return m;
}
}
// next: piece type or nothing
if (i<0)
m.piece = 'P';
else if (IsPiece(sMove[i]))
m.piece = sMove[i];
if (m.piece == 0) {
fprintf(flog, "ACN format error!(4)\n");
m.flags = ERROR;
return m;
}
}
}
m.source = COORDS_TO_INDEX(xi, yi);
m.destination = COORDS_TO_INDEX(xf, yf);
if (promovare)
m.promote_to = promovare;
// check "en passant" flag (pawn piece + capture)
if (m.piece == 'P' && m.flags == CAPTURE)
if (board.GetPieceType(m.destination) == -1)
m.flags = ENPASS;
if (xi==64 || yi==64) {
if (!m.FindCoordinates(board)) {
fprintf(flog, "Error while searching for piece!\n");
fprintf(flog, "Searched for a piece that gets to %d %d\n", xf, yf);
fprintf(flog, "What do we know about the starting position: %d %d\n", xi, yi);
fprintf(flog, "Capture: %d\n", (m.flags & CAPTURE)!=0);
fprintf(flog, "Piece: %c\n", m.piece);
m.flags = ERROR;
return m;
}
}
return m;
}
string EncodeACN(Move &mv, Board &board)
{
setvbuf(flog, 0, _IONBF, 0);
pair<int, int> src = INDEX_TO_COORDS(mv.source);
pair<int, int> dst = INDEX_TO_COORDS(mv.destination);
fprintf(flog, "\nEncode %c %d %d -> %d %d\n",
mv.piece,
src.first, src.second,
dst.first, dst.second);
string acn = "";
acn.reserve(16); // preallocate 16 bytes to prevent reallocation
if (mv.flags == KING_SIDE_CASTLE) {
acn = "O-O";
if (mv.check == CHECK)
acn += "+";
if (mv.check == MATE)
acn += "#";
fprintf(flog, "%s\n", acn.c_str());
return acn;
}
if (mv.flags == QUEEN_SIDE_CASTLE) {
acn = "O-O-O";
if (mv.check == CHECK)
acn += "+";
if (mv.check == MATE)
acn += "#";
fprintf(flog, "%s\n", acn.c_str());
return acn;
}
int lin = 64, col = 64;
Move mt;
mt.promote_to = mt.check = 0;
if (toupper(mv.piece) != 'P')
acn += mv.piece;
// set source
col = mv.source % 8;
lin = mv.source / 8;
mt.piece = toupper(mv.piece);
mt.source = COORDS_TO_INDEX(64, 64);
mt.destination = mv.destination;
mt.flags = mv.flags;
mt.player = mv.player;
mt.check = mv.check;
mt.promote_to = mv.promote_to;
if ((toupper(mv.piece) == 'P' && (mv.flags & ENPASS || mv.flags & CAPTURE)) || !mt.FindCoordinates(board)) {
// column desabiguazation
mt.source = COORDS_TO_INDEX(64, col);
if (mt.FindCoordinates(board)) {
acn += ('h' - col);
}
else {
// line desabiguazation
mt.source = COORDS_TO_INDEX(lin, 64);
if (mt.FindCoordinates(board)) {
acn += (char)('1'+lin);
}
else {
// line & column desabiguazation
mt.source = COORDS_TO_INDEX(lin, col);
acn += (char)('h' - col);
acn += (char)('1' + lin);
}
}
}
// capture
if (mv.flags & CAPTURE || mv.flags & ENPASS)
acn += 'x';
// set destination
col = mv.destination % 8;
lin = mv.destination / 8;
acn += ('h' - col);
acn += ('1' + lin);
// promote
if (mv.promote_to) {
acn += '=';
acn += mv.promote_to;
}
// check/mate
if (mv.check == CHECK)
acn += '+';
if (mv.check == MATE)
acn += '#';
fprintf(flog, "%s\n", acn.c_str());
return acn;
}