-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.c
More file actions
777 lines (678 loc) · 20.7 KB
/
board.c
File metadata and controls
777 lines (678 loc) · 20.7 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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include<pthread.h>
#include "AI.h"
#include "common.h"
#include "board.h"
#include "bitboard.h"
static piece_t fen_to_chesspiece(char c)
{
switch (c) {
case 'P':
return WHITE_PAWN;
break;
case 'N':
return WHITE_KNIGHT;
break;
case 'B':
return WHITE_BISHOP;
break;
case 'R':
return WHITE_ROOK;
break;
case 'Q':
return WHITE_QUEEN;
break;
case 'K':
return WHITE_KING;
break;
case 'p':
return BLACK_PAWN;
break;
case 'n':
return BLACK_KNIGHT;
break;
case 'b':
return BLACK_BISHOP;
break;
case 'r':
return BLACK_ROOK;
break;
case 'q':
return BLACK_QUEEN;
break;
case 'k':
return BLACK_KING;
break;
default:
return -1; // error
break;
}
}
static char chesspiece_to_fen(piece_t c)
{
switch (c) {
case WHITE_PAWN:
return 'P';
break;
case WHITE_KNIGHT:
return 'N';
break;
case WHITE_BISHOP:
return 'B';
break;
case WHITE_ROOK:
return 'R';
break;
case WHITE_QUEEN:
return 'Q';
break;
case WHITE_KING:
return 'K';
break;
case BLACK_PAWN:
return 'p';
break;
case BLACK_KNIGHT:
return 'n';
break;
case BLACK_BISHOP:
return 'b';
break;
case BLACK_ROOK:
return 'r';
break;
case BLACK_QUEEN:
return 'q';
break;
case BLACK_KING:
return 'k';
break;
default:
return -1; // error
break;
}
}
board_t *new_board(char *_fen)
{
int i, j;
int col, row;
board_t *board;
char *fen_ptr, *rank, *tmp;
if (_fen == NULL || *_fen == 0)
_fen = DEFAULT_FEN;
char fen[strlen(_fen) + 1];
strcpy(fen, _fen);
board = calloc(1, sizeof (board_t));
if (board == NULL)
return NULL;
for (i = 0; i < 8; i++)
board->board_2d[i] = &board->_board[i * 8];
board->board = board->_board; // backwards compatability
fen_ptr = strchr(fen, ' ');
*fen_ptr++ = 0;
// initialize board
rank = fen;
tmp = strchr(fen, '/');
*tmp++ = 0;
row = 7;
for (i = 0; i < 8; i++) {
col = 0;
for (j = 0; j < strlen(rank); j++) {
if (isdigit((int) rank[j])) {
int cnt = 0;
for (; cnt < rank[j] - '0'; ++col, ++cnt)
board->board_2d[row][col] = P_EMPTY;
} else {
board->board_2d[row][col] = fen_to_chesspiece(rank[j]);
col++;
}
}
row--;
rank = tmp;
if (tmp) {
tmp = strchr(tmp, '/');
if (tmp) {
*tmp++ = 0;
}
}
}
board->is_check = -1;
board->moves_count = -1;
board->turn = *fen_ptr == 'w' ? WHITE : BLACK;
init_bitboards(_fen, board);
return board;
}
void free_board(board_t *b)
{
free(b);
}
void generate_all_moves(board_t *b)
{
// already calculated moves for this round
if (b->moves_count != -1)
return;
b->moves_count = 0;
bb_generate_all_legal_moves(b);
}
int is_check(board_t *board)
{
if (board->is_check == -1)
bb_calculate_check(board);
return board->is_check;
}
int is_stalemate(board_t *b)
{
generate_all_moves(b);
return b->moves_count == 0;
}
int is_checkmate(board_t *b)
{
return is_stalemate(b) && is_check(b);
}
void swapturn(board_t *b)
{
b->turn = -b->turn;
b->is_check = -1;
b->moves_count = -1;
}
static inline void del_move(board_t *b, int n)
{
if (--b->moves_count != -1)
b->moves[n] = b->moves[b->moves_count];
}
int undo_move(board_t *b, int n)
{
struct move *m;
int tx, fx, ret;
m = &b->moves[n];
// convert coordinates from bitboard coords (0->7, 1->6, ..., 7->0)
tx = ~m->to.x & 0x7;
fx = ~m->frm.x & 0x7;
#ifdef BOARD_CONSISTENCY_TEST
printf("%s: consistency check before move has been undone\n", __func__);
printf("%s: move was from %d, %d to %d, %d\n", __func__, m->frm.y, fx, m->to.y, tx);
board_consistency_check(b);
#endif
b->is_check = -1;
ret = bb_undo_move(b, n);
PIECE(b->board, m->frm.y, fx) = PIECE(b->board, m->to.y, tx);
PIECE(b->board, m->to.y, tx) = b->backup.piece;
if (b->backup.promotion) {
PIECE(b->board, m->frm.y, fx) = b->turn == WHITE
? WHITE_PAWN : BLACK_PAWN;
}
switch (ret) {
case 3: // move was white short castling
PIECE(b->board, 0, 7) = PIECE(b->board, 0, 5);
PIECE(b->board, 0, 5) = P_EMPTY;
break;
case 4: // move was white long castling
PIECE(b->board, 0, 0) = PIECE(b->board, 0, 3);
PIECE(b->board, 0, 3) = P_EMPTY;
break;
case 5: // move was black short castling
PIECE(b->board, 7, 7) = PIECE(b->board, 7, 5);
PIECE(b->board, 7, 5) = P_EMPTY;
break;
case 6: // move was black long castling
PIECE(b->board, 7, 0) = PIECE(b->board, 7, 3);
PIECE(b->board, 7, 3) = P_EMPTY;
break;
case 7: // move was en passant
PIECE(b->board, m->to.y, tx) = P_EMPTY;
PIECE(b->board, m->to.y - 1 * b->turn, tx) = b->backup.piece;
break;
default:
break;
}
#ifdef BOARD_CONSISTENCY_TEST
printf("%s: consistency check after move has been undone\n", __func__);
printf("%s: move was from %d, %d to %d, %d\n", __func__, m->frm.y, fx, m->to.y, tx);
board_consistency_check(b);
#endif
return 1;
}
static void handle_special_moves(board_t *b, struct move *m, int ret_value)
{
int tx;
tx = bb_col_to_AI_col(m->to.x);
switch (ret_value) {
case 3: // move was white short castling
PIECE(b->board, 0, 5) = PIECE(b->board, 0, 7);
PIECE(b->board, 0, 7) = P_EMPTY;
break;
case 4: // move was white long castling
PIECE(b->board, 0, 3) = PIECE(b->board, 0, 0);
PIECE(b->board, 0, 0) = P_EMPTY;
break;
case 5: // move was black short castling
PIECE(b->board, 7, 5) = PIECE(b->board, 7, 7);
PIECE(b->board, 7, 7) = P_EMPTY;
break;
case 6: // move was black long castling
PIECE(b->board, 7, 3) = PIECE(b->board, 7, 0);
PIECE(b->board, 7, 0) = P_EMPTY;
break;
case 7: // en passant
b->backup.piece = PIECE(b->board, m->to.y - 1 * b->turn, tx);
PIECE(b->board, m->to.y - 1 * b->turn, tx) = P_EMPTY;
break;
}
if (b->backup.promotion) {
switch (tolower(m->promotion)) {
case 'q':
PIECE(b->board, m->to.y, tx) = b->turn == WHITE
? WHITE_QUEEN : BLACK_QUEEN;
break;
case 'n':
PIECE(b->board, m->to.y, tx) = b->turn == WHITE
? WHITE_KNIGHT : BLACK_KNIGHT;
break;
case 'b':
PIECE(b->board, m->to.y, tx) = b->turn == WHITE
? WHITE_BISHOP : BLACK_BISHOP;
break;
case 'r':
PIECE(b->board, m->to.y, tx) = b->turn == WHITE
? WHITE_ROOK : BLACK_ROOK;
break;
default:
// use queen as default promotion if no other option was set
PIECE(b->board, m->to.y, tx) = b->turn == WHITE
? WHITE_QUEEN : BLACK_QUEEN;
break;
}
}
}
/* should only be called from UCI mode. It does not verify that the move did not
* put the player in check */
int do_actual_move(board_t *b, struct move *m, struct uci *iface)
{
int tx, fx, ret;
// convert coordinates from bitboard coords (0->7, 1->6, ..., 7->0)
tx = bb_col_to_AI_col(m->to.x);
fx = bb_col_to_AI_col(m->frm.x);
#ifdef BOARD_CONSISTENCY_TEST
printf("%s: consistency check before move has been done\n", __func__);
printf("%s: move is from %d, %d to %d, %d\n", __func__, m->frm.y, fx, m->to.y, tx);
print_board(b->board);
bb_print(b->white_pieces.apieces);
board_consistency_check(b);
#endif
b->is_check = -1;
ret = bb_do_actual_move(b, m);
if (ret == 0) {
struct bitboard *bb = b->turn == WHITE ? &b->white_pieces :
&b->black_pieces;
printf("FATAL ERROR: %s: SOMETHING WENT WRONG\n", __func__);
printf("%s: HALTING EXECUTION!!1\n", __func__);
printf("position: %s\n", iface->position);
print_board(b->board);
bb_print(bb->apieces);
asm("int3");
return 0;
} else if (ret == 2) // attempted castling move was illegal
return 0;
b->backup.piece = PIECE(b->board, m->to.y, tx);
PIECE(b->board, m->to.y, tx) = PIECE(b->board, m->frm.y, fx);
PIECE(b->board, m->frm.y, fx) = P_EMPTY;
handle_special_moves(b, m, ret);
#ifdef BOARD_CONSISTENCY_TEST
printf("%s: consistency check after move has been done\n", __func__);
printf("%s: move was from %d, %d to %d, %d\n", __func__, m->frm.y, fx, m->to.y, tx);
board_consistency_check(b);
#endif
// pthread_mutex_unlock(&lock);
return 1;
}
/* call this if you are totally sure the move is a legal one */
int do_move(board_t *b, int n)
{
struct move *m;
int tx, fx, ret;
if (n >= b->moves_count)
return 0;
m = &b->moves[n];
// convert coordinates from bitboard coords (0->7, 1->6, ..., 7->0)
tx = bb_col_to_AI_col(m->to.x);
fx = bb_col_to_AI_col(m->frm.x);
#ifdef BOARD_CONSISTENCY_TEST
printf("%s: consistency check before move has been done\n", __func__);
printf("%s: move is from %d, %d to %d, %d\n", __func__, m->frm.y, fx, m->to.y, tx);
board_consistency_check(b);
#endif
b->is_check = -1;
ret = bb_do_move(b, n);
if (ret == 0) {
printf("FATAL ERROR: %s: SOMETHING WENT WRONG\n", __func__);
printf("%s: HALTING EXECUTION!!1\n", __func__);
printf("b = %p, n = %d, moves_count = %d\n",
b, n, b->moves_count);
asm("int3");
return 0;
} else if (ret == 2) {
del_move(b, n);
return 0;
}
if (is_check(b)) {
bb_undo_move(b, n);
del_move(b, n);
b->is_check = -1;
return 0;
}
b->backup.piece = PIECE(b->board, m->to.y, tx);
PIECE(b->board, m->to.y, tx) = PIECE(b->board, m->frm.y, fx);
PIECE(b->board, m->frm.y, fx) = P_EMPTY;
handle_special_moves(b, m, ret);
#ifdef BOARD_CONSISTENCY_TEST
printf("%s: consistency check after move has been done\n", __func__);
printf("%s: move was from %d, %d to %d, %d\n", __func__, m->frm.y, fx, m->to.y, tx);
board_consistency_check(b);
#endif
return 1;
}
/* call this if you are _NOT_ sure the move is a legal one.
* This is a wrapper function for do_move.
* returns -1 if there are no more legal moves to do
* returns 0 if n > b->moves_count
* returns 1 if a move was successfully taken */
int move(board_t *b, int n)
{
do {
if (is_stalemate(b))
return -1;
if (n >= b->moves_count)
return 0;
} while (do_move(b, n) != 1);
return 1;
}
char *get_fen(board_t *board, char* fen)
{
int row, col, cnt;
char *ret = fen, *ptr;
ptr = ret;
cnt = 0;
for (row = 7; row >= 0; row--) {
for (col = 0; col < 8; col++) {
if (board->board_2d[row][col] == P_EMPTY) {
++cnt;
} else {
if (cnt) {
*ptr++ = cnt + '0';
cnt = 0;
}
*ptr++ = chesspiece_to_fen(board->board_2d[row][col]);
}
}
if (cnt) {
*ptr++ = cnt + '0';
cnt = 0;
}
*ptr++ = '/';
}
*(ptr - 1) = ' ';
*ptr++ = board->turn == WHITE ? 'w' : 'b';
*ptr++ = ' ';
*ptr = '-';
char castling[5];
memset(castling, 0, sizeof(castling));
// set white castling options
if (!board->white_pieces.king_has_moved) {
if (!board->white_pieces.short_rook_moved)
strcat(castling, "K");
if (!board->white_pieces.long_rook_moved)
strcat(castling, "Q");
}
// set black castling options
if (!board->black_pieces.king_has_moved) {
if (!board->black_pieces.short_rook_moved)
strcat(castling, "k");
if (!board->black_pieces.long_rook_moved)
strcat(castling, "q");
}
strcpy(ptr, castling);
// TODO en passant
if (castling[0] == 0)
strcat(ptr, "- - 0 1");
else
strcat(ptr, " - 0 1");
return ret;
}
const char *piece_to_str(piece_t p)
{
const char *ret = NULL;
static const char *strings[] = {
"pawn(w)",
"rook(w)",
"knight(w)",
"bishop(w)",
"queen(w)",
"king(w)",
"pawn(b)",
"rook(b)",
"knight(b)",
"bishop(b)",
"queen(b)",
"king(b)",
"empty",
};
if (p & (1 << 12))
ret = strings[12];
else if (p > 1 << 5) {
ret = strings[get_moves_index(p) + 6];
} else if (p > 0)
ret = strings[get_moves_index(p) + 0];
return ret;
}
void print_board(piece_t *board)
{
int i, j;
printf(" 0 1 2 3"
" 4 5 6 7\n");
for (i = 7; i >= 0; i--) {
printf("%d ", i + 1);
for (j = 0; j < 8; j++)
printf("%10s", piece_to_str(PIECE(board, i, j)));
printf(" %d\n", i);
}
printf(" a b c d"
" e f g h\n\n");
}
void print_move(board_t *board, int n)
{
printf("%02d: (%d, %d) -> (%d, %d)\n", n, board->moves[n].frm.y, ~board->moves[n].frm.x & 0x7,
board->moves[n].to.y, ~board->moves[n].to.x & 0x7);
}
void print_legal_moves(board_t *board)
{
int i;
printf("count: %d\n", board->moves_count);
for (i = 0; i < board->moves_count; i++)
printf("%02d: (%d, %d) -> (%d, %d)\n", i, board->moves[i].frm.y, ~board->moves[i].frm.x & 0x7,
board->moves[i].to.y, ~board->moves[i].to.x & 0x7);
}
static void internal_notation_to_uci(char *u, move_t *m)
{
static int lookup[] = {'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a'};
u[0] = lookup[m->frm.x];
u[1] = m->frm.y + '1';
u[2] = lookup[m->to.x];
u[3] = m->to.y + '1';
u[4] = m->promotion;
u[5] = 0;
}
static void uci_move_notation_to_internal(char *u, move_t *m)
{
u[0] = tolower(u[0]) - 'a';
u[1] = u[1] - '1';
u[2] = tolower(u[2]) - 'a';
u[3] = u[3] - '1';
m->frm.x = AI_col_to_bb_col(u[0]);
m->frm.y = u[1];
m->to.x = AI_col_to_bb_col((int) u[2]);
m->to.y = u[3];
m->promotion = u[4];
}
int do_uci_move(board_t *board, struct uci *iface)
{
struct move m;
int fx, tx;
char *mv, move_copy[32];
uci_start_search(iface, board);
mv = uci_get_next_move(iface);
if (!strcmp(mv, "(none)")) {
int ret = do_move_random_piece(board, iface);
if (ret == 1)
printf("ERROR: move error in do_uci_move\n");
else
return ret;
}
strncpy(move_copy, mv, sizeof (move_copy));
memset(&m, 0, sizeof (m));
uci_move_notation_to_internal(move_copy, &m);
//uci_register_new_move(iface, mv);
fx = bb_col_to_AI_col(m.frm.x);
tx = bb_col_to_AI_col(m.to.x);
if ((PIECE(board->board, m.frm.y, fx) == WHITE_PAWN ||
PIECE(board->board, m.frm.y, fx) == BLACK_PAWN) &&
PIECE(board->board, m.to.y, tx) == P_EMPTY) {
m.en_passant = 1;
}
do_actual_move(board, &m, iface);
if (is_stalemate(board))
return 0;
if (is_checkmate(board))
return -1;
swapturn(board);
return 1;
}
void register_move_to_uci(struct move *m, struct uci *iface, board_t *board)
{
char uci[32];
internal_notation_to_uci(uci, m);
//uci_register_new_move(iface, uci);
}
#ifdef BOARD_CONSISTENCY_TEST
static void consistency_error(char *color, char *piece, int y, int x,
board_t *board, u64 bitboard)
{
printf("board consistency check failed! %s %s on "
"AI board is not set on bitboard (%d, %d)\n",
color, piece, y, x);
print_board(board->board);
bb_print(bitboard);
printf("Press a key to continue check..\n");
getchar();
}
void board_consistency_check(board_t *board)
{
int i, j, num_errors;
struct bitboard *bb, *bb2;
char *tmp;
u64 tmpboard;
num_errors = 0;
bb = &board->white_pieces;
bb2 = &board->white_pieces;
tmpboard = bb->pawns & bb->rooks & bb->knights & bb->bishops & bb->queens & bb->king &
bb2->pawns & bb2->rooks & bb2->knights & bb2->bishops & bb2->queens & bb2->king;
if (tmpboard) {
printf("error: multiple pieces in same square!:\n");
bb_print(tmpboard);
printf("black pawns:\n");
bb_print(bb->pawns);
printf("black rooks:\n");
bb_print(bb->rooks);
printf("black knights:\n");
bb_print(bb->pawns);
printf("black bishops:\n");
bb_print(bb->bishops);
printf("black queens:\n");
bb_print(bb->queens);
printf("black king:\n");
bb_print(bb->king);
printf("white pawns:\n");
bb_print(bb2->pawns);
printf("white rooks:\n");
bb_print(bb2->rooks);
printf("white knights:\n");
bb_print(bb2->pawns);
printf("white bishops:\n");
bb_print(bb2->bishops);
printf("white queens:\n");
bb_print(bb2->queens);
printf("white king:\n");
bb_print(bb2->king);
printf("Press a key to continue check..\n");
getchar();
++num_errors;
}
for (i = 7; i >= 0; i--) {
for (j = 0; j < 8; j++) {
if (PIECE(board->board, i, j) > WHITE_KING) {
bb = &board->black_pieces;
tmp = "black";
} else {
bb = &board->white_pieces;
tmp = "white";
}
switch (PIECE(board->board, i, j)) {
case WHITE_PAWN:
case BLACK_PAWN:
if (!is_set(bb->pawns, AI_coord_to_index(i, j))) {
consistency_error(tmp, "pawn", i, j, board, bb->pawns);
++num_errors;
}
break;
case WHITE_ROOK:
case BLACK_ROOK:
if (!is_set(bb->rooks, AI_coord_to_index(i, j))) {
consistency_error(tmp, "rook", i, j, board, bb->rooks);
++num_errors;
}
break;
case WHITE_KNIGHT:
case BLACK_KNIGHT:
if (!is_set(bb->knights, AI_coord_to_index(i, j))) {
consistency_error(tmp, "knight", i, j, board, bb->knights);
++num_errors;
}
break;
case WHITE_BISHOP:
case BLACK_BISHOP:
if (!is_set(bb->bishops, AI_coord_to_index(i, j))) {
consistency_error(tmp, "bishop", i, j, board, bb->bishops);
++num_errors;
}
break;
case WHITE_QUEEN:
case BLACK_QUEEN:
if (!is_set(bb->queens, AI_coord_to_index(i, j))) {
consistency_error(tmp, "queen", i, j, board, bb->queens);
++num_errors;
}
break;
case WHITE_KING:
case BLACK_KING:
if (!is_set(bb->king, AI_coord_to_index(i, j))) {
consistency_error(tmp, "king", i, j, board, bb->king);
++num_errors;
}
break;
case P_EMPTY:
if (is_set(bb->apieces, AI_coord_to_index(i, j))) {
consistency_error("empty", "square", i, j, board, board->white_pieces.apieces);
++num_errors;
}
break;
}
}
}
if (num_errors == 0)
printf("No errors\n");
else
printf("Total %d errors\n", num_errors);
}
#endif