forked from Harshita-Kanal/Data-Structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathZorbishtHashing.cpp
More file actions
112 lines (94 loc) · 1.83 KB
/
ZorbishtHashing.cpp
File metadata and controls
112 lines (94 loc) · 1.83 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
#include <bits/stdc++.h>
using namespace std;
unsigned long long int ZobristTable[8][8][12];
mt19937 mt(01234567);
unsigned long long int randomInt()
{
uniform_int_distribution<unsigned long long int>
dist(0, UINT64_MAX);
return dist(mt);
}
int indexOf(char piece)
{
if (piece=='P')
return 0;
if (piece=='N')
return 1;
if (piece=='B')
return 2;
if (piece=='R')
return 3;
if (piece=='Q')
return 4;
if (piece=='K')
return 5;
if (piece=='p')
return 6;
if (piece=='n')
return 7;
if (piece=='b')
return 8;
if (piece=='r')
return 9;
if (piece=='q')
return 10;
if (piece=='k')
return 11;
else
return -1;
}
void initTable()
{
for (int i = 0; i<8; i++)
for (int j = 0; j<8; j++)
for (int k = 0; k<12; k++)
ZobristTable[i][j][k] = randomInt();
}
unsigned long long int computeHash(char board[8][9])
{
unsigned long long int h = 0;
for (int i = 0; i<8; i++)
{
for (int j = 0; j<8; j++)
{
if (board[i][j]!='-')
{
int piece = indexOf(board[i][j]);
h ^= ZobristTable[i][j][piece];
}
}
}
return h;
}
int main()
{
char board[8][9] =
{
"---K----",
"-R----Q-",
"--------",
"-P----p-",
"-----p--",
"--------",
"p---b--q",
"----n--k"
};
initTable();
unsigned long long int hashValue = computeHash(board);
printf("The hash value is : %llu\n", hashValue);
//Move the white king to the left
char piece = board[0][3];
board[0][3] = '-';
hashValue ^= ZobristTable[0][3][indexOf(piece)];
board[0][2] = piece;
hashValue ^= ZobristTable[0][2][indexOf(piece)];
printf("The new hash value is : %llu\n", hashValue);
// Undo the white king move
piece = board[0][2];
board[0][2] = '-';
hashValue ^= ZobristTable[0][2][indexOf(piece)];
board[0][3] = piece;
hashValue ^= ZobristTable[0][3][indexOf(piece)];
printf("The old hash value is : %llu\n", hashValue);
return 0;
}