This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
stringer.c
454 lines (377 loc) · 9.66 KB
/
stringer.c
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
/*
Weak, a chess perft calculator derived from Stockfish.
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
Copyright (C) 2008-2012 Marco Costalba, Joona Kiiski, Tord Romstad (Stockfish authors)
Copyright (C) 2011-2012 Lorenzo Stoakes
Weak is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Weak is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "weak.h"
char
CharPiece(Piece piece)
{
switch(piece) {
case Pawn:
return 'P';
case Rook:
return 'R';
case Knight:
return 'N';
case Bishop:
return 'B';
case Queen:
return 'Q';
case King:
return 'K';
default:
return '?';
}
}
char*
StringBitBoard(BitBoard bitBoard)
{
const int LINE_LENGTH = 36;
const int LINE_COUNT = 18;
// TODO: Reduce duplication from StringChessSet.
// Include space for newlines.
char ret[LINE_LENGTH * LINE_COUNT + 1 + 1000];
int file, offset, rank;
Position pos;
Side lineSide = Black;
char *topLine = " A B C D E F G H \n";
char *dottedLine = " ---------------------------------\n";
char *blackLine = " | |...| |...| |...| |...|\n";
char *whiteLine = " |...| |...| |...| |...| |\n";
// Add top lines.
strncpy(ret, topLine, LINE_LENGTH);
strncpy(ret+LINE_LENGTH, dottedLine, LINE_LENGTH);
for(rank = Rank8; rank >= Rank1; rank--) {
offset = 2*LINE_LENGTH + 2*LINE_LENGTH*(Rank8-rank);
// Add line.
strncpy(ret + offset, lineSide == White ? whiteLine : blackLine, LINE_LENGTH);
// Add number.
ret[offset] = '1'+rank;
// Add pieces.
// For the sake of debugging, avoid PieceAt() in case it is buggy.
offset += 4;
for(file = FileA; file <= FileH; file++) {
pos = POSITION(rank, file);
if((bitBoard&POSBOARD(pos)) == POSBOARD(pos)) {
ret[offset] = 'X';
}
offset += 4;
}
// Add dotted line.
strncpy(ret + offset, dottedLine, LINE_LENGTH);
lineSide = OPPOSITE(lineSide);
}
ret[LINE_LENGTH * LINE_COUNT] = '\0';
return strdup(ret);
}
// ASCII-art representation of chessboard.
char*
StringChessSet(ChessSet *chessSet)
{
const int LINE_LENGTH = 36;
const int LINE_COUNT = 18;
// Include space for newlines.
char ret[LINE_LENGTH * LINE_COUNT + 1 + 1000];
char pieceChr;
int file, offset, rank;
Piece piece;
Position pos;
Side side;
Side lineSide = Black;
char *topLine = " A B C D E F G H \n";
char *dottedLine = " ---------------------------------\n";
char *blackLine = " | |...| |...| |...| |...|\n";
char *whiteLine = " |...| |...| |...| |...| |\n";
// Add top lines.
strncpy(ret, topLine, LINE_LENGTH);
strncpy(ret+LINE_LENGTH, dottedLine, LINE_LENGTH);
// Outputs chess set as ascii-art.
// pieces are upper-case if white, lower-case if black.
// P=pawn, R=rook, N=knight, B=bishop, Q=queen, K=king, .=empty square.
// e.g., the initial position is output as follows:-
// A B C D E F G H
// ---------------------------------
// 8 | r |.n.| b |.q.| k |.b.| n |.r.|
// ---------------------------------
// 7 |.p.| p |.p.| p |.p.| p |.p.| p |
// ---------------------------------
// 6 | |...| |...| |...| |...|
// ---------------------------------
// 5 |...| |...| |...| |...| |
// ---------------------------------
// 4 | |...| |...| |...| |...|
// ---------------------------------
// 3 |...| |...| |...| |...| |
// ---------------------------------
// 2 | P |.P.| P |.P.| P |.P.| P |.P.|
// ---------------------------------
// 1 |.R.| N |.B.| Q |.K.| B |.N.| R |
// ---------------------------------
for(rank = Rank8; rank >= Rank1; rank--) {
offset = 2*LINE_LENGTH + 2*LINE_LENGTH*(Rank8-rank);
// Add line.
strncpy(ret + offset, lineSide == White ? whiteLine : blackLine, LINE_LENGTH);
// Add number.
ret[offset] = '1'+rank;
// Add pieces.
// For the sake of debugging, avoid PieceAt() in case it is buggy.
offset += 4;
for(file = FileA; file <= FileH; file++) {
pos = POSITION(rank, file);
pieceChr = '\0';
for(side = White; side <= Black; side++) {
for(piece = Pawn; piece <= King; piece++) {
if((chessSet->Sets[side].Boards[piece]&POSBOARD(pos)) == POSBOARD(pos)) {
switch(piece) {
case Pawn:
pieceChr = 'P';
break;
case Rook:
pieceChr = 'R';
break;
case Knight:
pieceChr = 'N';
break;
case Bishop:
pieceChr = 'B';
break;
case Queen:
pieceChr = 'Q';
break;
case King:
pieceChr = 'K';
break;
default:
panic("Impossible.");
}
goto loop;
}
}
}
loop:
if(pieceChr != '\0') {
if(side == Black) {
pieceChr += 32;
}
ret[offset] = pieceChr;
}
offset += 4;
}
// Add dotted line.
strncpy(ret + offset, dottedLine, LINE_LENGTH);
lineSide = OPPOSITE(lineSide);
}
ret[LINE_LENGTH * LINE_COUNT] = '\0';
return strdup(ret);
}
char*
StringMove(Move move)
{
char ret[10];
if(move == INVALID_MOVE) {
return strdup("-");
}
sprintf(ret, "%s%s", StringPosition(FROM(move)),
StringPosition(TO(move)));
switch(TYPE(move)) {
case CastleKingSide:
return strdup("O-O");
case CastleQueenSide:
return strdup("O-O-O");
case PromoteKnight:
sprintf(ret+4, "=N");
break;
case PromoteBishop:
sprintf(ret+4, "=B");
break;
case PromoteRook:
sprintf(ret+4, "=R");
break;
case PromoteQueen:
sprintf(ret+4, "=Q");
break;
case EnPassant:
sprintf(ret+4, "ep");
break;
case Normal:
break;
default:
sprintf(ret+4, "??%d", TYPE(move));
}
return strdup(ret);
}
// String move in long algebraic form.
char*
StringMoveFull(Move move, Piece piece, bool capture)
{
char actionChr, pieceChr;
char *suffix, *from, *to;
char ret[1+2+1+2+2+1];
if(move == INVALID_MOVE) {
return strdup("-");
}
from = StringPosition(FROM(move));
to = StringPosition(TO(move));
switch(TYPE(move)) {
default:
suffix = "??";
break;
case CastleQueenSide:
return strdup("O-O-O");
case CastleKingSide:
return strdup("O-O");
case EnPassant:
suffix = "ep";
break;
case PromoteKnight:
suffix = "=N";
break;
case PromoteBishop:
suffix = "=B";
break;
case PromoteRook:
suffix = "=R";
break;
case PromoteQueen:
suffix = "=Q";
break;
case Normal:
suffix = "";
break;
}
if(capture) {
actionChr = 'x';
} else {
actionChr = '-';
}
pieceChr = CharPiece(piece);
if(pieceChr == 'P' || pieceChr == 'p') {
sprintf(ret, "%s%c%s%s", from, actionChr, to, suffix);
} else {
sprintf(ret, "%c%s%c%s%s", pieceChr, from, actionChr, to, suffix);
}
return strdup(ret);
}
char*
StringMoveHistory(MemorySlice *history, bool abbrev)
{
Move move;
Memory *curr;
Side side = White;
StringBuilder builder = NewStringBuilder();
int fullMoveCount = 1;
for(curr = history->Vals; curr != history->Curr; curr++) {
move = curr->Move;
if(abbrev) {
AppendString(&builder, "%s ", StringMove(move));
} else {
switch(side) {
case White:
AppendString(&builder, "%d. %s",
fullMoveCount, StringMove(move));
break;
case Black:
AppendString(&builder, " %s\n",
StringMove(move));
fullMoveCount++;
break;
}
}
side = OPPOSITE(side);
}
return builder.Length == 0 ? NULL : BuildString(&builder, true);
}
char*
StringPerft(PerftStats *s)
{
char ret[1000];
sprintf(ret, ""
"Count: %lu\n"
"Captures: %lu\n"
"En Passants: %lu\n"
"Castles: %lu\n"
"Promotions: %lu\n"
"Checks: %lu\n"
"Checkmates: %lu\n",
s->Count, s->Captures, s->EnPassants, s->Castles, s->Promotions, s->Checks,
s->Checkmates);
return strdup(ret);
}
char*
StringPiece(Piece piece)
{
char *ret;
switch(piece) {
case Pawn:
ret = "pawn";
break;
case Rook:
ret = "rook";
break;
case Knight:
ret = "knight";
break;
case Bishop:
ret = "bishop";
break;
case Queen:
ret = "queen";
break;
case King:
ret = "king";
break;
case MissingPiece:
ret = "#missing piece";
break;
default:
ret = "#invalid piece";
break;
}
return strdup(ret);
}
char*
StringPosition(Position pos)
{
char ret[2+1];
// Unsigned so we don't need to check < 0.
if(pos > 63) {
return strdup("#invalid position");
}
if(sprintf(ret, "%c%d", 'a'+FILE(pos), RANK(pos)+1) != 2) {
panic("Couldn't string position");
}
ret[2] = '\0';
return strdup(ret);
}
char*
StringSide(Side side)
{
char *ret;
switch(side) {
case White:
ret = "white";
break;
case Black:
ret = "black";
break;
default:
ret = "#invalid side";
break;
}
return strdup(ret);
}