change repetitionTable memory allocation place
This commit is contained in:
@ -2,6 +2,8 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <chess/types.h>
|
#include <chess/types.h>
|
||||||
|
|
||||||
|
#define REPETETION_TABLE_LENGTH 1024
|
||||||
|
|
||||||
bool bitboardGet(const uint_least64_t *board, struct piece_t piece, 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);
|
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);
|
void bitboardSetMask(uint_least64_t *board, struct piece_t piece, uint_least64_t value);
|
||||||
@ -10,4 +12,5 @@ uint_least64_t bitboardMaskAllPieces(const uint_least64_t *board);
|
|||||||
void bitboardClear(uint_least64_t *board, struct piece_t piece, uint_least8_t i);
|
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);
|
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 piece_t pieceAtField(const uint_least64_t *board, uint_least8_t i);
|
||||||
struct gameState_t newGameState(uint_least64_t *board, char *FEN);
|
struct gameState_t newGameState(uint_least64_t *board,
|
||||||
|
struct zobristTableElement *repetitionTableStore, char *FEN);
|
||||||
|
|||||||
@ -11,8 +11,8 @@ uint_least64_t zobristPieceI(struct piece_t piece, uint_least8_t field);
|
|||||||
uint_least8_t getFile(const uint_least8_t field);
|
uint_least8_t getFile(const uint_least8_t field);
|
||||||
uint_least8_t getRank(const uint_least8_t field);
|
uint_least8_t getRank(const uint_least8_t field);
|
||||||
|
|
||||||
struct zobristTable initZobirstTable(size_t length);
|
struct zobristTable initZobirstTable(struct zobristTableElement *table, size_t length);
|
||||||
void zobristTableIter(const struct zobristTable table, const uint_least64_t key, void *result,
|
void zobristTableIter(const struct zobristTable table, const uint_least64_t key, void *result,
|
||||||
void (*foreach)(struct zobristTableElement *element, const uint_least64_t key, void *result));
|
void (*callback)(struct zobristTableElement *element, const uint_least64_t key, void *result));
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -64,7 +64,7 @@ static int_least16_t alphaBeta(const struct gameState_t gameState, int_fast16_t
|
|||||||
const struct move_t move = moves[i];
|
const struct move_t move = moves[i];
|
||||||
const struct gameState_t newGameState = makeMove(gameState, move);
|
const struct gameState_t newGameState = makeMove(gameState, move);
|
||||||
const int_least16_t score = -alphaBeta(newGameState, -beta, -alpha, depth - 1);
|
const int_least16_t score = -alphaBeta(newGameState, -beta, -alpha, depth - 1);
|
||||||
undoMove(gameState.board, move, gameState.color);
|
undoMove(newGameState, move, gameState.color);
|
||||||
if(score >= beta) {
|
if(score >= beta) {
|
||||||
return beta; // beta-cutoff
|
return beta; // beta-cutoff
|
||||||
}
|
}
|
||||||
@ -85,7 +85,7 @@ static struct move_t searchRoot(const struct gameState_t gameState, uint_least8_
|
|||||||
const struct move_t move = moves[i];
|
const struct move_t move = moves[i];
|
||||||
const struct gameState_t newGameState = makeMove(gameState, move);
|
const struct gameState_t newGameState = makeMove(gameState, move);
|
||||||
const int_least16_t score = -alphaBeta(newGameState, -beta, -alpha, depth - 1);
|
const int_least16_t score = -alphaBeta(newGameState, -beta, -alpha, depth - 1);
|
||||||
undoMove(gameState.board, move, gameState.color);
|
undoMove(newGameState, move, gameState.color);
|
||||||
if(score > alpha) {
|
if(score > alpha) {
|
||||||
alpha = score; // alpha acts like max in MiniMax
|
alpha = score; // alpha acts like max in MiniMax
|
||||||
selectedMove = move;
|
selectedMove = move;
|
||||||
|
|||||||
@ -190,8 +190,9 @@ static void app_activate(GApplication *app, gpointer data) {
|
|||||||
GtkWindow *window = GTK_WINDOW(gtk_window_new());
|
GtkWindow *window = GTK_WINDOW(gtk_window_new());
|
||||||
static RsvgHandle *piecesSvg[2 * PIECES_LENGTH];
|
static RsvgHandle *piecesSvg[2 * PIECES_LENGTH];
|
||||||
static uint_least64_t board[BITBOARD_LENGTH];
|
static uint_least64_t board[BITBOARD_LENGTH];
|
||||||
|
static struct zobristTableElement repetitionTableStore[REPETETION_TABLE_LENGTH];
|
||||||
static struct gameState_t gameState;
|
static struct gameState_t gameState;
|
||||||
gameState = newGameState(board, START_FEN);
|
gameState = newGameState(board, repetitionTableStore, START_FEN);
|
||||||
{
|
{
|
||||||
static struct move_t moves[TOTAL_BOARD_SIZE];
|
static struct move_t moves[TOTAL_BOARD_SIZE];
|
||||||
static struct drawData_t drawData = {piecesSvg, &gameState, NOT_SELECTED, moves, 0, NOT_SELECTED};
|
static struct drawData_t drawData = {piecesSvg, &gameState, NOT_SELECTED, moves, 0, NOT_SELECTED};
|
||||||
|
|||||||
@ -166,7 +166,7 @@ static void zobristTableUndoMoveCallback(struct zobristTableElement *element, co
|
|||||||
fprintf(stderr, "Error: Position does not in repetitionTable in undoMove (move.c)");
|
fprintf(stderr, "Error: Position does not in repetitionTable in undoMove (move.c)");
|
||||||
}
|
}
|
||||||
|
|
||||||
void undoMove(struct gameState_t gameState, struct move_t move, bool color) {
|
void undoMove(const struct gameState_t gameState, struct move_t move, bool color) {
|
||||||
uint_least64_t *board = gameState.board;
|
uint_least64_t *board = gameState.board;
|
||||||
const struct piece_t srcPiece = {move.srcPiece, color};
|
const struct piece_t srcPiece = {move.srcPiece, color};
|
||||||
const struct piece_t dstPiece = {move.dstPiece, color};
|
const struct piece_t dstPiece = {move.dstPiece, color};
|
||||||
@ -177,6 +177,7 @@ void undoMove(struct gameState_t gameState, struct move_t move, bool color) {
|
|||||||
bitboardSet(board, piece, getCapturePos(move.src, move.dst, move.spezialMove));
|
bitboardSet(board, piece, getCapturePos(move.src, move.dst, move.spezialMove));
|
||||||
}
|
}
|
||||||
castleMoveRook(board, move.spezialMove, color, true);
|
castleMoveRook(board, move.spezialMove, color, true);
|
||||||
|
zobristTableIter(gameState.repetitionTable, gameState.zobrist, NULL, zobristTableUndoMoveCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setCastleInfoRook(struct castle_t *canCastle, uint_least8_t posOnRank) {
|
static void setCastleInfoRook(struct castle_t *canCastle, uint_least8_t posOnRank) {
|
||||||
@ -257,14 +258,14 @@ static void addMove(struct addMoveCtx_t ctx, struct piece_t movedPiece, uint_lea
|
|||||||
}
|
}
|
||||||
move.hash = hash;
|
move.hash = hash;
|
||||||
}
|
}
|
||||||
makeMove(gameState, move);
|
struct gameState_t newGameState = makeMove(gameState, move);
|
||||||
{
|
{
|
||||||
const bool color = gameState.color;
|
const bool color = gameState.color;
|
||||||
if(!kingInCheck(board, color)) {
|
if(!kingInCheck(board, color)) {
|
||||||
ctx.moves[*movesLength] = move;
|
ctx.moves[*movesLength] = move;
|
||||||
++*movesLength;
|
++*movesLength;
|
||||||
}
|
}
|
||||||
undoMove(board, move, color);
|
undoMove(newGameState, move, color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <chess/util.h>
|
#include <chess/util.h>
|
||||||
#include <chess/bitset.h>
|
#include <chess/bitset.h>
|
||||||
|
#include <chess/bitboard.h>
|
||||||
|
|
||||||
uint_least64_t bitboardGetMask(const uint_least64_t *board, struct piece_t piece) {
|
uint_least64_t bitboardGetMask(const uint_least64_t *board, struct piece_t piece) {
|
||||||
return board[piece.color * PIECES_LENGTH + piece.type];
|
return board[piece.color * PIECES_LENGTH + piece.type];
|
||||||
@ -66,7 +67,8 @@ struct piece_t pieceAtField(const uint_least64_t *board, uint_least8_t i) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct gameState_t newGameState(uint_least64_t *board, char *FEN) {
|
struct gameState_t newGameState(uint_least64_t *board,
|
||||||
|
struct zobristTableElement *repetitionTableStore, char *FEN) {
|
||||||
struct gameState_t gameState = {board, 0};
|
struct gameState_t gameState = {board, 0};
|
||||||
for(uint_least8_t i = 0; i < BITBOARD_LENGTH; ++i) {
|
for(uint_least8_t i = 0; i < BITBOARD_LENGTH; ++i) {
|
||||||
board[i] = 0;
|
board[i] = 0;
|
||||||
@ -134,6 +136,6 @@ struct gameState_t newGameState(uint_least64_t *board, char *FEN) {
|
|||||||
gameState.enPassantTo += (BOARD_SIZE - *FEN++) * BOARD_SIZE;
|
gameState.enPassantTo += (BOARD_SIZE - *FEN++) * BOARD_SIZE;
|
||||||
}
|
}
|
||||||
gameState.halfMoveCounter = atoi(++FEN);
|
gameState.halfMoveCounter = atoi(++FEN);
|
||||||
gameState.repetitionTable = initZobirstTable(1000);
|
gameState.repetitionTable = initZobirstTable(repetitionTableStore, REPETETION_TABLE_LENGTH);
|
||||||
return gameState;
|
return gameState;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,8 +23,10 @@ uint_least8_t getRank(const uint_least8_t field) {
|
|||||||
return field / BOARD_SIZE;
|
return field / BOARD_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct zobristTable initZobirstTable(size_t length) {
|
struct zobristTable initZobirstTable(struct zobristTableElement *table, size_t length) {
|
||||||
struct zobristTableElement *table = calloc(length, sizeof *table);
|
for(size_t i = 0; i < length; ++i) {
|
||||||
|
table[i].hash = 0;
|
||||||
|
}
|
||||||
return (struct zobristTable){table, length};
|
return (struct zobristTable){table, length};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -21,14 +21,15 @@ static uint_least64_t perft(const struct gameState_t gameState, const uint_least
|
|||||||
for(uint_least8_t i = 0; i < movesLength; ++i) {
|
for(uint_least8_t i = 0; i < movesLength; ++i) {
|
||||||
const struct gameState_t newGameState = makeMove(gameState, moves[i]);
|
const struct gameState_t newGameState = makeMove(gameState, moves[i]);
|
||||||
nodes += perft(newGameState, depth - 1);
|
nodes += perft(newGameState, depth - 1);
|
||||||
undoMove(gameState.board, moves[i], gameState.color);
|
undoMove(newGameState, moves[i], gameState.color);
|
||||||
}
|
}
|
||||||
return nodes;
|
return nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test(struct perf_t perf, const uint_least8_t i) {
|
static void test(struct perf_t perf, const uint_least8_t i) {
|
||||||
uint_least64_t board[BITBOARD_LENGTH];
|
uint_least64_t board[BITBOARD_LENGTH];
|
||||||
const struct gameState_t gameState = newGameState(board, perf.FEN);
|
struct zobristTableElement repetitionTableStore[REPETETION_TABLE_LENGTH];
|
||||||
|
const struct gameState_t gameState = newGameState(board, repetitionTableStore, perf.FEN);
|
||||||
const uint_least64_t nodes = perft(gameState, perf.depth);
|
const uint_least64_t nodes = perft(gameState, perf.depth);
|
||||||
if(perf.nodes != nodes) {
|
if(perf.nodes != nodes) {
|
||||||
printf("Test %" PRIuLEAST16 " failed: FEN: %s, depth: %" PRIuLEAST8 " calculated nodes: %" PRIuLEAST64
|
printf("Test %" PRIuLEAST16 " failed: FEN: %s, depth: %" PRIuLEAST8 " calculated nodes: %" PRIuLEAST64
|
||||||
@ -38,7 +39,7 @@ static void test(struct perf_t perf, const uint_least8_t i) {
|
|||||||
for(uint_least8_t j = 0; j < movesLength; ++j) {
|
for(uint_least8_t j = 0; j < movesLength; ++j) {
|
||||||
const struct gameState_t newGameState = makeMove(gameState, moves[j]);
|
const struct gameState_t newGameState = makeMove(gameState, moves[j]);
|
||||||
const uint_least64_t nodes = perft(newGameState, perf.depth - 1);
|
const uint_least64_t nodes = perft(newGameState, perf.depth - 1);
|
||||||
undoMove(gameState.board, moves[j], gameState.color);
|
undoMove(newGameState, moves[j], gameState.color);
|
||||||
{
|
{
|
||||||
printMove(moves[j]);
|
printMove(moves[j]);
|
||||||
printf(" %" PRIuLEAST64 "\n", nodes);
|
printf(" %" PRIuLEAST64 "\n", nodes);
|
||||||
|
|||||||
Reference in New Issue
Block a user