From 51a348c1e4c4faa2dbb57191d82f8fc4ee1e2320 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Thu, 21 Mar 2024 06:58:56 +0100 Subject: [PATCH] add pos_cmp (as a debug check after move_{do,undo}) --- src/position.c | 81 ++++++++++++++++++++++++++++++++++++++++---------- src/position.h | 35 ++++++++++++---------- util.c | 0 3 files changed, 84 insertions(+), 32 deletions(-) delete mode 100644 util.c diff --git a/src/position.c b/src/position.c index 80feea3..2d98be4 100644 --- a/src/position.c +++ b/src/position.c @@ -47,14 +47,7 @@ pos_t *pos_new(void) * pos_dup() - duplicate a position. * @pos: &position to duplicate. * - * New position is the same as source one (with duplicated pieces list), - * except: - * - moves list is empty - * - bestmove is NULL - * - nodecount is set to zero - * - eval is set to EVAL_INVALID - * - moves_generated ans moves_counted are unset - * - check is set to zero + * Return a copy, allocated with malloc(1), of @pos. * * @Return: The new position. * @@ -64,14 +57,7 @@ pos_t *pos_dup(const pos_t *pos) { pos_t *newpos = safe_malloc(sizeof(pos_t)); - //board = new->board; *newpos = *pos; - //new->bestmove = NULL; - newpos->node_count = 0; - //new->eval = EVAL_INVALID; - //new->moves_generated = false; - //new->moves_counted = false; - //new->check[WHITE] = new->check[BLACK] = 0; return newpos; } @@ -87,6 +73,8 @@ void pos_del(pos_t *pos) /** * pos_clear() - clear a position. * @pos: &position. + * + * @return: @pos. */ pos_t *pos_clear(pos_t *pos) { @@ -96,10 +84,12 @@ pos_t *pos_clear(pos_t *pos) pos->node_count = 0; pos->turn = WHITE; + /* move_do/undo position state */ pos->en_passant = SQUARE_NONE; pos->castle = 0; pos->clock_50 = 0; pos->plycount = 0; + pos->captured = NO_PIECE; for (square_t sq = A1; sq <= H8; ++sq) pos->board[sq] = EMPTY; @@ -107,7 +97,7 @@ pos_t *pos_clear(pos_t *pos) for (color_t color = WHITE; color <= BLACK; ++color) { for (piece_type_t piece = 0; piece <= KING; ++piece) pos->bb[color][piece] = 0; - pos->controlled[color] = 0; + //pos->controlled[color] = 0; pos->king[color] = SQUARE_NONE; } @@ -120,6 +110,65 @@ pos_t *pos_clear(pos_t *pos) return pos; } +/** + * pos_cmp() - compare two positions.. + * @pos1, @pos2: The two &position. + * + * @return: true if equal, false otherwise. + */ +bool pos_cmp(const pos_t *pos1, const pos_t *pos2) +{ +#define _cmpf(a) (pos1->a != pos2->a) + bool ret = false; + if (warn_on(_cmpf(node_count))) + goto end; + if (warn_on(_cmpf(turn))) + goto end; + + /* move_do/undo position state */ + if (warn_on(_cmpf(en_passant))) + goto end; + if (warn_on(_cmpf(castle))) + goto end; + if (warn_on(_cmpf(clock_50))) + goto end; + if (warn_on(_cmpf(plycount))) + goto end; + if (warn_on(_cmpf(captured))) + goto end; + + for (square_t sq = A1; sq <= H8; ++sq) + if (warn_on(_cmpf(board[sq]))) + goto end; + + for (color_t color = WHITE; color <= BLACK; ++color) { + for (piece_type_t piece = 0; piece <= KING; ++piece) + if (warn_on(_cmpf(bb[color][piece]))) + goto end; + //pos->controlled[color] = 0; + if (warn_on(_cmpf(king[color]))) + goto end; + } + + if (warn_on(_cmpf(checkers))) + goto end; + if (warn_on(_cmpf(pinners))) + goto end; + if (warn_on(_cmpf(blockers))) + goto end; + + if (warn_on(_cmpf(moves.nmoves))) + goto end; + for (int i = 0; i < pos1->moves.nmoves; ++i) + if (warn_on(_cmpf(moves.move[i]))) + goto end; + + ret = true; +end: + return ret; +#undef _cmpf +} + /** * pos_checkers() - find all checkers on a king. * @pos: &position diff --git a/src/position.h b/src/position.h index 5c548ef..0de3539 100644 --- a/src/position.h +++ b/src/position.h @@ -47,7 +47,7 @@ typedef struct __pos_s { piece_t board[BOARDSIZE]; bitboard_t bb[2][PIECE_TYPE_MAX]; /* bb[0][PAWN], bb[1][ALL_PIECES] */ - bitboard_t controlled[2]; /* unsure */ + //bitboard_t controlled[2]; /* unsure */ square_t king[2]; /* dup with bb, faster retrieval */ bitboard_t checkers; /* opponent checkers */ bitboard_t pinners; /* opponent pinners */ @@ -93,6 +93,8 @@ static __always_inline void pos_clr_sq(pos_t *pos, square_t square) pos->board[square] = EMPTY; pos->bb[color][type] &= ~mask(square); pos->bb[color][ALL_PIECES] &= ~mask(square); + if (type == KING) + pos->king[color] = SQUARE_NONE; } /** @@ -151,25 +153,26 @@ static __always_inline int pos_between_count(const pos_t *pos, //void bitboard_print(bitboard_t bb, char *title); //void bitboard_print2(bitboard_t bb1, bitboard_t bb2, char *title); -extern pos_t *pos_new(); -extern pos_t *pos_dup(const pos_t *pos); -extern void pos_del(pos_t *pos); -extern pos_t *pos_clear(pos_t *pos); +pos_t *pos_new(); +pos_t *pos_dup(const pos_t *pos); +void pos_del(pos_t *pos); +pos_t *pos_clear(pos_t *pos); +bool pos_cmp(const pos_t *pos1, const pos_t *pos2); -extern bitboard_t pos_checkers(const pos_t *pos, const color_t color); -extern bitboard_t pos_king_pinners(const pos_t *pos, const color_t color); -extern bitboard_t pos_king_blockers(const pos_t *pos, const color_t color, const bitboard_t ); -//extern bitboard_t set_king_pinners_blockers(pos_t *pos); -//extern char *pos_checkers2str(const pos_t *pos, char *str); -//extern char *pos_pinners2str(const pos_t *pos, char *str); +bitboard_t pos_checkers(const pos_t *pos, const color_t color); +bitboard_t pos_king_pinners(const pos_t *pos, const color_t color); +bitboard_t pos_king_blockers(const pos_t *pos, const color_t color, const bitboard_t ); +//bitboard_t set_king_pinners_blockers(pos_t *pos); +//char *pos_checkers2str(const pos_t *pos, char *str); +//char *pos_pinners2str(const pos_t *pos, char *str); -extern int pos_check(const pos_t *pos, const bool strict); +int pos_check(const pos_t *pos, const bool strict); -extern void pos_print(const pos_t *pos); -extern void pos_print_mask(const pos_t *pos, const bitboard_t mask); -extern void pos_print_raw(const pos_t *pos, const int type); +void pos_print(const pos_t *pos); +void pos_print_mask(const pos_t *pos, const bitboard_t mask); +void pos_print_raw(const pos_t *pos, const int type); -extern void pos_print_pieces(const pos_t *pos); +void pos_print_pieces(const pos_t *pos); #endif /* POSITION_H */ diff --git a/util.c b/util.c deleted file mode 100644 index e69de29..0000000