Just do something like
typedef unsigned long long uint_64;
this will depend on what compiler your using.
This code is sort of untested but I wanted to see how
zobrist hashing works.
Code:
#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;
typedef unsigned long long int uint_64;
const int HASH_TABLE_SIZE = 100003;
const int NUM_ROW = 8;
const int NUM_COL = 6;
const int NUM_BOARD = 2000;
struct Hash_node {
uint_64 key;
bool empty;
int row;
int col;
};
Hash_node hash_table[HASH_TABLE_SIZE];
uint_64 rand64(void)
{
return rand() ^ ((uint_64) rand() << 15) ^ ((uint_64)rand() << 30)
^ ((uint_64) rand() << 45) ^ ((uint_64)rand() << 60);
}
void load_board(int board[NUM_BOARD][NUM_ROW][NUM_COL])
{
for (int i = 0; i < NUM_BOARD; ++i) {
for (int j = 0; j < NUM_ROW; ++j) {
for (int k = 0; k < NUM_COL; ++k) {
board[i][j][k] = rand() % 3;
}
}
}
}
void print_board(int board[NUM_ROW][NUM_COL])
{
for (int i = 0; i < NUM_ROW; ++i) {
for (int j = 0; j < NUM_COL; ++j) {
cout << board[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
uint_64 hash_board(int board[NUM_ROW][NUM_COL],
uint_64 zobrist_key[3][NUM_ROW][NUM_COL])
{
uint_64 h = 0;
for (int i = 0; i < NUM_ROW; ++i) {
for (int j = 0; j < NUM_COL; ++j) {
h ^= zobrist_key[board[i][j]][i][j];
}
}
return h;
}
void fill_keys(uint_64 zobrist_key[3][NUM_ROW][NUM_COL])
{
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < NUM_ROW; ++j) {
for (int k = 0; k < NUM_COL; ++k) {
zobrist_key[i][j][k] = rand64();
}
}
}
}
void init_hash_table()
{
for (int i = 0; i < HASH_TABLE_SIZE; ++i) {
hash_table[i].key = 0;
hash_table[i].row = 0;
hash_table[i].col = 0;
hash_table[i].empty = true;
}
}
void hash_insert(int row, int col, uint_64 key)
{
int i = key % HASH_TABLE_SIZE;
int n = 0;
int probes = 0;
while(!hash_table[i].empty &&
hash_table[i].key != key && n < HASH_TABLE_SIZE) {
i++;
if (i == HASH_TABLE_SIZE)
i = 0;
n++;
probes++;
}
if (probes > 0)
cout << "number of probes = " << probes << endl;
if (hash_table[i].empty) {
hash_table[i].empty = false;
hash_table[i].row = row;
hash_table[i].col = col;
hash_table[i].key = key;
}
}
Hash_node hash_search(uint_64 key)
{
int i = key % HASH_TABLE_SIZE;
int n = 0;
while(!hash_table[i].empty && hash_table[i].key != key
&& n < HASH_TABLE_SIZE) {
i++;
if (i == HASH_TABLE_SIZE)
i = 0;
n++;
}
if (!hash_table[i].empty && hash_table[i].key == key) {
return hash_table[i];
}
Hash_node hash_node;
hash_node.empty = true;
hash_node.key = 0;
hash_node.row = 0;
hash_node.col = 0;
return hash_node;
}
int main(int argc, char* argv[])
{
int board[NUM_BOARD][NUM_ROW][NUM_COL];
uint_64 zobrist_key[3][NUM_ROW][NUM_COL];
init_hash_table();
load_board(board);
for (int i = 0; i < NUM_BOARD; ++i) {
print_board(board[i]);
}
fill_keys(zobrist_key);
for (int i = 0; i < NUM_BOARD; ++i) {
hash_insert(4, 5, hash_board(board[i], zobrist_key));
}
int new_board[NUM_ROW][NUM_COL];
for (int i = 0; i < NUM_ROW; ++i) {
for (int j = 0; j < NUM_COL; ++j) {
new_board[i][j] = (i * j) % 3;
}
}
hash_insert(3, 2, hash_board(new_board, zobrist_key));
Hash_node hn = hash_search(hash_board(new_board, zobrist_key));
if (!hn.empty) {
cout << "move = (" << hn.row << ", " << hn.col << ")" << endl;
} else {
cout << "position not in hash_table" << endl;
}
return 0;
}