zobristTableGet

This commit is contained in:
2024-09-22 14:52:06 +02:00
parent db958dcac1
commit 6c5ef0586f
4 changed files with 79 additions and 3 deletions

View File

@ -54,6 +54,7 @@ struct castle_t {
struct gameState_t {
uint_least64_t *board;
uint_least64_t zobrist;
uint_least8_t *repetitionTable;
bool color; // color to move
struct castle_t canCastle[2];

View File

@ -1,6 +1,23 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <chess/types.h>
struct zobristTableElement {
uint_least64_t hash;
int_least16_t value;
};
struct zobristTable {
struct zobristTableElement *arr;
size_t length;
};
struct zobristTableResult {
bool success;
int_least16_t value;
};
uint_least64_t rand_64();
uint_least64_t zobristPieceI(struct piece_t piece, uint_least8_t field);
uint_least8_t getFile(const uint_least8_t field);

View File

@ -9,16 +9,16 @@
#define INT_16_SAFE_MAX (INT_LEAST16_MAX - 1)
#ifdef __GNUC__ // Check if using GCC or compatible compiler
static int_least16_t countOnes(uint_least64_t num) {
static int_least16_t countOnes(const uint_least64_t num) {
return __builtin_popcountll(num); // Use GCC built-in function if available
}
#elif defined(_MSC_VER) // Check if using Microsoft Visual C++
#include <intrin.h>
static int_least16_t countOnes(uint_least64_t num) {
static int_least16_t countOnes(const uint_least64_t num) {
return __popcnt64(num); // Use MSVC intrinsic if available
}
#else
static int_least16_t countOnes(uint_least64_t num) {
static int_least16_t countOnes(const uint_least64_t num) {
int_least16_t count = 0;
while(num) {
count += num & 1;
@ -29,6 +29,7 @@ static int_least16_t countOnes(uint_least64_t num) {
#endif
static int_least16_t evaluate(const struct gameState_t gameState) {
if(gameState.halfMoveCounter == 50) return 0;
int_least16_t value = 0;
const uint_least64_t *board = gameState.board;
int_least16_t pieceValues[PIECES_LENGTH];

View File

@ -1,4 +1,5 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <chess/util.h>
@ -20,3 +21,59 @@ uint_least8_t getFile(const uint_least8_t field) {
uint_least8_t getRank(const uint_least8_t field) {
return field / BOARD_SIZE;
}
void initZobirstTable(struct zobristTableElement *table, size_t length) {
for(size_t i = 0; i < length; ++i) {
table[i].hash = 0; // use 0 for no value
}
}
static size_t zobristTableInitialI(const struct zobristTable table, const uint_least64_t key) {
return key % table.length;
}
enum returnStatus {
CONTINUE,
RETURN,
};
static void zobristTableIter(const struct zobristTable table, const uint_least64_t key, void *result,
enum returnStatus (*foreach)(const struct zobristTableElement element, const uint_least64_t key, void *result)) {
const size_t initI = zobristTableInitialI(table, key);
size_t i = initI;
do {
const struct zobristTableElement element = table.arr[i];
if(foreach(element, key, result) == RETURN) return;
++i;
if(i >= table.length) i = 0;
} while(i != initI);
fprintf(stderr, "Error: zobrist table is full");
exit(1);
}
static enum returnStatus zobristTableGetForeach(const struct zobristTableElement element, const uint_least64_t key,
void *resultVoid) {
struct zobristTableResult *result = resultVoid;
if(element.hash == key) {
*result = (struct zobristTableResult){true, element.value};
return RETURN;
}
if(element.hash == 0) {
result->success = false;
return RETURN;
}
return CONTINUE;
}
struct zobristTableResult zobristTableGet(const struct zobristTable table, const uint_least64_t key) {
struct zobristTableResult result;
zobristTableIter(table, key, &result, zobristTableGetForeach);
return result;
}
static enum returnStatus zobristTableSetForeach(const struct zobristTableElement element, const uint_least64_t key,
void *resultVoid) {
}
void zobristTableSet(struct zobristTable table, const uint_least64_t key, const int_least16_t value) {
}