-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook.c
296 lines (115 loc) · 2.52 KB
/
book.c
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
/*
This File is part of Boo's Chess Engine
Copyright 2000 by Christopher Bowron
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "bce.h"
#include "colors.h"
#include "pg.h"
int index_count;
int binary_book = 0;
#define BOOK_SIZE 800
extern move book_moves[];
move bookopening()
{
int i;
int matches = 0;
int frequency = 0;
move last_match = 0;
for (i=0;i<index_count;i++)
{
if (mem.book[i].hash == board->hash)
{
matches ++;
frequency += mem.book[i].count;
last_match = mem.book[i].m;
}
}
if (matches == 0)
return dummymove;
else if (matches == 1)
return last_match;
else
{
int n = getrandomnumber() % frequency;
for (i=0;i<index_count;i++)
{
if (mem.book[i].hash == board->hash)
{
n -= mem.book[i].count;
if (n <= 0)
return mem.book[i].m;
}
}
}
return dummymove;
}
int process_opening(move *move_ptr)
{
setupboard();
ply = 0;
while (*move_ptr)
{
int i;
move m = *move_ptr;
move_ptr++;
for (i=0;i<TABLE_SIZE;i++)
{
if ((mem.book[i].hash == board->hash)&&(mem.book[i].m == m))
{
mem.book[i].count++;
break;
}
if (mem.book[i].hash == 0)
{
mem.book[i].hash = board->hash;
mem.book[i].m = m;
mem.book[i].count = 1;
index_count++;
break;
}
}
domove(m);
}
return 1;
}
int bk_cmp(const void *a, const void *b)
{
const book_table *bt_a = a;
const book_table *bt_b = b;
return (bt_a -> hash < bt_b -> hash);
}
void loadbook()
{
int count = 0;
// int line_index = 0;
clear_screen();
pgPrintCenter(10,lightblue,"Clearing Hash Tables");
pgScreenFlipV();
clear_hash();
move *move_ptr = book_moves;
for (;;)
{
if (*move_ptr == 0) break;
process_opening(move_ptr);
// find the 0 at the end
while (*move_ptr)
move_ptr++;
// advance past it...
move_ptr++;
//draw_board();
if (count%(BOOK_SIZE/20) == 0)
{
//pgFillvram(black);
draw_board();
pgFancyPrint(35,3,lightblue,"Loading Book",0,1,1);
pgPrintInt(35,4,lightblue,100*count/BOOK_SIZE);
pgFancyPrint(37,4,lightblue,"%",0,1,1);
pgScreenFlipV();
}
count++;
}
setupboard();
}