diff --git a/include/chess/types.h b/include/chess/types.h index 5a39093..48b5ea8 100644 --- a/include/chess/types.h +++ b/include/chess/types.h @@ -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]; diff --git a/include/chess/util.h b/include/chess/util.h index 28dc2cd..eb30d96 100644 --- a/include/chess/util.h +++ b/include/chess/util.h @@ -1,6 +1,23 @@ +#include +#include #include #include +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); diff --git a/src/chess/evaluate.c b/src/chess/evaluate.c index 817d253..9b529cc 100644 --- a/src/chess/evaluate.c +++ b/src/chess/evaluate.c @@ -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 -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]; diff --git a/src/common/preCodeGen/util.c b/src/common/preCodeGen/util.c index 111ac50..4c31972 100644 --- a/src/common/preCodeGen/util.c +++ b/src/common/preCodeGen/util.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -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) { +}