-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.c
More file actions
253 lines (215 loc) · 8.05 KB
/
book.c
File metadata and controls
253 lines (215 loc) · 8.05 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
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "book.h"
#include "fen.h"
#include "utils.h"
Move m_getMoveFromUCI(char *uci, Board *p_board)
{
// Calcultate the rank and file from the uci string
uint8_t fromFile = uci[0] - 'a';
uint8_t fromRank = uci[1] - '1';
uint8_t toFile = uci[2] - 'a';
uint8_t toRank = uci[3] - '1';
// Calculate the board index from the rank and file
uint8_t from = fromFile + fromRank * 8;
uint8_t to = toFile + toRank * 8;
// Check for potential promotion
// The move would be to (2) from (2) and then promotion type (1)
uint8_t promotion = EMPTY;
if (strlen(uci) == 5)
{
switch (uci[4])
{
case 'R':
promotion = ROOK;
break;
case 'N':
promotion = KNIGHT;
break;
case 'B':
promotion = BISHOP;
break;
case 'Q':
promotion = QUEEN;
break;
}
}
// Itterate through all legal moves and return the one matching the from, to and promotion
ArrayList *legalMoves = getLegalMoves(p_board);
for(uint16_t i = 0; i < legalMoves->elements; i++)
{
Move move = legalMoves->array[i];
if (move.to == to && move.from == from && move.promotion == promotion)
{
freeMoveList(legalMoves);
return move;
}
}
// If there is no matching move, an error has occured
freeMoveList(legalMoves);
printf("Error while parsing uci: %ld\n", strlen(uci));
exit(EXIT_FAILURE);
}
void generateBook(Book *book, uint8_t depth, char *filename)
{
int line_count = 0;
size_t line_buf_size = 0;
char *line_buf = NULL;
ssize_t line_size = 0;
FILE *file = fopen(filename, "r");
if (!file)
{
printf("Unable to open file: %s", filename);
}
// Initialize the book without the initialnode and currentnode, they are set at the end because the pointers are moved with realloc
book->nNodes = 1;
book->nodes = malloc(sizeof(BookNode));
book->status = BOOK_READY;
Board initialBoard;
createBoardFormFEN(INITIAL_BOARD, &initialBoard);
book->nodes[0].hash = initialBoard.hash;
book->nodes[0].nNodes = 0;
book->nodes[0].moves = NULL;
book->nodes[0].nodeIndices = NULL;
// Iterate over all the lines of the file
// each line represents a game of annotated as pgn
line_size = getline(&line_buf, &line_buf_size, file);
while (line_size >= 0)
{
line_count++;
// Create the inital board of the game
Board board;
createBoardFormFEN(INITIAL_BOARD, &board);
// Loop over each uci move in the game and
char *spaceDelim = " ";
char *uci = strtok(line_buf, spaceDelim);
uint64_t currentNodeIndex = 0;
for (uint8_t i = 0; i < depth; i++)
{
Move move = m_getMoveFromUCI(uci, &board);
performMove(&move, &board);
// This is the index within the book->nodes list
uint64_t matchingNodeIndex = 0;
// Check if the new board exists in the book
uint8_t board_exists = 0;
for (uint64_t i = 0; i < book->nNodes; i++)
{
if (board.hash == book->nodes[i].hash)
{
board_exists = 1;
matchingNodeIndex = i;
break;
}
}
if (!board_exists)
{
// Create a new node and add it to the book nodes
book->nodes = realloc(book->nodes, ++book->nNodes * sizeof(BookNode));
// Set the matching-node-index to the index of the new node, which is the last
matchingNodeIndex = book->nNodes - 1;
book->nodes[matchingNodeIndex].hash = board.hash;
book->nodes[matchingNodeIndex].nNodes = 0;
book->nodes[matchingNodeIndex].moves = NULL;
book->nodes[matchingNodeIndex].nodeIndices = NULL;
// Add the new possition into the current node
BookNode *currentNode = &book->nodes[currentNodeIndex];
currentNode->nodeIndices = realloc(currentNode->nodeIndices, ++currentNode->nNodes * sizeof(uint64_t));
currentNode->nodeIndices[currentNode->nNodes - 1] = matchingNodeIndex;
currentNode->moves = realloc(currentNode->moves, currentNode->nNodes * sizeof(Move));
currentNode->moves[currentNode->nNodes - 1] = move;
}
else
{
// If the new board exists in the book, check if it exists in the position of the current node
uint8_t board_exists = 0;
for (uint8_t i = 0; i < book->nodes[currentNodeIndex].nNodes; i++)
{
if (board.hash == book->nodes[book->nodes[currentNodeIndex].nodeIndices[i]].hash)
{
board_exists = 1;
break;
}
}
// if the board does not exist in the current node, add it
if (!board_exists)
{
BookNode *currentNode = &book->nodes[currentNodeIndex];
currentNode->nodeIndices = realloc(currentNode->nodeIndices, ++currentNode->nNodes * sizeof(uint64_t));
currentNode->nodeIndices[currentNode->nNodes - 1] = matchingNodeIndex;
currentNode->moves = realloc(currentNode->moves, currentNode->nNodes * sizeof(Move));
currentNode->moves[currentNode->nNodes - 1] = move;
}
}
// Set the new current node
currentNodeIndex = matchingNodeIndex;
uci = strtok(NULL, spaceDelim);
// check if it contains the final score eg. "1/2-1/2" or "1-0"
if(uci == NULL || uci[0] == '1' || uci[0] == '0')
{
break;
}
}
line_size = getline(&line_buf, &line_buf_size, file);
}
book->initialNode = &book->nodes[0];
book->currentNode = &book->nodes[0];
free(line_buf);
line_buf = NULL;
fclose(file);
}
// Advances the book with the move, by changing the current node, and sets the book status
void advanceInBook(Book *book, Move move)
{
if (book->status == BOOK_ENDED)
return;
// Loop through all the possible moves and check if it matches any of the possible moves
for (uint8_t i = 0; i < book->currentNode->nNodes; i++)
{
if(move.from == book->currentNode->moves[i].from
&& move.to == book->currentNode->moves[i].to
&& move.promotion == book->currentNode->moves[i].promotion
)
{
uint64_t nextNodeIndex = book->currentNode->nodeIndices[i];
book->currentNode = &book->nodes[nextNodeIndex];
if(book->currentNode->nNodes == 0)
{
book->status = BOOK_ENDED;
}
return;
}
}
// If the moves that was played is not in the book, the book is out of the opening
book->status = BOOK_ENDED;
}
// Selects a move from the move list, the current node is also changed and status of the board is set
void getNextMove(Book *book, Move *move)
{
// If there are no moves in the current position, the book has ended
if (book->status == BOOK_ENDED)
return;
srand(time(NULL));
uint8_t randIndex = rand() % book->currentNode->nNodes;
*move = book->currentNode->moves[randIndex];
printf("Rand Index: %d\n", randIndex);
// Advance the to the next node
uint64_t nextNodeIndex = book->currentNode->nodeIndices[randIndex];
book->currentNode = &book->nodes[nextNodeIndex];
if (book->currentNode->nNodes == 0)
{
book->status = BOOK_ENDED;
}
}
void freeBook(Book *book)
{
for (uint64_t i = 0; i < book->nNodes; i++)
{
free(book->nodes[i].moves);
free(book->nodes[i].nodeIndices);
}
free(book->nodes);
}