📌  相关文章
📜  博弈论中的极小极大算法组合5(“杂技演员哈希”)

📅  最后修改于: 2021-05-04 19:22:11             🧑  作者: Mango

有关该主题的先前文章:博弈论中的Minimax算法,博弈论中的评估函数,井字游戏AI –找到最佳移动,Alpha-Beta修剪。

Zobrist Hashing是一种哈希函数,广泛用于2个棋盘游戏中。它是换位表中最常用的哈希函数。换位表基本上存储了先前板状态的评估值,因此,如果再次遇到它们,我们只需从换位表中检索存储的值即可。我们将在以后的文章中介绍换位表。在本文中,我们将以国际象棋棋盘为例,并为此实现一个哈希函数。

伪代码:

// A matrix with random numbers initialized once
Table[#ofBoardCells][#ofPieces] 

// Returns Zobrist hash function for current conf-
// iguration of board.
function findhash(board):
    hash = 0
    for each cell on the board :
        if cell is not empty :
            piece = board[cell]
            hash ^= table[cell][piece]
    return hash

解释 :

Zobrist Hashing背后的想法是,对于给定的板状态,如果给定单元格上有一块,我们将使用表中相应单元格中该块的随机数。

如果随机数中有更多位,则发生哈希冲突的机会较小。因此,通常使用64位数字作为标准,并且在如此大的数字下不太可能发生哈希冲突。在程序执行期间,该表仅需初始化一次。

同样,在棋盘游戏中广泛使用Zobrist Hashing的原因是,当玩家移动时,无需从头开始重新计算哈希值。由于XOR运算的性质,我们可以简单地使用少量XOR运算来重新计算哈希值。

执行 :

我们将尝试查找给定板配置的哈希值。

// A program to illustrate Zobeist Hashing Algorithm
#include 
using namespace std;
  
unsigned long long int ZobristTable[8][8][12];
mt19937 mt(01234567);
  
// Generates a Randome number from 0 to 2^64-1
unsigned long long int randomInt()
{
    uniform_int_distribution
                                 dist(0, UINT64_MAX);
    return dist(mt);
}
  
// This function associates each piece with
// a number
int indexOf(char piece)
{
    if (piece=='P')
        return 0;
    if (piece=='N')
        return 1;
    if (piece=='B')
        return 2;
    if (piece=='R')
        return 3;
    if (piece=='Q')
        return 4;
    if (piece=='K')
        return 5;
    if (piece=='p')
        return 6;
    if (piece=='n')
        return 7;
    if (piece=='b')
        return 8;
    if (piece=='r')
        return 9;
    if (piece=='q')
        return 10;
    if (piece=='k')
        return 11;
    else
        return -1;
}
  
// Initializes the table
void initTable()
{
    for (int i = 0; i<8; i++)
      for (int j = 0; j<8; j++)
        for (int k = 0; k<12; k++)
          ZobristTable[i][j][k] = randomInt();
}
  
// Computes the hash value of a given board
unsigned long long int computeHash(char board[8][9])
{
    unsigned long long int h = 0;
    for (int i = 0; i<8; i++)
    {
        for (int j = 0; j<8; j++)
        {
            if (board[i][j]!='-')
            {
                int piece = indexOf(board[i][j]);
                h ^= ZobristTable[i][j][piece];
            }
        }
    }
    return h;
}
  
// Main Function
int main()
{
    // Uppercase letters are white pieces
    // Lowercase letters are black pieces
    char board[8][9] =
    {
        "---K----",
        "-R----Q-",
        "--------",
        "-P----p-",
        "-----p--",
        "--------",
        "p---b--q",
        "----n--k"
    };
  
    initTable();
  
    unsigned long long int hashValue = computeHash(board);
    printf("The hash value is     : %llu\n", hashValue);
  
    //Move the white king to the left
    char piece = board[0][3];
  
    board[0][3] = '-';
    hashValue ^= ZobristTable[0][3][indexOf(piece)];
  
    board[0][2] = piece;
    hashValue ^= ZobristTable[0][2][indexOf(piece)];
  
  
    printf("The new hash value is : %llu\n", hashValue);
  
    // Undo the white king move
    piece = board[0][2];
  
    board[0][2] = '-';
    hashValue ^= ZobristTable[0][2][indexOf(piece)];
  
    board[0][3] = piece;
    hashValue ^= ZobristTable[0][3][indexOf(piece)];
  
    printf("The old hash value is : %llu\n", hashValue);
  
    return 0;
}

输出 :

The hash value is     : 14226429382419125366
The new hash value is : 15124945578233295113
The old hash value is : 14226429382419125366