add pos_cmp (as a debug check after move_{do,undo})

This commit is contained in:
2024-03-21 06:58:56 +01:00
parent 51c35e21f4
commit 51a348c1e4
3 changed files with 84 additions and 32 deletions

View File

@@ -47,14 +47,7 @@ pos_t *pos_new(void)
* pos_dup() - duplicate a position. * pos_dup() - duplicate a position.
* @pos: &position to duplicate. * @pos: &position to duplicate.
* *
* New position is the same as source one (with duplicated pieces list), * Return a copy, allocated with malloc(1), of @pos.
* 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: The new position. * @Return: The new position.
* *
@@ -64,14 +57,7 @@ pos_t *pos_dup(const pos_t *pos)
{ {
pos_t *newpos = safe_malloc(sizeof(pos_t)); pos_t *newpos = safe_malloc(sizeof(pos_t));
//board = new->board;
*newpos = *pos; *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; return newpos;
} }
@@ -87,6 +73,8 @@ void pos_del(pos_t *pos)
/** /**
* pos_clear() - clear a position. * pos_clear() - clear a position.
* @pos: &position. * @pos: &position.
*
* @return: @pos.
*/ */
pos_t *pos_clear(pos_t *pos) pos_t *pos_clear(pos_t *pos)
{ {
@@ -96,10 +84,12 @@ pos_t *pos_clear(pos_t *pos)
pos->node_count = 0; pos->node_count = 0;
pos->turn = WHITE; pos->turn = WHITE;
/* move_do/undo position state */
pos->en_passant = SQUARE_NONE; pos->en_passant = SQUARE_NONE;
pos->castle = 0; pos->castle = 0;
pos->clock_50 = 0; pos->clock_50 = 0;
pos->plycount = 0; pos->plycount = 0;
pos->captured = NO_PIECE;
for (square_t sq = A1; sq <= H8; ++sq) for (square_t sq = A1; sq <= H8; ++sq)
pos->board[sq] = EMPTY; pos->board[sq] = EMPTY;
@@ -107,7 +97,7 @@ pos_t *pos_clear(pos_t *pos)
for (color_t color = WHITE; color <= BLACK; ++color) { for (color_t color = WHITE; color <= BLACK; ++color) {
for (piece_type_t piece = 0; piece <= KING; ++piece) for (piece_type_t piece = 0; piece <= KING; ++piece)
pos->bb[color][piece] = 0; pos->bb[color][piece] = 0;
pos->controlled[color] = 0; //pos->controlled[color] = 0;
pos->king[color] = SQUARE_NONE; pos->king[color] = SQUARE_NONE;
} }
@@ -120,6 +110,65 @@ pos_t *pos_clear(pos_t *pos)
return 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_checkers() - find all checkers on a king.
* @pos: &position * @pos: &position

View File

@@ -47,7 +47,7 @@ typedef struct __pos_s {
piece_t board[BOARDSIZE]; piece_t board[BOARDSIZE];
bitboard_t bb[2][PIECE_TYPE_MAX]; /* bb[0][PAWN], bb[1][ALL_PIECES] */ 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 */ square_t king[2]; /* dup with bb, faster retrieval */
bitboard_t checkers; /* opponent checkers */ bitboard_t checkers; /* opponent checkers */
bitboard_t pinners; /* opponent pinners */ 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->board[square] = EMPTY;
pos->bb[color][type] &= ~mask(square); pos->bb[color][type] &= ~mask(square);
pos->bb[color][ALL_PIECES] &= ~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_print(bitboard_t bb, char *title);
//void bitboard_print2(bitboard_t bb1, bitboard_t bb2, char *title); //void bitboard_print2(bitboard_t bb1, bitboard_t bb2, char *title);
extern pos_t *pos_new(); pos_t *pos_new();
extern pos_t *pos_dup(const pos_t *pos); pos_t *pos_dup(const pos_t *pos);
extern void pos_del(pos_t *pos); void pos_del(pos_t *pos);
extern pos_t *pos_clear(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); 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); 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 ); 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); //bitboard_t set_king_pinners_blockers(pos_t *pos);
//extern char *pos_checkers2str(const pos_t *pos, char *str); //char *pos_checkers2str(const pos_t *pos, char *str);
//extern char *pos_pinners2str(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); void pos_print(const pos_t *pos);
extern void pos_print_mask(const pos_t *pos, const bitboard_t mask); 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_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 */ #endif /* POSITION_H */

0
util.c
View File