move_{do,undo}: FIX forgotten king[] update, add few bug_on()

This commit is contained in:
2024-03-23 17:37:40 +01:00
parent 798047b84d
commit d0279125ae
3 changed files with 29 additions and 9 deletions

2
brlib

Submodule brlib updated: 8ff163dcf5...e55ac95301

View File

@@ -17,10 +17,12 @@
#include "brlib.h"
#include "likely.h"
#include "bug.h"
#include "chessdefs.h"
#include "move.h"
#include "position.h"
#include "move-do.h"
/**
* move_do() - do move.
@@ -40,7 +42,7 @@
*
* @return: pos.
*/
pos_t *move_do(pos_t *pos, move_t move, state_t *state)
pos_t *move_do(pos_t *pos, const move_t move, state_t *state)
{
//# ifdef DEBUG_MOVE_DO
// move_print(move, M_PR_NL | M_PR_LONG);
@@ -50,6 +52,8 @@ pos_t *move_do(pos_t *pos, move_t move, state_t *state)
color_t us = pos->turn, them = OPPONENT(us);
square_t from = move_from(move), to = move_to(move);
piece_t piece = pos->board[from];
piece_type_t ptype = PIECE(piece);
color_t pcolor = COLOR(piece);
piece_t new_piece = piece;
++pos->clock_50;
@@ -57,12 +61,17 @@ pos_t *move_do(pos_t *pos, move_t move, state_t *state)
pos->en_passant = SQUARE_NONE;
pos->turn = them;
if (is_promotion(move))
bug_on(pcolor != us);
if (is_promotion(move)) {
bug_on(sq_rank(to) != sq_rel_rank(RANK_8, us));
new_piece = MAKE_PIECE(move_promoted(move), us);
}
if (is_capture(move)) {
pos->clock_50 = 0;
pos->captured = pos->board[to]; /* save capture info */
bug_on(pos->board[to] == EMPTY || COLOR(pos->captured) != them);
pos_clr_sq(pos, to); /* clear square */
} else if (is_castle(move)) { /* handle rook move */
square_t rookfrom, rookto;
@@ -76,7 +85,7 @@ pos_t *move_do(pos_t *pos, move_t move, state_t *state)
pos_set_sq(pos, rookto, pos->board[rookfrom]);
pos_clr_sq(pos, rookfrom);
pos->castle = clr_castle(pos->castle, us);
} else if (PIECE(piece) == PAWN) { /* pawn non capture or e.p. */
} else if (ptype == PAWN) { /* pawn non capture or e.p. */
pos->clock_50 = 0;
if (is_dpush(move)) /* if pawn double push, set e.p. */
pos->en_passant = pawn_push_up(from, us);
@@ -89,6 +98,9 @@ pos_t *move_do(pos_t *pos, move_t move, state_t *state)
pos_clr_sq(pos, from); /* always clear "from" and set "to" */
pos_set_sq(pos, to, new_piece);
if (ptype == KING)
pos->king[us] = to;
/* update castling flags
* As we always consider flags are valid, we :
* - adjust our flags if relative from is "E1", "A1", H1"
@@ -96,7 +108,8 @@ pos_t *move_do(pos_t *pos, move_t move, state_t *state)
*/
if (can_castle(pos->castle, us)) { /* do we save time with this test ? */
square_t rel_e1 = sq_rel(E1, us);
square_t rel_a1 = sq_rel(A1, us); square_t rel_h1 = sq_rel(H1, us);
square_t rel_a1 = sq_rel(A1, us);
square_t rel_h1 = sq_rel(H1, us);
if (from == rel_e1)
pos->castle = clr_castle(pos->castle, us);
else if (from == rel_a1)
@@ -105,7 +118,8 @@ pos_t *move_do(pos_t *pos, move_t move, state_t *state)
pos->castle = clr_oo(pos->castle, us);
}
if (can_castle(pos->castle, them)) { /* do we save time with this test ? */
square_t rel_a8 = sq_rel(A8, us); square_t rel_h8 = sq_rel(H8, us);
square_t rel_a8 = sq_rel(A8, us);
square_t rel_h8 = sq_rel(H8, us);
if (to == rel_a8)
pos->castle = clr_ooo(pos->castle, them);
else if (to == rel_h8)
@@ -142,7 +156,6 @@ pos_t *move_undo(pos_t *pos, const move_t move, const state_t *state)
square_t from = move_from(move), to = move_to(move);
piece_t piece = pos->board[to];
pos->state = *state; /* restore irreversible changes */
if (is_promotion(move))
piece = MAKE_PIECE(PAWN, us);
@@ -150,6 +163,9 @@ pos_t *move_undo(pos_t *pos, const move_t move, const state_t *state)
pos_clr_sq(pos, to); /* always clear "to" and set "from" */
pos_set_sq(pos, from, piece);
if (PIECE(piece) == KING)
pos->king[us] = from;
if (is_capture(move)) {
pos_set_sq(pos, to, pos->captured); /* restore captured piece */
} else if (is_castle(move)) { /* make reverse rook move */
@@ -168,5 +184,7 @@ pos_t *move_undo(pos_t *pos, const move_t move, const state_t *state)
pos_set_sq(pos, grabbed, MAKE_PIECE(PAWN, them));
}
pos->state = *state; /* restore irreversible changes */
pos->turn = us;
return pos;
}

View File

@@ -14,7 +14,9 @@
#ifndef MOVE_DO_H
#define MOVE_DO_H
//extern pos_t *move_do(pos_t *pos, move_t *move);
//extern void move_undo(pos_t *pos, move_t *move);
#include "position.h"
pos_t *move_do(pos_t *pos, const move_t move, state_t *state);
pos_t *move_undo(pos_t *pos, const move_t move, const state_t *state);
#endif /* MOVE_DO_H */