#include "sjeng.h"
#include "protos.h"
#include "extvars.h"
char book[4000][161];
char book_flags[4000][41];
int num_book_lines;
int book_ply;
int use_book;
char opening_history[STR_BUFF];
#define book_always 1
#define book_never 2
#define book_interesting 3
#define book_solid 4
#define book_murky 5
int init_book (void) {
FILE *f_book;
int ch, i = 0, j = 0;
int tagmode = FALSE;
int commentmode = FALSE;
memset(book_flags, 0, sizeof(book_flags));
memset(book, 0, sizeof(book));
num_book_lines = 0;
srand((unsigned) time (NULL));
if (Variant == Normal)
{
if ((f_book = fopen ("normal.opn", "r")) == NULL)
return FALSE;
}
else if (Variant == Crazyhouse)
{
if ((f_book = fopen ("zh.opn", "r")) == NULL)
return FALSE;
}
else if (Variant == Suicide)
{
if ((f_book = fopen ("suicide.opn", "r")) == NULL)
return FALSE;
}
else if (Variant == Losers)
{
if ((f_book = fopen ("losers.opn", "r")) == NULL)
return FALSE;
}
else
{
if ((f_book = fopen ("bug.opn", "r")) == NULL)
return FALSE;
}
while ((ch = getc(f_book)) != EOF) {
if (commentmode)
{
if (ch == '/')
{
commentmode = FALSE;
}
}
else
{
if (ch == '\n') {
if (j > 0)
{
book[i++][j] = '\0';
j = 0;
}
tagmode = FALSE;
}
else if (ch == '!')
{
if (!tagmode)
{
book_flags[i][((j + 1) / 4) - 1] = book_always;
tagmode = TRUE;
}
else
{
book_flags[i][((j + 1) / 4) - 1] = book_murky;
}
}
else if (ch == '?')
{
if (!tagmode)
{
book_flags[i][((j + 1) / 4) - 1] = book_never;
tagmode = TRUE;
}
else
{
book_flags[i][((j + 1) / 4) - 1] = book_interesting;
}
}
else if (ch == '=')
{
book_flags[i][((j + 1) / 4) - 1] = book_solid;
tagmode = TRUE;
}
else if ((ch == ' ') || (ch == '\t'))
{
tagmode = FALSE;
}
else if (ch == '/')
{
commentmode = TRUE;
}
else if (j < 160 && i < 4000)
{
book[i][j++] = ch;
tagmode = FALSE;
}
}
}
num_book_lines = i;
fclose(f_book);
return TRUE;
}
move_s choose_book_move (void) {
static int last_book_move = 0;
int book_match = FALSE;
int i, j, num_moves, random_number, num_replies;
char possible_move[5], coord_move[5];
move_s book_replies[4000], moves[400];
char force_move = FALSE;
int ic;
srand(time(0));
if (!book_ply)
last_book_move = 0;
num_replies = 0;
gen(&moves[0]);
num_moves = numb_moves;
for (i = last_book_move; i < num_book_lines; i++) {
if (!strncmp(opening_history, book[i], (book_ply * 4)) || (!book_ply)) {
if ((book_flags[i][book_ply] != book_never)
&& (book_flags[i][book_ply] != book_murky))
{
if (book_flags[i][book_ply] == book_always)
{
if (force_move != TRUE)
{
force_move = TRUE;
num_replies = 0;
}
}
if ((force_move != TRUE) ||
((force_move == TRUE) &&
(book_flags[i][book_ply] == book_always)))
{
strncpy(possible_move, book[i] + (book_ply * 4), 4);
possible_move[4] = '\0';
for (j = 0; j < num_moves; j++)
{
comp_to_coord(moves[j], coord_move);
if (!strcmp(possible_move, coord_move))
{
ic = in_check();
make(&moves[0], j);
if (check_legal(&moves[0], j, ic))
{
book_replies[num_replies++] = moves[j];
book_match = TRUE;
}
unmake(&moves[0], j);
}
}
}
}
}
if (!book_match && num_replies)
break;
}
if (!num_replies)
return dummy;
printf("Book moves: %d\n", num_replies);
random_number = rand() % num_replies;
return book_replies[random_number];
}