zobrist hashing

This commit is contained in:
2024-09-22 11:45:30 +02:00
parent 4d2a86c7b7
commit db958dcac1
21 changed files with 273 additions and 176 deletions

View File

@ -2,9 +2,6 @@
#include <stdint.h>
#include <chess/types.h>
bool bitsetGet(uint_least64_t bitset, uint_least8_t i);
uint_least64_t bitsetClear(uint_least64_t bitset, uint_least8_t i);
uint_least64_t bitsetSet(uint_least64_t bitset, uint_least8_t i);
bool bitboardGet(const uint_least64_t *board, struct piece_t piece, uint_least8_t i);
uint_least64_t bitboardGetMask(const uint_least64_t *board, struct piece_t piece);
void bitboardSetMask(uint_least64_t *board, struct piece_t piece, uint_least64_t value);
@ -14,5 +11,3 @@ void bitboardClear(uint_least64_t *board, struct piece_t piece, uint_least8_t i)
void bitboardSet(uint_least64_t *board, struct piece_t piece, uint_least8_t i);
struct piece_t pieceAtField(const uint_least64_t *board, uint_least8_t i);
struct gameState_t newGameState(uint_least64_t *board, char *FEN);
uint_least8_t getFile(const uint_least8_t field);
uint_least8_t getRank(const uint_least8_t field);

6
include/chess/bitset.h Normal file
View File

@ -0,0 +1,6 @@
#include <stdint.h>
#include <stdbool.h>
bool bitsetGet(uint_least64_t bitset, uint_least8_t i);
uint_least64_t bitsetClear(uint_least64_t bitset, uint_least8_t i);
uint_least64_t bitsetSet(uint_least64_t bitset, uint_least8_t i);

View File

@ -17,6 +17,7 @@ enum spezialMoves {
struct move_t {
uint_least8_t src, dst, spezialMove;
uint_least8_t srcPiece, dstPiece, capturedPiece;
uint_least64_t hash;
};
void genDirectionConsts();
@ -25,7 +26,7 @@ uint_least8_t pieceValidMoves(struct gameState_t gameState, struct piece_t piece
struct move_t *moves, bool promotion);
void undoMove(uint_least64_t *board, struct move_t move, bool color);
struct gameState_t makeMove(struct gameState_t gameState, struct move_t move);
struct gameState_t makeMove(struct gameState_t gameState, const struct move_t move);
void initMagicTable();
bool kingInCheck(const uint_least64_t *board, const bool color);
uint_least8_t getBaseRankI(bool color);

View File

@ -2,26 +2,29 @@
#include <stdint.h>
#include <chess/move.h>
#define fprintArray(file, printer, arr) \
#define fprintArrayCustomLength(file, printer, arr, length) \
do { \
fprintf(file, "{"); \
for(size_t i = 0; i < LENGTH(arr); ++i) { \
for(size_t i = 0; i < length; ++i) { \
printer(file, arr[i]); \
if (i < LENGTH(arr) - 1) { \
if (i < length - 1) { \
fprintf(file, ", "); \
} \
} \
fprintf(file, " }"); \
} while(0)
#define fprintArray(file, printer, arr) fprintArrayCustomLength(file, printer, arr, LENGTH(arr))
#define fdefineArray(file, declaration, printer, arr) \
#define fdefineArrayCustomLength(file, declaration, printer, arr, length) \
do { \
fprintf(file, "%s[%zu] = ", declaration, LENGTH(arr)); \
fprintArray(file, printer, arr); \
fprintf(file, "%s[%zu] = ", declaration, length); \
fprintArrayCustomLength(file, printer, arr, length); \
fprintf(file, ";\n"); \
} while(0)
#define fdefineArray(file, declaration, printer, arr) fdefineArrayCustomLength(file, declaration, printer, arr, LENGTH(arr))
#define defineArray(declaration, printer, arr) fdefineArray(stdout, declaration, printer, arr)
#define defineArrayCustomLength(declaration, printer, arr, length) fdefineArrayCustomLength(stdout, declaration, printer, arr, length)
#define defineArray(declaration, printer, arr) defineArrayCustomLength(declaration, printer, arr, LENGTH(arr))
void printerll(FILE *file, long long num);
void printerull(FILE *file, unsigned long long num);

View File

@ -1,3 +0,0 @@
#include <stdint.h>
uint_least64_t rand_64();

View File

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

7
include/chess/util.h Normal file
View File

@ -0,0 +1,7 @@
#include <stdint.h>
#include <chess/types.h>
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);
uint_least8_t getRank(const uint_least8_t field);

View File

@ -0,0 +1,16 @@
#ifndef CHESS_ZOBRIST_CONSTS_H
#define CHESS_ZOBRIST_CONSTS_H
#include "chess/types.h"
#include <stdint.h>
#define ZOBRIST_CASTLE_LENGTH 2
#define ZOBRIST_PIECE_LENGTH (TOTAL_BOARD_SIZE * 2 * PIECES_LENGTH)
extern const uint_least64_t ZOBRIST_PIECE[];
extern const uint_least64_t ZOBRIST_BLACK_MOVE;
extern const uint_least64_t ZOBRIST_SHORT_CASTLE[ZOBRIST_CASTLE_LENGTH];
extern const uint_least64_t ZOBRIST_LONG_CASTLE[ZOBRIST_CASTLE_LENGTH];
extern const uint_least64_t ZOBRIST_EN_PASSENT_FILE[BOARD_SIZE];
#endif