From 4d2a86c7b7bf79cffd5469668bd580e14784d184 Mon Sep 17 00:00:00 2001 From: MrGeorgen Date: Wed, 18 Sep 2024 22:39:16 +0200 Subject: [PATCH] generate zobrist masks --- include/chess/rand.h | 3 +++ src/common/rand.c | 10 ++++++++++ src/findMagicNumber.c | 8 +------- src/generateCode/zobrist.c | 30 ++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 include/chess/rand.h create mode 100644 src/common/rand.c create mode 100644 src/generateCode/zobrist.c diff --git a/include/chess/rand.h b/include/chess/rand.h new file mode 100644 index 0000000..c786646 --- /dev/null +++ b/include/chess/rand.h @@ -0,0 +1,3 @@ +#include + +uint_least64_t rand_64(); diff --git a/src/common/rand.c b/src/common/rand.c new file mode 100644 index 0000000..6f30c43 --- /dev/null +++ b/src/common/rand.c @@ -0,0 +1,10 @@ +#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); +} diff --git a/src/findMagicNumber.c b/src/findMagicNumber.c index 5fe9bf5..7cda657 100644 --- a/src/findMagicNumber.c +++ b/src/findMagicNumber.c @@ -9,18 +9,12 @@ #include #include #include +#include #define MAX_BITS 12 #define MAX_SIZE (1 << MAX_BITS) #define ATTACK_TABLE_LENGTH (2 * MAX_SIZE * TOTAL_BOARD_SIZE) -static 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); -} - static uint_least64_t randFewBits() { return rand_64() & rand_64() & rand_64(); } diff --git a/src/generateCode/zobrist.c b/src/generateCode/zobrist.c new file mode 100644 index 0000000..a7ac819 --- /dev/null +++ b/src/generateCode/zobrist.c @@ -0,0 +1,30 @@ +#include "chess/types.h" +#include +#include +#include +#include +#include +#include + +static uint_least64_t zobristPieceI(struct piece_t piece, uint_least8_t field) { + return field * 2 * PIECES_LENGTH + piece.color * PIECES_LENGTH + piece.type; +} + +int main() { + uint_least64_t zobristPiece[TOTAL_BOARD_SIZE * 2 * PIECES_LENGTH]; + srand(0xc0229b8e); + printf("#ifndef CHESS_GENERATED_ZOBRIST_H\n" + "#define CHESS_GENERATED_ZOBRIST_H\n" + "#include \n" + ); + for(uint_least8_t color = BLACK; color <= WHITE; ++color) { + for(uint_least8_t pieceType = KING; pieceType < PIECES_LENGTH; ++pieceType) { + const struct piece_t piece = {pieceType, color}; + for(uint_least8_t field = 0; field < TOTAL_BOARD_SIZE; ++field) { + zobristPiece[zobristPieceI(piece, field)] = rand_64(); + } + } + } + defineArray("#define defineZobristPiece const static uint_least64_t ZOBRIST_PIECE", printerull, zobristPiece); + printf("#define defineZobristBlackMove const uint_least64_t ZOBRIST_BLACK_MOVE = %" PRIuLEAST64 "\n", rand_64()); +}