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

@ -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) {
}