0

So I'm trying to implement zobrist hashing into a school project I am working on. However, when initializing the table of random numbers, the output from printing the zTable has identical entries (126 and 127 which are the last numbers created). I know that for zobrist hashing, I should use large 64 bit numbers to represent each piece and its position. However in my game, there are only two pieces, so my thinking was as long as the numbers are unique, it shouldn't matter.

After a few rounds of debugging, I was able to figure out that the correct numbers are being assigned as the program loops, but once the program exits, the numbers are different when I call print(zTable). Any advice? Thanks

zTable = [[[None] * 2] * 8] * 8
currNumber = 0


# Initializes the zHashTable for this board
def initTable(self):
    for row in range(8):
        for col in range(8):
            for i in range(2):
                self.zTable[row][col][i] = self.currNumber
                self.currNumber += 1
5teeze
  • 3
  • 2

1 Answers1

0

Following code should work. Notice the IDs are different for each sublist

# Initializes the zHashTable for this board
def initTable():
    zTable = [[[None] * 2 for _ in range(8)] for _ in range(8)]
    currNumber = 0

    for row in range(8):
        for col in range(8):
            for i in range(2):
                zTable[row][col][i] = currNumber
                currNumber += 1

    for subList in zTable:
        print(id(subList))

    print(zTable)


initTable();

Output

140453289927112
140453289930376
140453289934920
140453289935560
140453289936200
140453289936840
140453289937544
140453289938184
[[[0, 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]]]

In your code (simplified a bit), notice the IDs are the same for each sublist

# Initializes the zHashTable for this board
def initTable():
    zTable = [[[None] * 2] * 8] * 8
    currNumber = 0

    for row in range(8):
        for col in range(8):
            for i in range(2):
                zTable[row][col][i] = currNumber
                currNumber += 1

    for subList in zTable:
        print(id(subList))

    print(zTable)


initTable();

Output

140702461087048
140702461087048
140702461087048
140702461087048
140702461087048
140702461087048
140702461087048
140702461087048
[[[126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127]], [[126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127]], [[126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127]], [[126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127]], [[126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127]], [[126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127]], [[126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127]], [[126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127], [126, 127]]]

With zTable = [[[None] * 2] * 8] * 8, you are refering to same list in each copy.

Whereas zTable = [[[None] * 2 for _ in range(8)] for _ in range(8)], creates deep copy each time.

Krishna
  • 924
  • 1
  • 7
  • 28