[英]How to make custom hash function for hashing matrix (Othello board) to number
I have to do project for which I need custom function for hashing matrix.我必须做一个项目,我需要自定义 function 用于散列矩阵。 Project is about Othello (Reversi) game which means that I need to hash fixed 8x8 matrix.项目是关于黑白棋(黑白棋)游戏,这意味着我需要 hash 固定 8x8 矩阵。
This is how initializing matrix looks like:这是初始化矩阵的样子:
board = [['.' for x in range(8)] for y in range(8)]
Here is one example of how board looks:以下是电路板外观的一个示例:
[
['.', '.', '.', '.', '.', '.', '.', '.'],
['.', '2', '1', '.', '.', '.', '.', '.'],
['.', '.', '2', '.', '.', '.', '.', '.'],
['.', '.', '1', '2', '1', '.', '.', '.'],
['.', '.', '.', '1', '2', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.']
]
As you can see, one player is 1 (which is always me) and the second player is 2 (which is always computer) and .
如您所见,一个玩家是 1(始终是我),第二个玩家是 2(始终是计算机)和.
are empty board places.是空板的地方。
I made some kind of hashing function.我做了某种散列 function。 It looks like this:它看起来像这样:
def hash(self, board):
string = ''
for y in range(8):
for x in range(8):
string += board[y][x]
broj = 0
for index, znak in enumerate(string):
broj += (index + 1) * ord(znak)
return broj
Function accepts board (matrix) and first makes string that contains all board fields in exact order and state as it is in board. Function 接受板(矩阵)并首先创建包含所有板字段的字符串,其中包含确切顺序的所有板字段和 state,因为它在板上。 After that I hash that string using formula from for
loop.之后,我使用for
循环中的公式 hash 该字符串。 Function ord
returns ASCII value of character. Function ord
返回字符的 ASCII 值。
I am aware that this is not good hash function so I am interested to hear some ideas for improving this one or implementing some completely different.我知道这不太好I saw idea which is based on representing board with two 64-bit binary numbers where first number contains ones on places where player 1 has pieces and zeros on all other places, and second number contains ones where player 2 has pieces and zeros on all other places.我看到了基于用两个 64 位二进制数表示棋盘的想法,其中第一个数字包含玩家 1 在所有其他地方都有棋子和零的地方,第二个数字包含玩家 2 在所有其他地方都有棋子和零的地方地方。 After that, as I remember, I have to do hashing for that two numbers using some kind of algorithm.在那之后,我记得,我必须使用某种算法对这两个数字进行哈希处理。 Thing is, I don't know if that is good hash function and if I can implement it whatsoever.问题是,我不知道这是否好 hash function 以及我是否可以实现它。
Important to note is that I can't use built-in hash functions or any other function imported from some library.需要注意的重要一点是,我不能使用从某个库导入的内置 hash 函数或任何其他 function 函数。 I have to make custom hash function with some kind of algorithm.我必须用某种算法定制 hash function 。
Thanks in advance.提前致谢。
As I indicated in a response to my original (bogus) comment, you could consider each board state to be a 64 digit base-3 number.正如我在回复我的原始(虚假)评论中指出的那样,您可以将每个板 state 视为 64 位基数 3 数。 This approach will result in a unique integer value for every possible configuration — which can be considered its "hash" value.这种方法将为每个可能的配置产生一个唯一的 integer 值——可以被认为是它的“散列”值。
This is what I mean:这就是我的意思:
def hash_func(board):
numstr = (''.join(''.join(row) for row in board)).replace('.', '0')
return int(numstr, 3) # Convert to an integer.
sample = [['.', '.', '.', '.', '.', '.', '.', '.'],
['.', '2', '1', '.', '.', '.', '.', '.'],
['.', '.', '2', '.', '.', '.', '.', '.'],
['.', '.', '1', '2', '1', '.', '.', '.'],
['.', '.', '.', '1', '2', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.']]
print(f'{hash_func(sample):,}') # -> 135,688,629,099,716,090,516,394,594
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.