8 Commits

14 changed files with 442 additions and 251 deletions

View File

@@ -286,7 +286,7 @@ memcheck: targets
##################################### test binaries ##################################### test binaries
.PHONY: testing test .PHONY: testing test
TEST :=piece-test fen-test bitboard-test movegen-test attack-test TEST := piece-test fen-test bitboard-test movegen-test attack-test movedo-test
PIECE_OBJS := piece.o PIECE_OBJS := piece.o
FEN_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintessence.o \ FEN_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintessence.o \
@@ -297,6 +297,8 @@ MOVEGEN_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintesse
attack.o move.o move-gen.o attack.o move.o move-gen.o
ATTACK_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintessence.o \ ATTACK_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintessence.o \
attack.o move.o move-gen.o attack.o move.o move-gen.o
MOVEDO_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintessence.o \
attack.o move.o move-gen.o move-do.o
TEST := $(addprefix $(BINDIR)/,$(TEST)) TEST := $(addprefix $(BINDIR)/,$(TEST))
@@ -305,6 +307,7 @@ FEN_OBJS := $(addprefix $(OBJDIR)/,$(FEN_OBJS))
BB_OBJS := $(addprefix $(OBJDIR)/,$(BB_OBJS)) BB_OBJS := $(addprefix $(OBJDIR)/,$(BB_OBJS))
MOVEGEN_OBJS := $(addprefix $(OBJDIR)/,$(MOVEGEN_OBJS)) MOVEGEN_OBJS := $(addprefix $(OBJDIR)/,$(MOVEGEN_OBJS))
ATTACK_OBJS := $(addprefix $(OBJDIR)/,$(ATTACK_OBJS)) ATTACK_OBJS := $(addprefix $(OBJDIR)/,$(ATTACK_OBJS))
MOVEDO_OBJS := $(addprefix $(OBJDIR)/,$(MOVEDO_OBJS))
test: test:
echo TEST=$(TEST) echo TEST=$(TEST)
@@ -332,6 +335,10 @@ bin/attack-test: test/attack-test.c test/common-test.h $(ATTACK_OBJS)
@echo compiling $@ test executable. @echo compiling $@ test executable.
@$(CC) $(ALL_CFLAGS) $< $(ATTACK_OBJS) $(ALL_LDFLAGS) -o $@ @$(CC) $(ALL_CFLAGS) $< $(ATTACK_OBJS) $(ALL_LDFLAGS) -o $@
bin/movedo-test: test/movedo-test.c test/common-test.h $(MOVEDO_OBJS)
@echo compiling $@ test executable.
@$(CC) $(ALL_CFLAGS) $< $(MOVEDO_OBJS) $(ALL_LDFLAGS) -o $@
##################################### Makefile debug ##################################### Makefile debug
.PHONY: showflags wft .PHONY: showflags wft

View File

@@ -173,10 +173,6 @@ static __always_inline bitboard_t bb_rank(int rank)
{ {
return RANK_1bb << (rank * 8); return RANK_1bb << (rank * 8);
} }
static __always_inline bitboard_t bb_file(int file)
{
return FILE_Abb << file;
}
static __always_inline bitboard_t bb_rel_rank(int rank, color) static __always_inline bitboard_t bb_rel_rank(int rank, color)
{ {
return RANK_1bb << (rank * 8); return RANK_1bb << (rank * 8);
@@ -187,11 +183,13 @@ static __always_inline bitboard_t bb_file(int file)
} }
*/ */
#define BB_RANK(r) ((u64) RANK_1bb << ((r) * 8)) #define bb_rank(r) ((u64) RANK_1bb << ((r) * 8))
#define BB_FILE(f) ((u64) FILE_Abb << (f)) #define BB_FILE(f) ((u64) FILE_Abb << (f))
#define BB_REL_RANK(r, c) (RANK_1bb << (SQ_REL_RANK((r), (c)) * 8)) #define bb_rel_rank(r, c) bb_rank(sq_rel_rank(r, c))
#define BB_REL_FILE(f, c) (FILE_Abb << (SQ_REL_RANK((f), (c))))
//#define BB_REL_RANK(r, c) (RANK_1bb << (SQ_REL_RANK(r, c) * 8))
//#define BB_REL_FILE(f, c) (FILE_Abb << (SQ_REL_RANK((f), (c))))
/** /**
* bb_sq_aligned() - check if two squares are aligned (same file or rank). * bb_sq_aligned() - check if two squares are aligned (same file or rank).

View File

@@ -21,8 +21,30 @@
#define mask(i) ( (u64) (ONE << (i)) ) #define mask(i) ( (u64) (ONE << (i)) )
#define BOARDSIZE (8*8) #define BOARDSIZE (8*8)
/* relative rank */
#define SQ_REL_RANK(r, c) (rank_t)((7 * (c)) ^ r) /**
* sq_rel - get relative square
* @sq: white point of view square
* @c: color
*
* Get relative (mirrored if @c is BLACK) square.
* Example: sq_rel(A1, WHITE) = A1, sq_rel(B2, BLACK) = B7
*
* @return: Relative square.
*/
#define sq_rel(sq, c) ((square_t)((sq) ^ (56 * (c))))
/**
* sq_rel_rank - get relative rank
* @rank: white point of view rank
* @c: color
*
* Get relative (mirrored if @c is BLACK) rank.
* Example: sq_rel(RANK_2, WHITE) = RANK_2, sq_rel(RANK_6, BLACK) = RANK_3
*
* @return: Relative rank.
*/
#define sq_rel_rank(rank, c) ((rank_t)((7 * (c)) ^ rank))
/* castle_t bits structure /* castle_t bits structure
*/ */
@@ -35,16 +57,23 @@ typedef enum {
CASTLE_W = (CASTLE_WK | CASTLE_WQ), /* 00000011 W castle mask */ CASTLE_W = (CASTLE_WK | CASTLE_WQ), /* 00000011 W castle mask */
CASTLE_B = (CASTLE_BK | CASTLE_BQ), /* 00001100 B castle mask */ CASTLE_B = (CASTLE_BK | CASTLE_BQ), /* 00001100 B castle mask */
CASTLE_K = (1 << 0), /* generic K/Q, after doing : */ CASTLE_K = (1 << 0), /* generic K/Q, bits 0 and 1 */
CASTLE_Q = (1 << 1), /* flags >> (2 * color) */ CASTLE_Q = (1 << 1),
CASTLE_KQ = (CASTLE_K |CASTLE_Q)
} castle_rights_t; } castle_rights_t;
/* determine is oo or ooo is possible with castle flags f and color c /* determine is oo or ooo is possible with castle flags f and color c
*/ */
#define NORM_CASTLE(f, c) ((f) >> (2 * (c))) /* shift flags to bits 0/1 */ //#define NORM_CASTLE(f, c) ((f) >> (2 * (c))) /* shift flags to bits 0/1 */
#define CAN_OO(f, c) (NORM_CASTLE(f, c) & CASTLE_K) //#define
#define CAN_OOO(f, c) (NORM_CASTLE(f, c) & CASTLE_Q) //(NORM_CASTLE(f, c) & CASTLE_Q)
#define CAN_CASTLE(f, c) (CAN_OO(f, c) | CAN_OOO(f, c)) #define can_oo(f, c) ((f) & (CASTLE_K << ((c) * 2)))
#define can_ooo(f, c) ((f) & (CASTLE_Q << ((c) * 2)))
#define can_castle(f, c) ((f) & (CASTLE_KQ << ((c) * 2)))
#define clr_oo(f, c) ((f) & ~(CASTLE_K << (2 * (c))))
#define clr_ooo(f, c) ((f) & ~(CASTLE_Q << (2 * (c))))
#define clr_castle(f, c) ((f) & ~(CASTLE_KQ << (2 * (c)) ))
/* game phases /* game phases
*/ */

View File

@@ -81,9 +81,9 @@ static int fen_check(pos_t *pos)
if (pos->en_passant != SQUARE_NONE) { if (pos->en_passant != SQUARE_NONE) {
rank_t eprank = sq_rank(pos->en_passant); rank_t eprank = sq_rank(pos->en_passant);
file_t epfile = sq_file(pos->en_passant); file_t epfile = sq_file(pos->en_passant);
rank_t rank5 = SQ_REL_RANK(RANK_5, pos->turn); rank_t rank5 = sq_rel_rank(RANK_5, pos->turn);
rank_t rank6 = SQ_REL_RANK(RANK_6, pos->turn); rank_t rank6 = sq_rel_rank(RANK_6, pos->turn);
rank_t rank7 = SQ_REL_RANK(RANK_7, pos->turn); rank_t rank7 = sq_rel_rank(RANK_7, pos->turn);
piece_t pawn = pos->turn == WHITE? B_PAWN: W_PAWN; piece_t pawn = pos->turn == WHITE? B_PAWN: W_PAWN;
if (warn(eprank != rank6 || if (warn(eprank != rank6 ||
pos->board[sq_make(epfile, rank5)] != pawn || pos->board[sq_make(epfile, rank5)] != pawn ||

View File

@@ -1,6 +1,6 @@
/* move-do.c - move do/undo. /* move-do.c - move do/undo.
* *
* Copyright (C) 2021 Bruno Raoult ("br") * Copyright (C) 2021-2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later. * Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING. * Some rights reserved. See COPYING.
* *
@@ -15,146 +15,158 @@
#include <ctype.h> #include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include "brlib.h"
#include "likely.h"
#include "chessdefs.h" #include "chessdefs.h"
#include "move.h" #include "move.h"
#include "position.h" #include "position.h"
/* /**
/\** * move_do() - do move.
* * move_do() - execute move in a duplicated position. * @pos: &pos_t position
* * @pos: &pos_t struct on which move will be applied * @move: move to apply
* * @move: &move_t struct to apply * @state: &state_t address where irreversible changes will be saved
* *
* * @return: &new position
* *\/
* pos_t *move_do(pos_t *pos, move_t *move)
* {
* # ifdef DEBUG_MOVE
* //log(1, "new move: ");
* //move_print(0, move, M_PR_NL | M_PR_LONG);
* # endif
* *
* pos_t *new = pos_dup(pos); * @move is applied to @pos:
* piece_t piece = PIECE(move->piece), newpiece = piece, captured = move->capture; * - bitboards and board are updated
* int color = COLOR(move->piece); * - counters are updated:
* square_t from = move->from, to = move->to; * - move count
* u64 bb_from = SQ88_2_BB(from), bb_to = SQ88_2_BB(to); * - 50-moves rule count
* - flags are possibly updated:
* - castling
* - en-passant
* - captured piece (excl. en-passant)
* *
* if (move->capture || piece == PAWN) /\* 50 moves *\/ * @return: pos.
* new->clock_50 = 0;
* else
* new->clock_50++;
*
* if (move->flags & M_CAPTURE) { /\* capture *\/
* if (move->flags & M_EN_PASSANT) {
* uchar ep_file = F88(pos->en_passant);
* square_t ep_grab = color == WHITE ? SQ88(ep_file, 4): SQ88(ep_file, 3);
* u64 bb_ep_grab = SQ88_2_BB(ep_grab);
*
* log_f(5, "en-passant=%d,%d\n", ep_file, color == WHITE ? 4 : 3);
* piece_del(&new->board[ep_grab].s_piece->list);
* new->board[ep_grab].piece = 0;
* new->occupied[OPPONENT(color)] &= ~bb_ep_grab;
* new->bb[OPPONENT(color)][BB_PAWN] &= ~bb_ep_grab;
*
* } else {
* piece_del(&new->board[to].s_piece->list);
* new->board[to].piece = 0;
* new->occupied[OPPONENT(color)] &= ~bb_to;
* new->bb[OPPONENT(color)][PIECETOBB(captured)] &= ~bb_to;
* }
*
* } else if (move->flags & M_CASTLE_Q) {
* uchar row = R88(from);
* square_t rook_from = SQ88(0, row);
* square_t rook_to = SQ88(3, row);
* u64 bb_rook_from = SQ88_2_BB(rook_from);
* u64 bb_rook_to = SQ88_2_BB(rook_to);
*
* new->board[rook_to] = new->board[rook_from];
* new->board[rook_to].s_piece->square = rook_to;
* new->occupied[color] &= ~bb_rook_from;
* new->occupied[color] |= bb_rook_to;
* new->bb[color][PIECETOBB(BB_ROOK)] &= ~bb_rook_from;
* new->bb[color][PIECETOBB(BB_ROOK)] |= bb_rook_to;
* new->board[rook_from].piece = 0;
* new->board[rook_from].s_piece = NULL;
* //new->castle &= color == WHITE? ~CASTLE_W: ~CASTLE_B;
*
* } else if (move->flags & M_CASTLE_K) {
* uchar row = R88(from);
* square_t rook_from = SQ88(7, row);
* square_t rook_to = SQ88(5, row);
* u64 bb_rook_from = SQ88_2_BB(rook_from);
* u64 bb_rook_to = SQ88_2_BB(rook_to);
*
* new->board[rook_to] = new->board[rook_from];
* new->board[rook_to].s_piece->square = rook_to;
* new->occupied[color] &= ~bb_rook_from;
* new->occupied[color] |= bb_rook_to;
* new->bb[color][PIECETOBB(BB_ROOK)] &= ~bb_rook_from;
* new->bb[color][PIECETOBB(BB_ROOK)] |= bb_rook_to;
* new->board[rook_from].piece = 0;
* new->board[rook_from].s_piece = NULL;
* // new->castle &= color == WHITE? ~CASTLE_W: ~CASTLE_B;
* }
*
* new->board[to] = new->board[from];
* /\* fix dest square *\/
* new->board[to].s_piece->square = to;
* if (move->flags & M_PROMOTION) {
* log_f(5, "promotion to %s\n", P_SYM(move->promotion));
* log_f(5, "newpiece=%#x p=%#x\n", move->promotion, PIECE(move->promotion));
* newpiece = PIECE(move->promotion);
* new->board[to].piece = move->promotion;
* new->board[to].s_piece->piece = move->promotion;
* }
* /\* replace old occupied bitboard by new one *\/
* new->occupied[color] &= ~bb_from;
* new->occupied[color] |= bb_to;
* new->bb[color][PIECETOBB(piece)] &= ~bb_from;
* new->bb[color][PIECETOBB(newpiece)] |= bb_to;
* if (move->flags & M_PROMOTION) {
* log_f(5, "promotion color=%d bbpiece=%d\n", color, PIECETOBB(newpiece));
* //bitboard_print(new->bb[color][PIECETOBB(newpiece)]);
* }
* /\* set en_passant *\/
* new->en_passant = 0;
* if (piece == PAWN) {
* if (R88(from) == 1 && R88(to) == 3)
* pos->en_passant = SQ88(F88(from), 2);
* else if (R88(from) == 6 && R88(to) == 4)
* pos->en_passant = SQ88(F88(from), 5);
* }
*
* /\* always make "from" square empty *\/
* new->board[from].piece = 0;
* new->board[from].s_piece = NULL;
*
* //printf("old turn=%d ", color);
* //printf("new turn=%d\n", new->turn);
* //fflush(stdout);
* /\* adjust castling flags *\/
* if ((bb_from | bb_to) & E1bb)
* new->castle &= ~(CASTLE_WQ | CASTLE_WK);
* else if ((bb_from | bb_to) & A1bb)
* new->castle &= ~CASTLE_WQ;
* else if ((bb_from | bb_to) & H1bb)
* new->castle &= ~CASTLE_WK;
*
* if ((bb_from | bb_to) & E8bb)
* new->castle &= ~(CASTLE_BQ | CASTLE_BK);
* else if ((bb_from | bb_to) & A8bb)
* new->castle &= ~CASTLE_BQ;
* else if ((bb_from | bb_to) & H8bb)
* new->castle &= ~CASTLE_BK;
*
* SET_COLOR(new->turn, OPPONENT(color)); /\* pos color *\/
* return new;
* }
*
* void move_undo(pos_t *pos, __unused move_t *move)
* {
* pos_del(pos);
* }
*/ */
pos_t *move_do(pos_t *pos, move_t move, state_t *state)
{
//# ifdef DEBUG_MOVE_DO
// move_print(move, M_PR_NL | M_PR_LONG);
//# endif
*state = pos->state; /* save irreversible changes */
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_t new_piece = piece;
++pos->clock_50;
++pos->plycount;
pos->en_passant = SQUARE_NONE;
pos->turn = them;
if (is_promotion(move))
new_piece = MAKE_PIECE(move_promoted(move), us);
if (is_capture(move)) {
pos->clock_50 = 0;
pos->captured = pos->board[to]; /* save capture info */
pos_clr_sq(pos, to); /* clear square */
} else if (is_castle(move)) { /* handle rook move */
square_t rookfrom, rookto;
if (is_castle_K(move)) {
rookfrom = sq_rel(H1, us);
rookto = sq_rel(F1, us);
} else {
rookfrom = sq_rel(A1, us);
rookto = sq_rel(D1, us);
}
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. */
pos->clock_50 = 0;
if (is_dpush(move)) /* if pawn double push, set e.p. */
pos->en_passant = pawn_push_up(from, us);
else if (is_enpassant(move)) { /* clear grabbed pawn */
square_t grabbed = pawn_push_up(to, them);
pos_clr_sq(pos, grabbed);
}
}
pos_clr_sq(pos, from); /* always clear "from" and set "to" */
pos_set_sq(pos, to, new_piece);
/* update castling flags
* As we always consider flags are valid, we :
* - adjust our flags if relative from is "E1", "A1", H1"
* - adjust opp flags if relative to if "A8", H8"
*/
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);
if (from == rel_e1)
pos->castle = clr_castle(pos->castle, us);
else if (from == rel_a1)
pos->castle = clr_ooo(pos->castle, us);
else if (from == rel_h1)
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);
if (to == rel_a8)
pos->castle = clr_ooo(pos->castle, them);
else if (to == rel_h8)
pos->castle = clr_oo(pos->castle, them);
}
return pos;
}
/**
* move_undo() - undo move.
* @pos: &pos_t position
* @move: move to undo
* @state: &state_t address where irreversible changes were saved
*
* @move is applied to @pos:
* - bitboards and board are updated
* - previous information is restored:
* - castling
* - en-passant
* - captured piece (excl. en-passant)
* - move count
* - 50-moves rule count
*
* @return: pos.
*/
pos_t *move_undo(pos_t *pos, const move_t move, const state_t *state)
{
//# ifdef DEBUG_MOVE
//log(1, "new move: ");
//move_print(0, move, M_PR_NL | M_PR_LONG);
//# endif
color_t them = pos->turn, us = OPPONENT(them);
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);
pos_clr_sq(pos, to); /* always clear "to" and set "from" */
pos_set_sq(pos, from, piece);
if (is_capture(move)) {
pos_set_sq(pos, to, pos->captured); /* restore captured piece */
} else if (is_castle(move)) { /* make reverse rook move */
square_t rookfrom, rookto;
if (is_castle_K(move)) {
rookfrom = sq_rel(F1, us);
rookto = sq_rel(H1, us);
} else {
rookfrom = sq_rel(D1, us);
rookto = sq_rel(A1, us);
}
pos_set_sq(pos, rookto, pos->board[rookfrom]);
pos_clr_sq(pos, rookfrom);
} else if (is_enpassant(move)) { /* restore grabbed pawn */
square_t grabbed = pawn_push_up(to, them);
pos_set_sq(pos, grabbed, MAKE_PIECE(PAWN, them));
}
return pos;
}

View File

@@ -157,8 +157,9 @@ movelist_t *pos_all_legal(const pos_t *pos, movelist_t *dest)
* - castling, if king passes an enemy-controlled square (not final square). * - castling, if king passes an enemy-controlled square (not final square).
* When immediately known, a few move flags are also applied in these cases: * When immediately known, a few move flags are also applied in these cases:
* - castling: M_CASTLE_{K,Q} * - castling: M_CASTLE_{K,Q}
* - pawn capture (incl. en-passant): M_CAPTURE * - pawn capture (excl. en-passant): M_CAPTURE
* en-passant: M_EN_PASSANT * - en-passant: M_EN_PASSANT
* - pawn double push: M_DPUSH
* - promotion: M_PROMOTION * - promotion: M_PROMOTION
* - promotion and capture * - promotion and capture
* *
@@ -174,7 +175,6 @@ int pos_gen_pseudomoves(pos_t *pos)
bitboard_t my_pieces = pos->bb[us][ALL_PIECES]; bitboard_t my_pieces = pos->bb[us][ALL_PIECES];
bitboard_t not_my_pieces = ~my_pieces; bitboard_t not_my_pieces = ~my_pieces;
bitboard_t enemy_pieces = pos->bb[them][ALL_PIECES]; bitboard_t enemy_pieces = pos->bb[them][ALL_PIECES];
bitboard_t occ = my_pieces | enemy_pieces; bitboard_t occ = my_pieces | enemy_pieces;
bitboard_t empty = ~occ; bitboard_t empty = ~occ;
@@ -183,59 +183,58 @@ int pos_gen_pseudomoves(pos_t *pos)
move_t *moves = pos->moves.move; move_t *moves = pos->moves.move;
int nmoves = pos->moves.nmoves; int nmoves = pos->moves.nmoves;
int from, to; int from, to;
/* king - MUST BE FIRST ! */ bug_on(nmoves != 0);
/* king - MUST BE FIRST (we stop if doubler check) */
from = pos->king[us]; from = pos->king[us];
movebits = bb_king_moves(not_my_pieces, from); movebits = bb_king_moves(not_my_pieces, from);
bit_for_each64(to, tmp1, movebits) { bit_for_each64(to, tmp1, movebits & empty) {
moves[nmoves++] = move_make(from, to); moves[nmoves++] = move_make(from, to);
} }
bit_for_each64(to, tmp1, movebits & enemy_pieces) {
moves[nmoves++] = move_make_capture(from, to);
}
if (popcount64(pos->checkers) > 1) /* double check, we stop here */ if (popcount64(pos->checkers) > 1) /* double check, we stop here */
return (pos->moves.nmoves = nmoves); return (pos->moves.nmoves = nmoves);
/* sliding pieces */ /* sliding pieces */
bit_for_each64(from, tmp1, pos->bb[us][BISHOP]) { bit_for_each64(from, tmp1, pos->bb[us][BISHOP] | pos->bb[us][QUEEN]) {
movebits = hyperbola_bishop_moves(occ, from) & not_my_pieces; movebits = hyperbola_bishop_moves(occ, from) & not_my_pieces;
bit_for_each64(to, tmp2, movebits) { bit_for_each64(to, tmp2, movebits & empty) {
moves[nmoves++] = move_make(from, to); moves[nmoves++] = move_make(from, to);
} }
bit_for_each64(to, tmp2, movebits & enemy_pieces) {
moves[nmoves++] = move_make_capture(from, to);
} }
bit_for_each64(from, tmp1, pos->bb[us][ROOK]) { }
bit_for_each64(from, tmp1, pos->bb[us][ROOK] | pos->bb[us][QUEEN]) {
// printf("rook=%d/%s\n", from, sq_to_string(from)); // printf("rook=%d/%s\n", from, sq_to_string(from));
movebits = hyperbola_rook_moves(occ, from) & not_my_pieces; movebits = hyperbola_rook_moves(occ, from) & not_my_pieces;
bit_for_each64(to, tmp2, movebits) { bit_for_each64(to, tmp2, movebits & empty) {
moves[nmoves++] = move_make(from, to); moves[nmoves++] = move_make(from, to);
} }
} bit_for_each64(to, tmp2, movebits & enemy_pieces) {
/* TODO: remove this one, after movegen is validated */ moves[nmoves++] = move_make_capture(from, to);
bit_for_each64(from, tmp1, pos->bb[us][QUEEN]) {
movebits = hyperbola_queen_moves(occ, from) & not_my_pieces;
bit_for_each64(to, tmp2, movebits) {
moves[nmoves++] = move_make(from, to);
} }
} }
/* knight */ /* knight */
bit_for_each64(from, tmp1, pos->bb[us][KNIGHT]) { bit_for_each64(from, tmp1, pos->bb[us][KNIGHT]) {
movebits = bb_knight_moves(not_my_pieces, from); movebits = bb_knight_moves(not_my_pieces, from);
bit_for_each64(to, tmp2, movebits) { bit_for_each64(to, tmp2, movebits & empty) {
moves[nmoves++] = move_make(from, to); moves[nmoves++] = move_make(from, to);
} }
bit_for_each64(to, tmp2, movebits & enemy_pieces) {
moves[nmoves++] = move_make_capture(from, to);
}
} }
/* pawn: relative rank and files */ /* pawn: relative rank and files */
bitboard_t rel_rank7 = us == WHITE ? RANK_7bb : RANK_2bb; bitboard_t rel_rank7 = bb_rel_rank(RANK_7, us);
bitboard_t rel_rank3 = us == WHITE ? RANK_3bb : RANK_6bb; bitboard_t rel_rank3 = bb_rel_rank(RANK_3, us);
//printf("r7_o = %016lx\nr7_n = %016lx\nsize=%lu\n", rel_rank7, BB_REL_RANK(RANK_7, me),
// sizeof(RANK_3bb));
//printf("r3_o = %016lx\nr3_n = %016lx\nsize=%lu\n", rel_rank3, BB_REL_RANK(RANK_3, me),
// sizeof(RANK_3bb));
//printf("fc_o = %016lx\nfc_n = %016lx\nsize=%lu\n", FILE_Cbb, BB_REL_FILE(FILE_C, me),
// sizeof(RANK_3bb));
//bitboard_t rel_filea = (me == WHITE ? FILE_Abb : FILE_Hbb);
//bitboard_t rel_fileh = (me == WHITE ? FILE_Hbb : FILE_Abb);
/* pawn: ranks 2-6 push 1 and 2 squares */ /* pawn: ranks 2-6 push 1 and 2 squares */
movebits = pawn_shift_up(pos->bb[us][PAWN] & ~rel_rank7, us) & empty; movebits = pawn_shift_up(pos->bb[us][PAWN] & ~rel_rank7, us) & empty;
@@ -248,7 +247,7 @@ int pos_gen_pseudomoves(pos_t *pos)
movebits = pawn_shift_up(movebits & rel_rank3, us) & empty; movebits = pawn_shift_up(movebits & rel_rank3, us) & empty;
bit_for_each64(to, tmp1, movebits) { bit_for_each64(to, tmp1, movebits) {
from = pawn_push_up(pawn_push_up(to, them), them); from = pawn_push_up(pawn_push_up(to, them), them);
moves[nmoves++] = move_make(from, to); moves[nmoves++] = move_make_flags(from, to, M_DPUSH);
} }
/* pawn: ranks 2-6 captures left */ /* pawn: ranks 2-6 captures left */
@@ -309,21 +308,21 @@ int pos_gen_pseudomoves(pos_t *pos)
/* castle - Attention ! Castling flags are assumed correct /* castle - Attention ! Castling flags are assumed correct
*/ */
if (!pos->checkers) { if (!pos->checkers) {
bitboard_t rel_rank1 = BB_REL_RANK(RANK_1, us); bitboard_t rel_rank1 = bb_rel_rank(RANK_1, us);
from = pos->king[us]; from = pos->king[us];
square_t from_square[2] = { E1, E8 }; /* verify king is on E1/E8 */ square_t from_square[2] = { E1, E8 }; /* verify king is on E1/E8 */
bug_on(CAN_CASTLE(pos->castle, us) && from != from_square[us]); bug_on(can_castle(pos->castle, us) && from != from_square[us]);
/* For castle, we check the opponent attacks on squares between from and to. /* For castle, we check the opponent attacks on squares between from and to.
* To square attack check will be done in gen_is_legal. * To square attack check will be done in gen_is_legal.
*/ */
if (CAN_OO(pos->castle, us)) { if (can_oo(pos->castle, us)) {
bitboard_t occmask = rel_rank1 & (FILE_Fbb | FILE_Gbb); bitboard_t occmask = rel_rank1 & (FILE_Fbb | FILE_Gbb);
if (!(occ & occmask) && if (!(occ & occmask) &&
!sq_attackers(pos, occ, from+1, them)) { /* f1/f8 */ !sq_attackers(pos, occ, from+1, them)) { /* f1/f8 */
moves[nmoves++] = move_make_flags(from, from + 2, M_CASTLE_K); moves[nmoves++] = move_make_flags(from, from + 2, M_CASTLE_K);
} }
} }
if (CAN_OOO(pos->castle, us)) { if (can_ooo(pos->castle, us)) {
bitboard_t occmask = rel_rank1 & (FILE_Bbb | FILE_Cbb | FILE_Dbb); bitboard_t occmask = rel_rank1 & (FILE_Bbb | FILE_Cbb | FILE_Dbb);
if (!(occ & occmask) && if (!(occ & occmask) &&
!sq_attackers(pos, occ, from-1, them)) { /* d1/d8 */ !sq_attackers(pos, occ, from-1, them)) { /* d1/d8 */

View File

@@ -110,7 +110,7 @@ char *move_str(char *dst, const move_t move, __unused const int flags)
sprintf(dst, "%s%s%n", sq_to_string(from), sq_to_string(to), &len); sprintf(dst, "%s%s%n", sq_to_string(from), sq_to_string(to), &len);
if (is_promotion(move)) { if (is_promotion(move)) {
piece_t promoted = move_promoted(move); piece_t promoted = (piece_t) move_promoted(move);
sprintf(dst + len, "%s", piece_to_low(promoted)); sprintf(dst + len, "%s", piece_to_low(promoted));
} }
return dst; return dst;
@@ -147,8 +147,8 @@ static int _moves_cmp_bysquare(const void *p1, const void *p2)
square_t t1 = move_to(m1); square_t t1 = move_to(m1);
square_t f2 = move_from(m2); square_t f2 = move_from(m2);
square_t t2 = move_to(m2); square_t t2 = move_to(m2);
piece_t prom1 = move_promoted(m1); piece_type_t prom1 = move_promoted(m1);
piece_t prom2 = move_promoted(m2); piece_type_t prom2 = move_promoted(m2);
if (f1 < f2) return -1; if (f1 < f2) return -1;
if (f1 > f2) return 1; if (f1 > f2) return 1;
/* f1 == f2 */ /* f1 == f2 */

View File

@@ -20,7 +20,7 @@
/* move structure: /* move structure:
* 3 3 2 2 1 1 1 1 1 1 * 3 3 2 2 1 1 1 1 1 1
* 1 0 5 3 8 7 5 4 2 1 6 5 0 * 1 0 4 3 8 7 5 4 2 1 6 5 0
* S UUUUUUU FFFFFF ccc ppp tttttt ffffff * S UUUUUUU FFFFFF ccc ppp tttttt ffffff
* *
* bits len off range type mask get desc * bits len off range type mask get desc
@@ -52,7 +52,8 @@ typedef enum {
M_PROMOTION = mask(M_OFF_FLAGS + 2), M_PROMOTION = mask(M_OFF_FLAGS + 2),
M_CASTLE_K = mask(M_OFF_FLAGS + 3), /* maybe only one ? */ M_CASTLE_K = mask(M_OFF_FLAGS + 3), /* maybe only one ? */
M_CASTLE_Q = mask(M_OFF_FLAGS + 5), /* maybe only one ? */ M_CASTLE_Q = mask(M_OFF_FLAGS + 5), /* maybe only one ? */
M_CHECK = mask(M_OFF_FLAGS + 6) /* maybe unknown/useless ? */ M_CHECK = mask(M_OFF_FLAGS + 6), /* maybe unknown/useless ? */
M_DPUSH = mask(M_OFF_FLAGS + 7) /* pawn double push */
} move_flags_t; } move_flags_t;
#define move_set_flags(move, flags) ((move) | (flags)) #define move_set_flags(move, flags) ((move) | (flags))
@@ -64,7 +65,7 @@ typedef enum {
#define is_castle_K(m) ((m) & M_CASTLE_K) #define is_castle_K(m) ((m) & M_CASTLE_K)
#define is_castle_Q(m) ((m) & M_CASTLE_Q) #define is_castle_Q(m) ((m) & M_CASTLE_Q)
#define is_check(m) ((m) & M_CHECK) #define is_check(m) ((m) & M_CHECK)
#define is_dpush(m) ((m) & M_DPUSH)
#define MOVES_MAX 256 #define MOVES_MAX 256
@@ -83,7 +84,7 @@ static inline square_t move_to(move_t move)
return (move >> M_OFF_TO) & 077; return (move >> M_OFF_TO) & 077;
} }
static inline piece_t move_promoted(move_t move) static inline piece_type_t move_promoted(move_t move)
{ {
return (move >> M_OFF_PROMOTED) & 07; return (move >> M_OFF_PROMOTED) & 07;
} }
@@ -93,7 +94,6 @@ static inline piece_type_t move_captured(move_t move)
return (move >> M_OFF_CAPTURED) & 07; return (move >> M_OFF_CAPTURED) & 07;
} }
static inline move_t move_make(square_t from, square_t to) static inline move_t move_make(square_t from, square_t to)
{ {
return (to << M_OFF_TO) | from; return (to << M_OFF_TO) | from;
@@ -111,7 +111,7 @@ static inline move_t move_make_capture(square_t from, square_t to)
static inline move_t move_make_enpassant(square_t from, square_t to) static inline move_t move_make_enpassant(square_t from, square_t to)
{ {
return move_make_flags(from, to, M_CAPTURE | M_ENPASSANT); return move_make_flags(from, to, M_ENPASSANT);
} }
static inline move_t move_make_promote(square_t from, square_t to, static inline move_t move_make_promote(square_t from, square_t to,
@@ -126,6 +126,11 @@ static inline move_t move_make_promote_capture(square_t from, square_t to,
return move_make_promote(from, to, promoted) | M_CAPTURE; return move_make_promote(from, to, promoted) | M_CAPTURE;
} }
static inline move_t move_set_captured(move_t move, piece_type_t captured)
{
return move | (captured << M_OFF_CAPTURED);
}
/* moves_print flags /* moves_print flags
*/ */
#define M_PR_CAPT 0x01 #define M_PR_CAPT 0x01

View File

@@ -65,10 +65,6 @@ char *piece_to_low(piece_t p)
{ {
return piece_details[p].low; return piece_details[p].low;
} }
//char *piece_to_sym(piece_t p)
//{
// return piece_details[PIECE(p)].sym;
//}
char *piece_to_sym(piece_t p) char *piece_to_sym(piece_t p)
{ {

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
@@ -299,7 +348,7 @@ void pos_print_raw(const pos_t *pos, const int type)
*/ */
void pos_print_pieces(const pos_t *pos) void pos_print_pieces(const pos_t *pos)
{ {
int bit, count, cur; int sq, count, cur;
char *pname; char *pname;
u64 tmp; u64 tmp;
bitboard_t p; bitboard_t p;
@@ -308,12 +357,12 @@ void pos_print_pieces(const pos_t *pos)
p = pos->bb[color][piece]; p = pos->bb[color][piece];
count = popcount64(p); count = popcount64(p);
cur = 0; cur = 0;
pname = piece_to_cap(piece); pname = piece_to_char(p);
printf("%s(0)%s", pname, count? ":": ""); printf("%s(%d)%s", pname, count, count? ":": "");
if (count) { if (count) {
bit_for_each64(bit, tmp, p) { bit_for_each64(sq, tmp, p) {
char cf = sq_file(bit), cr = sq_rank(bit); // char cf = sq_file(bit), cr = sq_rank(bit);
printf("%s%c%c", cur? ",": "", FILE2C(cf), RANK2C(cr)); printf("%s%s", cur? ",": "", sq_to_string(sq));
cur++; cur++;
} }
} }

View File

@@ -41,12 +41,13 @@ typedef struct __pos_s {
castle_rights_t castle; castle_rights_t castle;
u16 clock_50; u16 clock_50;
u16 plycount; /* plies so far, start from 1 */ u16 plycount; /* plies so far, start from 1 */
piece_t captured; /* only for move_undo */
); );
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 */
@@ -92,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;
} }
/** /**
@@ -130,7 +133,7 @@ static __always_inline bitboard_t pos_between_occ(const pos_t *pos,
static __always_inline int pos_between_count(const pos_t *pos, static __always_inline int pos_between_count(const pos_t *pos,
const square_t sq1, const square_t sq2) const square_t sq1, const square_t sq2)
{ {
return bb_between_excl[sq1][sq2] & pos_occ(pos); return popcount64(pos_between_occ(pos, sq1, sq2));
} }
/** /**
@@ -150,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 */

View File

@@ -19,6 +19,7 @@
#define BITBOARD 2 #define BITBOARD 2
#define MOVEGEN 4 #define MOVEGEN 4
#define ATTACK 8 #define ATTACK 8
#define MOVEDO 16
struct fentest { struct fentest {
uint modules; uint modules;
@@ -367,7 +368,18 @@ struct fentest {
"illegal, SF crash", "illegal, SF crash",
"2r1k3/3P4/8/8/8/8/8/4K3 w - - 0 1" "2r1k3/3P4/8/8/8/8/8/4K3 w - - 0 1"
}, },
{ MOVEDO,
"simple movedo/undo: only 2 W knights",
"8/1k6/8/8/8/8/6K1/1NN5 w - - 0 1"
},
{ MOVEDO,
"simple movedo/undo: only 2 W knights",
"8/1k6/8/8/8/8/6K1/1NN5 w - - 0 1"
},
{ MOVEDO,
"simple movedo/undo: only 2 W knights",
"5n2/1k6/8/8/5K2/8/P7/1N6 w - - 0 1"
},
{ 0, NULL, NULL } { 0, NULL, NULL }
}; };

80
test/movedo-test.c Normal file
View File

@@ -0,0 +1,80 @@
/* movedo-test.c - basic movedo/undo tests.
*
* Copyright (C) 2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING.
*
* You should have received a copy of the GNU General Public License along with this
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "chessdefs.h"
#include "fen.h"
#include "position.h"
#include "move.h"
#include "move-do.h"
#include "move-gen.h"
#include "common-test.h"
int main(int __unused ac, __unused char**av)
{
int i = 1;
char *fen, movebuf[8];;
pos_t *pos, *savepos;
move_t move;
setlinebuf(stdout); /* line-buffered stdout */
bitboard_init();
hyperbola_init();
while ((fen = next_fen(MOVEGEN | MOVEDO))) {
if (!(pos = fen2pos(NULL, fen))) {
printf("wrong fen %d: [%s]\n", i, fen);
continue;
}
pos_gen_pseudomoves(pos);
savepos = pos_dup(pos);
if (pos_cmp(pos, savepos) != true) {
printf("*** positions differ 1\n");
exit(0);
}
int tmp = 0, j = 1;
while ((move = pos_next_legal(pos, &tmp)) != MOVE_NONE) {
state_t state;
pos_print(pos);
printf("i=%d j=%d turn=%d move=[%s]\n", i, j, pos->turn,
move_str(movebuf, move, 0));
//move_p
move_do(pos, move, &state);
pos_print(pos);
fflush(stdout);
pos_check(pos, true);
printf("%d/%d move_do check ok\n", i, j);
move_undo(pos, move, &state);
if (pos_cmp(pos, savepos) != true) {
printf("*** positions differ 2\n");
exit(0);
}
fflush(stdout);
pos_check(pos, true);
printf("%d/%d move_undo check ok\n", i, j);
if (j++ == 1000)
exit(0);
}
pos_del(savepos);
pos_del(pos);
i++;
}
return 0;
}

0
util.c
View File