⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hash.cpp

📁 超强国际象棋引擎
💻 CPP
字号:
// hash.cpp

// includes

#include "hash.h"
#include "random.h"

// variables

uint64 castle_64[16];

// prototypes

static uint64 hash_counter_key(int piece_12, int count);

// functions

// hash_init()

void hash_init()
    {

    int i;

    for ( i = 0; i < 16; i++ )
        castle_64[i] = hash_castle_key(i);
    }

// hash_key()

uint64 hash_key(const board_t *board)
    {

    uint64 key;
    int color;
    const sq_t *ptr;
    int sq, piece;

    // init

    key = 0;

    // pieces

    for ( color = 0; color < 2; color++ )
        {
        for ( ptr = &board->piece[color][0]; (sq = *ptr) != 0; ptr++ )
            {
            piece = board->square[sq];
            key ^= hash_piece_key(piece, sq);
            }

        for ( ptr = &board->pawn[color][0]; (sq = *ptr) != 0; ptr++ )
            {
            piece = board->square[sq];
            key ^= hash_piece_key(piece, sq);
            }
        }

    // castle flags

    key ^= hash_castle_key(board->flags);

    // en-passant square

    sq = board->ep_square;

    if(sq != 0)
        key ^= hash_ep_key(sq);

    // turn

    key ^= hash_turn_key(board->turn);

    return key;
    }

// hash_pawn_key()

uint64 hash_pawn_key(const board_t *board)
    {

    uint64 key;
    int color;
    const sq_t *ptr;
    int sq, piece;

    // init

    key = 0;

    // pawns

    for ( color = 0; color < 2; color++ )
        {
        for ( ptr = &board->pawn[color][0]; (sq = *ptr) != 0; ptr++ )
            {
            piece = board->square[sq];
            key ^= hash_piece_key(piece, sq);
            }
        }

    return key;
    }

// hash_material_key()

uint64 hash_material_key(const board_t *board)
    {

    uint64 key;
    int piece_12, count;

    // init

    key = 0;

    // counters

    for ( piece_12 = 0; piece_12 < 12; piece_12++ )
        {
        count = board->number[piece_12];
        key ^= hash_counter_key(piece_12, count);
        }

    return key;
    }

// hash_piece_key()

uint64 hash_piece_key(int piece, int square)
    {
    return RANDOM_64(0+(PIECE_TO_12(piece)^1)*64+SQUARE_TO_64(square));
    }

// hash_castle_key()

uint64 hash_castle_key(int flags)
    {

    uint64 key;
    int i;

    key = 0;

    for ( i = 0; i < 4; i++ )
        {
        if((flags &(1 << i)) != 0)
            key ^= RANDOM_64(768 + i);
        }

    return key;
    }

// hash_ep_key()

uint64 hash_ep_key(int square)
    {

    return RANDOM_64(772 + SQUARE_FILE(square) - (0x4));
    }

// hash_turn_key()

uint64 hash_turn_key(int color)
    {

    return (COLOUR_IS_WHITE(color)) ? RANDOM_64(780) : 0;
    }

// hash_counter_key()

static uint64 hash_counter_key(int piece_12, int count)
    {

    uint64 key;
    int i, index;

    // init

    key = 0;

    // counter

    index = piece_12 * 16;

    for ( i = 0; i < count; i++ )
        key ^= RANDOM_64(index + i);

    return key;
    }

// end of hash.cpp

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -