File size: 879 Bytes
7d23b62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import random
# Checked
class ZobristCache:
    def __init__(self, size):
        self.size = size
        self.zobristTable = self.initializeZobristTable(size)
        self.hash = 0

    def initializeZobristTable(self, size):
        table = []
        for i in range(size):
            table.append([])
            for j in range(size):
                table[i].append({
                    1: random.getrandbits(64),  # black
                    -1: random.getrandbits(64)  # white
                })
        return table

    def togglePiece(self, x, y, role):
        self.hash ^= self.zobristTable[x][y][role]

    def getHash(self):
        return self.hash

if __name__ == '__main__':
    # Example usage
    size = 8
    cache = ZobristCache(size)
    x = 3
    y = 4
    role = 1
    cache.togglePiece(x, y, role)
    hash_value = cache.getHash()
    print(hash_value)