#include "chess/types.h" #include #include #include #include uint_least64_t rand_64() { uint_least64_t u1, u2, u3, u4; u1 = (uint_least64_t)(rand()) & 0xFFFF; u2 = (uint_least64_t)(rand()) & 0xFFFF; u3 = (uint_least64_t)(rand()) & 0xFFFF; u4 = (uint_least64_t)(rand()) & 0xFFFF; return u1 | (u2 << 16) | (u3 << 32) | (u4 << 48); } uint_least64_t zobristPieceI(struct piece_t piece, uint_least8_t field) { return field * 2 * PIECES_LENGTH + piece.color * PIECES_LENGTH + piece.type; } uint_least8_t getFile(const uint_least8_t field) { return field % BOARD_SIZE; } uint_least8_t getRank(const uint_least8_t field) { return field / BOARD_SIZE; } struct zobristTable initZobirstTable(struct zobristTableElement *table, size_t length) { for(size_t i = 0; i < length; ++i) { table[i].hash = 0; } return (struct zobristTable){table, length}; } static size_t zobristTableInitialI(const struct zobristTable table, const uint_least64_t key) { return key % table.length; } void zobristTableIter(const struct zobristTable table, const uint_least64_t key, void *result, void (*callback)(struct zobristTableElement *element, const uint_least64_t key, void *result)) { const size_t initI = zobristTableInitialI(table, key); size_t i = initI; do { struct zobristTableElement *element = table.arr + i; if(element->hash == 0 || element->hash == key) { callback(element, key, result); } ++i; if(i >= table.length) i = 0; } while(i != initI); fprintf(stderr, "Error: zobrist table is full"); exit(1); } static void zobristTableItemPtrCallback(struct zobristTableElement *element, const uint_least64_t key, void *resultVoid) { struct zobristTableElement **result = resultVoid; if(element->hash == key) { *result = element; return; } // if(hash == 0) *result = NULL; } static struct zobristTableElement* zobristTableItemPtr(const struct zobristTable table, const uint_least64_t key) { struct zobristTableElement *result; zobristTableIter(table, key, &result, zobristTableItemPtrCallback); return result; } void zobristTableDelete(struct zobristTable table, const uint_least64_t key) { struct zobristTableElement *element = zobristTableItemPtr(table, key); if(element != NULL) element->hash = 0; } int_least16_t* zobristTableGetPtr(const struct zobristTable table, const uint_least64_t key) { struct zobristTableElement *element = zobristTableItemPtr(table, key); if(element == NULL) return NULL; return &element->value; }