2 Commits

Author SHA1 Message Date
856e3e52da add movedo-test 2024-03-21 07:00:20 +01:00
51a348c1e4 add pos_cmp (as a debug check after move_{do,undo}) 2024-03-21 06:58:56 +01:00
6 changed files with 190 additions and 39 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

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

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