blob: e87a666614acb9433b77a054f21fa5038e3e4c31 (
plain)
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
|
#include <stdio.h>
#include <string.h>
#include "board.h"
Bitboard KNIGHT_ATTACKS[64];
Bitboard WHITE_PAWN_ATTACKS[64];
Bitboard BLACK_PAWN_ATTACKS[64];
Board parseFEN(char* FEN) {
Board board;
for (int i = 0; i < 12; i++) board.pieces[i] = 0;
int rank = 7;
int file = 0;
for (int k = 0; k < strlen(FEN); k++) {
int c = FEN[k];
int index = rank * 8 + file;
Bitboard position = BIT << index;
if (c > '0' && c <= '8') file += c - '0';
else {
switch (c) {
case 'R':
board.pieces[ROOK] |= position;
break;
case 'N':
board.pieces[KNIGHT] |= position;
break;
case 'B':
board.pieces[BISHOP] |= position;
break;
case 'Q':
board.pieces[QUEEN] |= position;
break;
case 'K':
board.pieces[KING] |= position;
break;
case 'P':
board.pieces[PAWN] |= position;
break;
case 'r':
board.pieces[ROOK | BLACK] |= position;
break;
case 'n':
board.pieces[KNIGHT | BLACK] |= position;
break;
case 'b':
board.pieces[BISHOP | BLACK] |= position;
break;
case 'q':
board.pieces[QUEEN | BLACK] |= position;
break;
case 'k':
board.pieces[KING | BLACK] |= position;
break;
case 'p':
board.pieces[PAWN | BLACK] |= position;
break;
case '/':
rank--;
file = -1; // So that it becomes 0
break;
}
file++;
}
}
return board;
}
void printBoard(Board board) {
printf("\n");
for (int rank = 7; rank >= 0; rank--) {
printf("%i|", rank + 1);
for (int file = 0; file < 8; file++) {
int index = rank * 8 + file;
Bitboard position = BIT << index;
int found = 0;
for (int piece = 0; piece < 12; piece++) {
if (board.pieces[piece] & position) {
found = 1;
printf("%s ", pieces[piece]);
break;
}
}
if (!found) printf(". ");
}
printf("\n");
}
printf(" a b c d e f g h\n");
}
void precomputeKnightAttackTable(Bitboard attacks[64]) {
for (int index = 0; index < 64; index++) {
U64 position = BIT << index;
attacks[index] =
((position & notAFile & notBFile) << 6) |
((position & notGFile & notHFile) << 10) |
((position & notAFile) << 15) |
((position & notHFile) << 17) |
((position & notGFile & notHFile) >> 6) |
((position & notAFile & notBFile) >> 10) |
((position & notHFile) >> 15) |
((position & notAFile) >> 17);
}
}
void precomputeKingAttackTable(Bitboard attacks[64]) {
for (int index = 0; index < 64; index++) {
U64 position = BIT << index;
attacks[index] =
(position & notAFile) << 7 | (position << 8) | (position & notHFile) << 9 |
(position & notAFile) >> 1 | (position & notHFile) << 1 |
(position & notAFile) >> 9 | (position >> 8) | (position & notHFile) >> 7;
}
}
void precomputePawnAttackTable(Bitboard attacks[64], BYTE color) {
for (int index = 0; index < 64; index++) {
U64 position = BIT << index;
if (color == WHITE) attacks[index] = (position & notAFile) << 7 | (position & notHFile) << 9;
else attacks[index] = (position & notAFile) >> 9 | (position & notHFile) >> 7;
}
}
/* Given a TO square return all pseudo-legal FROM squares which attack or protect that square
* In order to be color-specific, intersect the result with appropriate color bitboard */
Bitboard attacksToSquare(Board board, enumSquare sq) {
Bitboard knights = board.pieces[KNIGHT] | board.pieces[KNIGHT | BLACK];
Bitboard whitePawns = board.pieces[PAWN];
Bitboard blackPawns = board.pieces[PAWN | BLACK];
return (KNIGHT_ATTACKS[sq] & knights)
| (BLACK_PAWN_ATTACKS[sq] & whitePawns)
| (WHITE_PAWN_ATTACKS[sq] & blackPawns);
};
|