-
Notifications
You must be signed in to change notification settings - Fork 1
/
transpositiontable.py
64 lines (55 loc) · 1.85 KB
/
transpositiontable.py
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
from UserDict import UserDict
class TranspositionTable (UserDict):
def __init__ (self, maxSize):
UserDict.__init__(self)
assert maxSize > 0
self.maxSize = maxSize
self.krono = []
self.maxdepth = 0
self.killer1 = [-1]*20
self.killer2 = [-1]*20
self.hashmove = [-1]*20
def __setitem__ (self, key, item):
if not key in self:
if len(self) >= self.maxSize:
try:
del self[self.krono[0]]
except KeyError:
pass # Overwritten
del self.krono[0]
self.data[key] = item
self.krono.append(key)
def probe (self, hash, depth, alpha, beta):
if not hash in self:
return
move, score, hashf, ply = self[hash]
if ply < depth:
return
if hashf == hashfEXACT:
return move, score, hashf
if hashf == hashfALPHA and score <= alpha:
return move, alpha, hashf
if hashf == hashfBETA and score >= beta:
return move, beta, hashf
def record (self, hash, move, score, hashf, ply):
self[hash] = (move, score, hashf, ply)
def add_killer (self, ply, move):
if self.killer1[ply] == -1:
self.killer1[ply] = move
elif move != self.killer1[ply]:
self.killer2[ply] = move
def is_killer (self, ply, move):
if self.killer1[ply] == move:
return 10
elif self.killer2[ply] == move:
return 8
if ply >= 2:
if self.killer1[ply-2] == move:
return 6
elif self.killer2[ply-2] == move:
return 4
return 0
def set_hash_move (self, ply, move):
self.hashmove[ply] = move
def is_hash_move (self, ply, move):
return self.hashmove[ply] == move