-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranspositionTable.c
166 lines (119 loc) · 4.13 KB
/
transpositionTable.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "transpositionTable.h"
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
void init_hash_table(){
init_zobrist();
pos_transp_table = (PosTransp*)malloc(sizeof(PosTransp)*(TABLE_SIZE));
if(pos_transp_table == NULL){
printf("Please reduce TABLE_SIZE in transpositionTable.h\n");
exit(0);
}
printf("Reserved : %ld bytes\n",(sizeof(PosTransp)*(TABLE_SIZE)));
printf("The agent will free this space when you close him!\n");
//all empty
for(int i=0; i < TABLE_SIZE; i++)
pos_transp_table[i].type =0;
}
void init_zobrist(){
//generate random bit streams
for (int i=0; i < BOARD_COLUMNS*BOARD_ROWS; i++){
zobrist_table[i][0] = ((((long) rand() << 0) & 0x00000000FFFFFFFFull) | (((long) rand() << 32) & 0xFFFFFFFF00000000ull));
zobrist_table[i][1] = ((((long) rand() << 0) & 0x00000000FFFFFFFFull) | (((long) rand() << 32) & 0xFFFFFFFF00000000ull));
}
}
unsigned long zobrist_hash(Position* pos){
int i,j;
unsigned long h = 0;
for (i= 0; i < BOARD_ROWS; i++){
for (j=0; j < BOARD_COLUMNS; j++){
if (pos->board[i][j] == WHITE ||pos->board[i][j] == BLACK )
h = h ^ zobrist_table[i*BOARD_COLUMNS + j][(unsigned)pos->board[i][j]];
}
}
return h;
}
unsigned int hahsCode(unsigned long zobrist){
return zobrist % TABLE_SIZE;
}
void saveExact(Position* aPos, int upperBound, int lowerBound, char depth){
unsigned long key = zobrist_hash(aPos);
unsigned int hash = hahsCode(key);
int i=0;
//if it is not empty
while ((pos_transp_table[hash].type & 0x1)&&(pos_transp_table[hash].zobrist_key != key)&&(i<OPEN_ADDRESSING)){
i++;
hash++;
//wrap around the table
hash = hash % TABLE_SIZE;
}
if(((pos_transp_table[hash].type = 0x7) && (pos_transp_table[hash].upperDepth + pos_transp_table[hash].lowerDepth>=2*depth ))&&(rand() < RAND_MAX/2))
return;
pos_transp_table[hash].zobrist_key = key;
pos_transp_table[hash].upperBound = upperBound;
pos_transp_table[hash].lowerBound = lowerBound;
pos_transp_table[hash].upperDepth = depth;
pos_transp_table[hash].lowerDepth = depth;
pos_transp_table[hash].type = 0x7;
}
void saveUpper(Position* aPos, int upperBound, char depth){
unsigned long key = zobrist_hash(aPos);
unsigned int hash = hahsCode(key);
int i=0;
//if it is not empty
while ((pos_transp_table[hash].type & 0x1)&&(pos_transp_table[hash].zobrist_key != key)&&(i<OPEN_ADDRESSING)){
i++;
hash++;
//wrap around the table
hash = hash % TABLE_SIZE;
}
if((pos_transp_table[hash].type & 0x1)&&(pos_transp_table[hash].zobrist_key == key)) //already the correct position, then just update
{
if(((pos_transp_table[hash].type & 0x4) && (pos_transp_table[hash].upperDepth >= depth))&&(rand() > RAND_MAX/2))
return;
}
pos_transp_table[hash].zobrist_key = key;
pos_transp_table[hash].upperBound = upperBound;
pos_transp_table[hash].upperDepth = depth;
//pos_transp_table[hash].lowerDepth = INT_MIN;
pos_transp_table[hash].type = 0x5;
}
void saveLower(Position* aPos, int lowerBound, char depth){
unsigned long key = zobrist_hash(aPos);
unsigned int hash = hahsCode(key);
int i = 0;
while ((pos_transp_table[hash].type & 0x1)&&(pos_transp_table[hash].zobrist_key != key)&&(i<OPEN_ADDRESSING)){
i++;
hash++;
//wrap around the table
hash = hash % TABLE_SIZE;
}
if((pos_transp_table[hash].type & 0x1)&&(pos_transp_table[hash].zobrist_key == key)) //already the correct position, then just update
{
if((pos_transp_table[hash].type & 0x2) && (pos_transp_table[hash].lowerDepth >= depth)&&(rand() > RAND_MAX/2))
return;
}
pos_transp_table[hash].zobrist_key = key;
pos_transp_table[hash].lowerBound = lowerBound;
pos_transp_table[hash].lowerDepth = depth;
//pos_transp_table[hash].upperDepth = INT_MIN;
pos_transp_table[hash].type = 0x3;
}
PosTransp* retrieve(Position* aPosition){
unsigned long zobrist = zobrist_hash(aPosition);
unsigned int hash = hahsCode(zobrist);
int i =0;
while((pos_transp_table[hash].type & 0x1)&&(i < OPEN_ADDRESSING)){
if((pos_transp_table[hash].zobrist_key == zobrist)){
return &pos_transp_table[hash];
}
i++;
hash++;
//wrap around the table
hash = hash % TABLE_SIZE;
}
return NULL;
}
void freeTable(){
free(pos_transp_table);
}