forked from JayfonLin/chessserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransposition_table.cpp
149 lines (116 loc) · 3.29 KB
/
transposition_table.cpp
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
/*
Created on 2015-09-05
@author:jeff
*/
#include "transposition_table.h"
#include "chess_util.h"
#include <cstdlib>
#include <ctime>
LONGLONG Rand64(){
return rand() ^ ((LONGLONG)rand() << 15) ^ ((LONGLONG)rand() << 30) ^
((LONGLONG)rand() << 45) ^ ((LONGLONG)rand() << 60);
}
long Rand32(){
return rand() ^ ((long)rand() << 15) ^ ((long)rand() << 30);
}
CTranspositionTable::CTranspositionTable(){
InitializeHashKey();
}
CTranspositionTable::~CTranspositionTable(){
delete m_ptt[0];
delete m_ptt[1];
}
void CTranspositionTable::InitializeHashKey(){
int i,j;
srand((unsigned)time(NULL));
for (i = 0; i < PIECE_COUNT; ++i)
for (j = 0; j < BOARD_NUMBER; ++j){
if (!CChessUtil::InBoard(j))
continue;
m_hash_key_32[i][j] = Rand32();
m_hash_key_64[i][j] = Rand64();
}
m_ptt[0] = new HashItem[HASH_SIZE];
m_ptt[1] = new HashItem[HASH_SIZE];
}
void CTranspositionTable::CalculateInitHashKey(int cur_position[]){
int j, chess_type;
m_hash_32 = 0;
m_hash_64 = 0;
for (j = 0; j < BOARD_NUMBER; ++j){
if (!CChessUtil::InBoard(j)){
continue;
}
chess_type = cur_position[j];
if (chess_type != 0)
{
m_hash_32 = m_hash_32 ^ m_hash_key_32[chess_type][j];
m_hash_64 = m_hash_64 ^ m_hash_key_64[chess_type][j];
}
}
}
void CTranspositionTable::HashMakeMove(CHESS_MOVE move, int cur_position[]){
int to_id, from_id;
int sq_src = CChessUtil::Src(move.m_move);
int sq_dst = CChessUtil::Dst(move.m_move);
from_id = cur_position[sq_src];
to_id = cur_position[sq_dst];
m_hash_32 = m_hash_32 ^ m_hash_key_32[from_id][sq_src];
m_hash_64 = m_hash_64 ^ m_hash_key_64[from_id][sq_src];
if (to_id != 0){
m_hash_32 = m_hash_32 ^ m_hash_key_32[to_id][sq_dst];
m_hash_64 = m_hash_64 ^ m_hash_key_64[to_id][sq_dst];
}
m_hash_32 = m_hash_32 ^ m_hash_key_32[from_id][sq_dst];
m_hash_64 = m_hash_64 ^ m_hash_key_64[from_id][sq_dst];
}
void CTranspositionTable::HashUnmakeMove(CHESS_MOVE move, int chess_id, int cur_position[]){
int to_id;
int sq_src = CChessUtil::Src(move.m_move);
int sq_dst = CChessUtil::Dst(move.m_move);
to_id = cur_position[sq_dst];
m_hash_32 = m_hash_32 ^ m_hash_key_32[to_id][sq_src];
m_hash_64 = m_hash_64 ^ m_hash_key_64[to_id][sq_src];
m_hash_32 = m_hash_32 ^ m_hash_key_32[to_id][sq_dst];
m_hash_64 = m_hash_64 ^ m_hash_key_64[to_id][sq_dst];
if (chess_id != 0){
m_hash_32 = m_hash_32 ^ m_hash_key_32[chess_id][sq_dst];
m_hash_64 = m_hash_64 ^ m_hash_key_64[chess_id][sq_dst];
}
}
int CTranspositionTable::LookupHashTable(int alpha, int beta, int depth, int table_no){
int x;
HashItem *pht;
x = m_hash_32 & 0xFFFFF;
pht = &m_ptt[table_no][x];
/*if (pht == null){
return Constant.INVALID_SCORE;
}*/
if (pht->depth >= depth && pht->checksum == m_hash_64){
switch (pht->entry_type) {
case EXACT:
return pht->eval;
case LOWER_BOUND:
if (pht->eval >= beta)
return (pht->eval);
else
break;
case UPPER_BOUND:
if (pht->eval <= alpha)
return (pht->eval);
else
break;
}
}
return INVALID_SCORE;
}
void CTranspositionTable::EnterHashTable(ENTRY_TYPE entry_type, short eval, short depth, int table_no){
int x;
HashItem *pht;
x = m_hash_32 & 0xFFFFF;
pht = &m_ptt[table_no][x];
pht->checksum = m_hash_64;
pht->entry_type = entry_type;
pht->eval = eval;
pht->depth = depth;
}