3 Commits

10 changed files with 221 additions and 50 deletions

View File

@@ -286,7 +286,7 @@ memcheck: targets
##################################### test binaries
.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
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_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintessence.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))
@@ -305,6 +307,7 @@ FEN_OBJS := $(addprefix $(OBJDIR)/,$(FEN_OBJS))
BB_OBJS := $(addprefix $(OBJDIR)/,$(BB_OBJS))
MOVEGEN_OBJS := $(addprefix $(OBJDIR)/,$(MOVEGEN_OBJS))
ATTACK_OBJS := $(addprefix $(OBJDIR)/,$(ATTACK_OBJS))
MOVEDO_OBJS := $(addprefix $(OBJDIR)/,$(MOVEDO_OBJS))
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.
@$(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
.PHONY: showflags wft

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 */

View File

@@ -109,7 +109,7 @@ bool pseudo_is_legal(const pos_t *pos, const move_t move)
* @start is set to next non-checked move in pseudo-legal list.
* Position pseudo-legal moves must be already calculated before calling this function.
*
* @return: move, or -1 if no move.
* @return: move, or MOVE_NONE if no move.
*/
move_t pos_next_legal(const pos_t *pos, int *start)
{

View File

@@ -35,8 +35,8 @@
typedef s32 move_t;
/* special move_t values */
#define MOVE_NONE (-1)
#define MOVE_NULL (0) /* hack: from = to = A1 */
#define MOVE_NONE ((move_t) -1)
#define MOVE_NULL ((move_t) 0) /* hack: from = to = A1 */
enum {
M_OFF_FROM = 0,

View File

@@ -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

View File

@@ -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 */

View File

@@ -14,11 +14,12 @@
#include <stdio.h>
#include "chessdefs.h"
/* when below FENs are in a struct with selection per test */
#define NOTEST 0
#define FEN 1
#define BITBOARD 2
#define MOVEGEN 4
#define ATTACK 8
#define NOTEST 0
#define FEN 1
#define BITBOARD 2
#define MOVEGEN 4
#define ATTACK 8
#define MOVEDO 16
struct fentest {
uint modules;
@@ -367,7 +368,18 @@ struct fentest {
"illegal, SF crash",
"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 }
};

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