-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfen.c
More file actions
149 lines (138 loc) · 3.92 KB
/
fen.c
File metadata and controls
149 lines (138 loc) · 3.92 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
#include <stdio.h>
#include "fen.h"
#include "hashing.h"
void createBoardFormFEN(char *FEN, Board *p_board)
{
// Fill the board with empty squares before placing the piaces
for (int8_t square = 0; square < 64; square++)
{
p_board->board[square] = EMPTY;
}
// Read the board position
for (int8_t rank = 7; rank >= 0; rank--)
{
int8_t file = 0;
while (*FEN != '/' && *FEN != ' ')
{
// Check for numbers (empty squares)
if(*FEN <= '9' && *FEN >= '0')
{
// Move to the next file and move to the next FEN char
file += *FEN - '0';
FEN++;
continue;
}
// Check the next piece
uint8_t piece = EMPTY;
switch (*FEN)
{
case 'q':
case 'Q':
piece = QUEEN;
break;
case 'k':
p_board->blackKing = file + rank * 8;
piece = KING;
break;
case 'K':
p_board->whiteKing = file + rank * 8;
piece = KING;
break;
case 'r':
case 'R':
piece = ROOK;
break;
case 'n':
case 'N':
piece = KNIGHT;
break;
case 'b':
case 'B':
piece = BISHOP;
break;
case 'p':
case 'P':
piece = PAWN;
break;
default:
printf("Illegal piece type char in FEN %c\n", *FEN);
break;
}
// Set the color of the piece based on the capitalization of the char
// Non-capital letters => black
if(*FEN >= 'a' && *FEN <= 'z')
{
piece |= BLACK;
}
// Capital letters => white
else if(*FEN >= 'A' && *FEN <= 'Z')
{
piece |= WHITE;
}
// Illegal character
else
{
printf("Illegal color type of the char in FEN %c\n", *FEN);
}
// Put the piece on the bord and move to the next rank and FEN char
p_board->board[file + rank*8] = piece;
FEN++;
file++;
}
FEN++;
}
// Read the game status
// Set the turn
if(*FEN == 'w'){
p_board->turn = WHITE;
}
else if(*FEN == 'b')
{
p_board->turn = BLACK;
}
FEN += 2;
// Castle Rights
p_board->castleRights = 0;
while (*FEN != ' ')
{
switch (*FEN)
{
case 'Q':
p_board->castleRights |= WHITE_QUEEN_CASTLE_MASK;
break;
case 'q':
p_board->castleRights |= BLACK_QUEEN_CASTLE_MASK;
break;
case 'K':
p_board->castleRights |= WHITE_KING_CASTLE_MASK;
break;
case 'k':
p_board->castleRights |= BLACK_KING_CASTLE_MASK;
break;
case '-':
p_board->castleRights = 0;
break;
}
FEN++;
}
FEN++;
// En passant target square in algebraic notation
// After this section, the FEN pointer points towards the letter before the space
if(*FEN == '-')
{
p_board->enPassantTarget = -1;
}
else
{
// Calculated as file + rank * 8
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsequence-point"
p_board->enPassantTarget = (*FEN - 'a') + (*(FEN++) - '0') * 8;
#pragma GCC diagnostic pop
}
FEN+=2;
p_board->halfMoves = *FEN - '0';
FEN+=2;
p_board->fullMoves = *FEN - '0';
p_board->hash = zobristHash(p_board);
}