forked from tylerxprice/hive-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzobrist.py
38 lines (25 loc) · 1.24 KB
/
zobrist.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
import random
from pieces import Point
class Zobrist:
"""
This is a modification of Zobrist keys: they are lazily generated into a dictionary
since the number of different hexes a piece can land throughout the game on is indeterminate.
"""
def __init__(self, numberOfPieces):
self.zobristKeys = [[dict() for y in range(numberOfPieces)] for x in range(2)]
self.sideKey = self._generateRandomNumber()
self.currentState = 0L
def _getDictKey(self, point = Point(None, None, None)):
return str(point.x) + ',' + str(point.y) + ',' + str(point.z)
def _getZobristKey(self, colorIndex, kindIndex, point = Point(None, None, None)):
dictKey = self._getDictKey(point)
if not self.zobristKeys[colorIndex][kindIndex].has_key(dictKey):
self.zobristKeys[colorIndex][kindIndex][dictKey] = self._generateRandomNumber()
return self.zobristKeys[colorIndex][kindIndex][dictKey]
def _generateRandomNumber(self): # is this sufficient?
return random.randint(1, 2**32)
def changeSide(self):
self.currentState = self.currentState ^ self.sideKey
def updateState(self, piece):
key = self._getZobristKey(piece.getColorIndex(), piece.getKindIndex(), piece.point)
self.currentState = self.currentState ^ key