32 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
51c35e21f4 castling: lowercase macros, nove-{do,undo}: full (untested) version 2024-03-20 10:00:07 +01:00
3a06407d5a move: exclusive M_CAPTURE / M_EN_PASSANT 2024-03-20 09:58:42 +01:00
ae6328ce26 position: add captured piece 2024-03-20 09:57:10 +01:00
08064dd1a1 start bb-based move_do 2024-03-19 18:30:31 +01:00
49705bc707 rename few macros to lowercase, add M_DPUSH move flag 2024-03-19 18:30:10 +01:00
8527c3dee1 movegen: separate all captures (easier later handling) 2024-03-18 10:21:25 +01:00
dc916f5c56 move.h: revert flags to direct bit mask 2024-03-18 09:15:16 +01:00
748953a767 move-gen: separate pawn capture from en-passant (easier later testing) 2024-03-18 09:13:51 +01:00
9c2e76442d add a struct_group for move_do() irreversible changes 2024-03-17 17:34:21 +01:00
49302c7a60 lowercase move flags macros 2024-03-17 17:33:48 +01:00
db38b507ff add gmon.out 2024-03-17 17:30:51 +01:00
9d40a53aea simplify pos_next_legal() 2024-03-16 12:14:59 +01:00
260d8d34bd move.h: simplify flags. movegen.c: add pos_next_legal() 2024-03-16 10:06:55 +01:00
037d49e4ca move-do: copy from pre-bitboard version 2024-03-15 10:53:26 +01:00
4eb620a873 add occ param in sq_attackers() 2024-03-15 09:14:10 +01:00
92dcb1e778 fix king move in pos_is_legal() 2024-03-15 09:12:42 +01:00
c8aea61529 pos_checkers(): add occ parameter 2024-03-15 09:11:28 +01:00
b3f0dd0534 add move_str() 2024-03-15 09:10:32 +01:00
0f06ccb8db simplify piece_details structure/functions (need to rewrite) 2024-03-15 09:08:49 +01:00
3e477f7442 movegen-test: add SF/uci promotion parsing 2024-03-15 09:07:02 +01:00
aba0113344 add piece-test 2024-03-15 09:04:44 +01:00
cc7d91ebfb default compile-command: make testing 2024-03-15 09:03:30 +01:00
17d1dc52f9 movegen: pseudo_is_legal() and pos_legalmoves() 2024-03-12 10:45:20 +01:00
cc3754ae00 add position blockers 2024-03-12 10:44:25 +01:00
08082faed3 moves_print() and move_sort_by_sq(): use movelist_t instead of pos_t 2024-03-12 10:42:24 +01:00
6ee4cd1642 cleanup 2024-03-11 16:12:45 +01:00
b7fdcca66d pos_check(): add adjacent kings test 2024-03-11 16:11:51 +01:00
890cb05296 add move_make_promote_capture and move_make_capture 2024-03-11 16:10:16 +01:00
301ca24783 add funcs comments and const parameters 2024-03-11 16:07:12 +01:00
d81dca6e23 add sq_line array, bb_sq_aligned3, renamed sq_manh to sq_taxi 2024-03-11 16:04:45 +01:00
30 changed files with 1280 additions and 1305 deletions

View File

@@ -1,3 +1,3 @@
((nil . ((compile-command . (concat "make -C " ((nil . ((compile-command . (concat "make -C "
(vc-root-dir) (vc-root-dir)
" -k -j2"))))) " -k -j2 testing")))))

1
.gitignore vendored
View File

@@ -17,4 +17,5 @@ vgcore.*
/tmp/ /tmp/
/notused/ /notused/
valgrind.out valgrind.out
gmon.out
compile_commands.json compile_commands.json

View File

@@ -64,10 +64,12 @@ CPPFLAGS += -DWARN_ON # brlib bug.h
#CPPFLAGS += -DDEBUG_DEBUG # enable log() functions #CPPFLAGS += -DDEBUG_DEBUG # enable log() functions
#CPPFLAGS += -DDEBUG_DEBUG_C # enable log() settings #CPPFLAGS += -DDEBUG_DEBUG_C # enable log() settings
#CPPFLAGS += -DDEBUG_POOL # memory pools management #CPPFLAGS += -DDEBUG_POOL # memory pools management
#CPPFLAGS += -DDEBUG_FEN # FEN decoding
#CPPFLAGS += -DDEBUG_POS # position.c #CPPFLAGS += -DDEBUG_POS # position.c
#CPPFLAGS += -DDEBUG_MOVE # move generation #CPPFLAGS += -DDEBUG_MOVE # move generation
# fen.c
#CPPFLAGS += -DDEBUG_FEN # FEN decoding
# attack.c # attack.c
#CPPFLAGS += -DDEBUG_ATTACK_ATTACKERS1 # sq_attackers details #CPPFLAGS += -DDEBUG_ATTACK_ATTACKERS1 # sq_attackers details
CPPFLAGS += -DDEBUG_ATTACK_ATTACKERS # sq_attackers CPPFLAGS += -DDEBUG_ATTACK_ATTACKERS # sq_attackers
@@ -205,7 +207,7 @@ cleanobjdir: cleanobj
# "normal" ones, but do not imply to rebuild target. # "normal" ones, but do not imply to rebuild target.
$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR) $(DEPDIR) $(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR) $(DEPDIR)
@echo compiling brchess module: $< "->" $@. @echo compiling brchess module: $< "->" $@.
$(CC) -c $(ALL_CFLAGS) $< -o $@ @$(CC) -c $(ALL_CFLAGS) $< -o $@
##################################### brlib libraries ##################################### brlib libraries
.PHONY: cleanbrlib cleanallbrlib brlib .PHONY: cleanbrlib cleanallbrlib brlib
@@ -231,7 +233,7 @@ cleanbindir:
$(call rmdir,$(BINDIR),binaries) $(call rmdir,$(BINDIR),binaries)
$(TARGET): libs $(OBJ) | $(BINDIR) $(TARGET): libs $(OBJ) | $(BINDIR)
@echo generating $@ executable. @echo generating $@.
$(CC) $(LDFLAGS) $(OBJ) $(LIBS) -o $@ $(CC) $(LDFLAGS) $(OBJ) $(LIBS) -o $@
##################################### pre-processed (.i) and assembler (.s) output ##################################### pre-processed (.i) and assembler (.s) output
@@ -241,11 +243,11 @@ cleanasmcpp:
@$(call rmfiles,$(ASMFILES) $(CPPFILES),asm and pre-processed) @$(call rmfiles,$(ASMFILES) $(CPPFILES),asm and pre-processed)
%.i: %.c %.i: %.c
@echo generating $@ @echo generating $@ (cpp processed).
@$(CC) -E $(CPPFLAGS) $(CFLAGS) $< -o $@ @$(CC) -E $(CPPFLAGS) $(CFLAGS) $< -o $@
%.s: %.c %.s: %.c
@echo generating $@ @echo generating $@ (asm).
@$(CC) -S -fverbose-asm $(CPPFLAGS) $(CFLAGS) $< -o $@ @$(CC) -S -fverbose-asm $(CPPFLAGS) $(CFLAGS) $< -o $@
##################################### LSP (ccls) ##################################### LSP (ccls)
@@ -284,8 +286,9 @@ memcheck: targets
##################################### test binaries ##################################### test binaries
.PHONY: testing test .PHONY: testing test
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 \ FEN_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintessence.o \
attack.o attack.o
BB_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintessence.o \ BB_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintessence.o \
@@ -294,13 +297,17 @@ 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))
PIECE_OBJS := $(addprefix $(OBJDIR)/,$(PIECE_OBJS))
FEN_OBJS := $(addprefix $(OBJDIR)/,$(FEN_OBJS)) 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)
@@ -308,6 +315,10 @@ test:
testing: $(TEST) testing: $(TEST)
bin/piece-test: test/piece-test.c $(FEN_OBJS)
@echo compiling $@ test executable.
@$(CC) $(ALL_CFLAGS) $< $(FEN_OBJS) $(ALL_LDFLAGS) -o $@
bin/fen-test: test/fen-test.c test/common-test.h $(FEN_OBJS) bin/fen-test: test/fen-test.c test/common-test.h $(FEN_OBJS)
@echo compiling $@ test executable. @echo compiling $@ test executable.
@$(CC) $(ALL_CFLAGS) $< $(FEN_OBJS) $(ALL_LDFLAGS) -o $@ @$(CC) $(ALL_CFLAGS) $< $(FEN_OBJS) $(ALL_LDFLAGS) -o $@
@@ -324,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

@@ -20,9 +20,13 @@
#include "hyperbola-quintessence.h" #include "hyperbola-quintessence.h"
#include "attack.h" #include "attack.h"
// #define DEBUG_ATTACK_ATTACKERS1
/** /**
* sq_attackers() - find attackers on a square * sq_attackers() - find attackers on a square
* @pos: position * @pos: position
* @occ: occupation mask used
* @sq: square to test * @sq: square to test
* @c: attacker color * @c: attacker color
* *
@@ -36,14 +40,11 @@
* *
* @Return: a bitboard of attackers. * @Return: a bitboard of attackers.
*/ */
bitboard_t sq_attackers(const pos_t *pos, const bitboard_t occ, const square_t sq, const color_t c)
// #define DEBUG_ATTACK_ATTACKERS1
bitboard_t sq_attackers(const pos_t *pos, const square_t sq, const color_t c)
{ {
bitboard_t attackers = 0, tmp; bitboard_t attackers = 0, tmp;
bitboard_t sqbb = mask(sq); bitboard_t sqbb = mask(sq);
bitboard_t occ = pos_occ(pos); //bitboard_t occ = pos_occ(pos);
bitboard_t to; bitboard_t to;
color_t opp = OPPONENT(c); color_t opp = OPPONENT(c);
@@ -96,24 +97,24 @@ bitboard_t sq_attackers(const pos_t *pos, const square_t sq, const color_t c)
} }
/** /**
* sq_pinners() - find pinners on a square * sq_pinners() - get "pinners" on a square
* @pos: position * @pos: position
* @sq: square to test * @sq: square to test
* @c: attacker color * @color: attacker color
* *
* Find all @c pieces which are separated from @sq by only one piece (of * Find all @c pieces which are separated from @sq by only one piece (of
* any color). * any color).
* *
* @Return: a bitboard of attackers. * @Return: bitboard of pinners.
*/ */
bitboard_t sq_pinners(const pos_t *pos, const square_t sq, const color_t c) bitboard_t sq_pinners(const pos_t *pos, const square_t sq, const color_t color)
{ {
bitboard_t attackers, pinners = 0; bitboard_t attackers, pinners = 0;
bitboard_t occ = pos_occ(pos); bitboard_t occ = pos_occ(pos);
bitboard_t maybe_pinner, tmp, lines; bitboard_t maybe_pinner, tmp, lines;
/* bishop type */ /* bishop type */
attackers = pos->bb[c][BISHOP] | pos->bb[c][QUEEN]; attackers = pos->bb[color][BISHOP] | pos->bb[color][QUEEN];
/* occupancy on sq diag and antidiag */ /* occupancy on sq diag and antidiag */
lines = (bb_sqdiag[sq] | bb_sqanti[sq]) & occ; lines = (bb_sqdiag[sq] | bb_sqanti[sq]) & occ;
bit_for_each64(maybe_pinner, tmp, attackers) { bit_for_each64(maybe_pinner, tmp, attackers) {
@@ -124,7 +125,7 @@ bitboard_t sq_pinners(const pos_t *pos, const square_t sq, const color_t c)
} }
/* same for rook type */ /* same for rook type */
attackers = pos->bb[c][ROOK] | pos->bb[c][QUEEN]; attackers = pos->bb[color][ROOK] | pos->bb[color][QUEEN];
lines = (bb_sqrank[sq] | bb_sqfile[sq]) & occ; lines = (bb_sqrank[sq] | bb_sqfile[sq]) & occ;
bit_for_each64(maybe_pinner, tmp, attackers) { bit_for_each64(maybe_pinner, tmp, attackers) {
bitboard_t between = bb_between_excl[maybe_pinner][sq]; bitboard_t between = bb_between_excl[maybe_pinner][sq];
@@ -151,5 +152,6 @@ bitboard_t sq_pinners(const pos_t *pos, const square_t sq, const color_t c)
*/ */
bitboard_t sq_attackers_all(const pos_t *pos, const square_t sq) bitboard_t sq_attackers_all(const pos_t *pos, const square_t sq)
{ {
return sq_attackers(pos, sq, WHITE) | sq_attackers(pos, sq, BLACK); bitboard_t occ = pos_occ(pos);
return sq_attackers(pos, occ, sq, WHITE) | sq_attackers(pos, occ, sq, BLACK);
} }

View File

@@ -17,7 +17,7 @@
#include "chessdefs.h" #include "chessdefs.h"
#include "bitboard.h" #include "bitboard.h"
extern bitboard_t sq_attackers(const pos_t *pos, const square_t sq, const color_t c); bitboard_t sq_attackers(const pos_t *pos, const bitboard_t occ, const square_t sq, const color_t c);
extern bitboard_t sq_attackers_all(const pos_t *pos, const square_t sq); bitboard_t sq_attackers_all(const pos_t *pos, const square_t sq);
extern bitboard_t sq_pinners(const pos_t *pos, const square_t sq, const color_t c); bitboard_t sq_pinners(const pos_t *pos, const square_t sq, const color_t c);
#endif #endif

View File

@@ -25,6 +25,7 @@ bitboard_t bb_sq[64];
bitboard_t bb_sqrank[64], bb_sqfile[64], bb_sqdiag[64], bb_sqanti[64]; bitboard_t bb_sqrank[64], bb_sqfile[64], bb_sqdiag[64], bb_sqanti[64];
bitboard_t bb_between_excl[64][64]; bitboard_t bb_between_excl[64][64];
bitboard_t bb_between[64][64]; bitboard_t bb_between[64][64];
bitboard_t bb_line[64][64];
bitboard_t bb_knight[64], bb_king[64]; bitboard_t bb_knight[64], bb_king[64];
@@ -59,7 +60,7 @@ bitboard_t bitboard_between_excl(square_t sq1, square_t sq2)
btwn_bits = (m1 << sq1) ^ (m1 << sq2); /* includes sq1 and sq2 */ btwn_bits = (m1 << sq1) ^ (m1 << sq2); /* includes sq1 and sq2 */
rank_diff = ((sq2 | 7) - sq1) >> 3, /* signed */ rank_diff = ((sq2 | 7) - sq1) >> 3, /* signed */
file_diff = (sq2 & 7) - (sq1 & 7); /* signed */ file_diff = (sq2 & 7) - (sq1 & 7); /* signed */
anti_diff = rank_diff + file_diff; anti_diff = rank_diff + file_diff;
rank_diff = rank_diff & 15; rank_diff = rank_diff & 15;
@@ -139,6 +140,24 @@ void bitboard_init(void)
bb_sqdiag[sq] = tmpbb[sq][2]; bb_sqdiag[sq] = tmpbb[sq][2];
bb_sqanti[sq] = tmpbb[sq][3]; bb_sqanti[sq] = tmpbb[sq][3];
} }
for (square_t sq1 = 0; sq1 < 64; ++sq1) {
for (square_t sq2 = 0; sq2 < 64; ++sq2) {
if (sq1 != sq2) {
if (bb_sqfile[sq1] == bb_sqfile[sq2])
bb_line[sq1][sq2] = bb_sqfile[sq1];
else if (bb_sqrank[sq1] == bb_sqrank[sq2])
bb_line[sq1][sq2] = bb_sqrank[sq1];
else if (bb_sqdiag[sq1] == bb_sqdiag[sq2])
bb_line[sq1][sq2] = bb_sqdiag[sq1];
else if (bb_sqanti[sq1] == bb_sqanti[sq2])
bb_line[sq1][sq2] = bb_sqanti[sq1];
//if (bb_line[sq1][sq2]) {
// printf("bb_line[%d][%d] = %16lx\n", sq1, sq2, bb_line[sq1][sq2]);
//}
}
}
}
/* 3) knight and king moves */ /* 3) knight and king moves */
for (square_t sq = A1; sq <= H8; ++sq) { for (square_t sq = A1; sq <= H8; ++sq) {

View File

@@ -21,8 +21,6 @@
#include "board.h" #include "board.h"
#include "piece.h" #include "piece.h"
//typedef u64 bitboard_t;
/* mapping square -> bitboard */ /* mapping square -> bitboard */
extern bitboard_t bb_sq[64]; extern bitboard_t bb_sq[64];
/* squares between sq1 and sq2, exclusing both */ /* squares between sq1 and sq2, exclusing both */
@@ -30,12 +28,17 @@ extern bitboard_t bb_between_excl[64][64];
/* squares between sq1 and sq2, including sq2 */ /* squares between sq1 and sq2, including sq2 */
extern bitboard_t bb_between[64][64]; extern bitboard_t bb_between[64][64];
/* bb_sqrank[64]: square to rank /**
* bb_sqrank[64]: square to rank
* bb_sqfile[64]: square to file * bb_sqfile[64]: square to file
* bb_sqdiag[64]: square to diagonal * bb_sqdiag[64]: square to diagonal
* bb_sqanti[64]: square to antidiagonal * bb_sqanti[64]: square to antidiagonal
*/ */
extern bitboard_t bb_sqrank[64], bb_sqfile[64], bb_sqdiag[64], bb_sqanti[64]; extern bitboard_t bb_sqrank[64], bb_sqfile[64], bb_sqdiag[64], bb_sqanti[64];
/* line (rank, file, diagonal or anti-diagonal) between two squares */
extern bitboard_t bb_line[64][64];
/* knight and king moves */ /* knight and king moves */
extern bitboard_t bb_knight[64], bb_king[64]; extern bitboard_t bb_knight[64], bb_king[64];
@@ -170,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);
@@ -184,25 +183,34 @@ 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).
* @sq1: square 1 * @sq1, @sq2: the two squares.
* @sq2: square 2
* *
* @sq2 is included in return value, to be non zero if the two squares * @return: true if @sq1 and @sq2 are on same line, false otherwise.
* are neighbors.
*
* @return: bitboard of squares between @sq1 and @sq2, including @sq2.
*/ */
static __always_inline bitboard_t bb_sq_aligned(square_t sq1, square_t sq2) static __always_inline bool bb_sq_aligned(square_t sq1, square_t sq2)
{ {
return bb_between[sq1][sq2]; return bb_line[sq1][sq2];
}
/**
* bb_sq_aligned3() - check if 3 squares are aligned (same file or rank).
* @sq1, @sq2, @sq3: the three squares.
*
* @return: true if @sq1, @sq2, and @sq3 are aligned, false otherwise.
*/
static __always_inline bool bb_sq_aligned3(square_t sq1, square_t sq2, square_t sq3)
{
return bb_line[sq1][sq2] & mask(sq3);
} }
/** /**

View File

@@ -65,9 +65,9 @@ void board_print(const piece_t *board)
for (int file = 0; file < 8; ++file) { for (int file = 0; file < 8; ++file) {
piece_t pc = board[sq_make(file, rank)]; piece_t pc = board[sq_make(file, rank)];
# ifdef DIAGRAM_SYM # ifdef DIAGRAM_SYM
printf(" %s |", pc? piece_to_sym_color(pc): " "); printf(" %s |", pc? piece_to_sym(pc): " ");
# else # else
printf(" %s |", pc? piece_to_sym_color(pc): " "); printf(" %s |", pc? piece_to_fen(pc): " ");
# endif # endif
} }
printf("\n +---+---+---+---+---+---+---+---+\n"); printf("\n +---+---+---+---+---+---+---+---+\n");
@@ -96,7 +96,7 @@ void board_print_mask(const piece_t *board, const bitboard_t mask)
bitboard_t set = mask(sq) & mask; bitboard_t set = mask(sq) & mask;
printf("%s", set? REVERSE : " "); printf("%s", set? REVERSE : " ");
# ifdef DIAGRAM_SYM # ifdef DIAGRAM_SYM
printf("%s", pc? piece_to_sym_color(pc): " "); printf("%s", pc? piece_to_sym(pc): " ");
# else # else
printf("%s", pc? piece_to_char_color(pc): " "); printf("%s", pc? piece_to_char_color(pc): " ");
# endif # endif
@@ -118,7 +118,7 @@ void board_print_raw(const piece_t *board, const int type)
for (file_t f = FILE_A; f <= FILE_H; ++f) { for (file_t f = FILE_A; f <= FILE_H; ++f) {
piece_t p = board[sq_make(f, r)]; piece_t p = board[sq_make(f, r)];
if (type) { if (type) {
printf("%s ", p == EMPTY? ".": piece_to_char_color(p)); printf("%s ", p == EMPTY? ".": piece_to_char(p));
} else { } else {
printf("%02o ", p); printf("%02o ", p);
} }

View File

@@ -59,14 +59,28 @@ static __always_inline rank_t sq_rank(square_t square)
#define sq_ok(sq) ((sq) >= A1 && (sq) <= H8) #define sq_ok(sq) ((sq) >= A1 && (sq) <= H8)
#define sq_coord_ok(c) ((c) >= 0 && (c) < 8) #define sq_coord_ok(c) ((c) >= 0 && (c) < 8)
/* Chebyshev distance: max( |r2 - r1|, |f2 - f1| ) /**
* sq_dist() - Chebyshev (king) distance between two squares (macro).
* @sq1, @sq2: The two squares.
*
* See: https://www.chessprogramming.org/Distance * See: https://www.chessprogramming.org/Distance
* Distance is max( |r2 - r1|, |f2 - f1| )
*
* @Return: the Chebyshev distance.
*/ */
#define sq_dist(sq1, sq2) (max(abs(sq_file(sq2) - sq_file(sq1)), \ #define sq_dist(sq1, sq2) (max(abs(sq_file(sq2) - sq_file(sq1)), \
abs(sq_rank(sq2) - sq_rank(sq1)))) abs(sq_rank(sq2) - sq_rank(sq1))))
/* Manhattan distance: |r2 - r1| + |f2 - f1|
/**
* sq_taxi() - Manhattan (taxi) distance between two squares (macro).
* @sq1, @sq2: The two squares.
*
* See: https://www.chessprogramming.org/Distance
* Distance is |r2 - r1| + |f2 - f1|.
*
* @Return: the Manhattan distance.
*/ */
#define sq_manh(sq1, sq2) (abs(sq_file(sq2) - sq_file(sq1)) + \ #define sq_taxi(sq1, sq2) (abs(sq_file(sq2) - sq_file(sq1)) + \
abs(sq_rank(sq2) - sq_rank(sq1))) abs(sq_rank(sq2) - sq_rank(sq1)))
extern const char *sq_to_string(const square_t sq); extern const char *sq_to_string(const square_t sq);

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 ||
@@ -133,7 +133,8 @@ static int fen_check(pos_t *pos)
if (!(error = pos_check(pos, 0))) { if (!(error = pos_check(pos, 0))) {
/* TODO: Should it really be here ? */ /* TODO: Should it really be here ? */
pos->checkers = pos_checkers(pos, pos->turn); pos->checkers = pos_checkers(pos, pos->turn);
pos->pinners = pos_pinners(pos, pos->turn); pos->pinners = pos_king_pinners(pos, pos->turn);
pos->blockers = pos_king_blockers(pos, pos->turn, pos->pinners);
} }
return error ? -1: warning; return error ? -1: warning;
} }
@@ -183,9 +184,9 @@ pos_t *fen2pos(pos_t *pos, const char *fen)
file += *cur - '0'; file += *cur - '0';
continue; continue;
} }
if ((piece = char_color_to_piece(*cur)) != EMPTY) { if ((piece = piece_from_fen(*cur)) != EMPTY) {
# ifdef DEBUG_FEN # ifdef DEBUG_FEN
log_i(5, "f=%d r=%d *p=%c piece=%#04x t=%d c=%d\n", file, rank, *cur, printf("f=%d r=%d *p=%c piece=%#04x t=%d c=%d\n", file, rank, *cur,
piece, PIECE(piece), COLOR(piece)); piece, PIECE(piece), COLOR(piece));
# endif # endif
pos_set_sq(&tmppos, sq_make(file, rank), piece); pos_set_sq(&tmppos, sq_make(file, rank), piece);
@@ -295,13 +296,23 @@ char *pos2fen(const pos_t *pos, char *fen)
for (file_t f = FILE_A; f <= FILE_H;) { for (file_t f = FILE_A; f <= FILE_H;) {
square_t sq = sq_make(f, r); square_t sq = sq_make(f, r);
piece_t piece = pos->board[sq]; piece_t piece = pos->board[sq];
if (pos->board[sq] == EMPTY) { # ifdef DEBUG_FEN
printf("r=%d f=%d p=%d pos=%d\n", r, f, piece, cur);
# endif
if (piece == EMPTY) {
int len = 0; int len = 0;
for (; f <= FILE_H && pos->board[sq_make(f, r)] == EMPTY; f++) for (; f <= FILE_H && pos->board[sq_make(f, r)] == EMPTY; f++)
len++; len++;
# ifdef DEBUG_FEN
printf("empty=%d char=%c\n", len, '0' + len);
# endif
fen[cur++] = '0' + len; fen[cur++] = '0' + len;
} else { } else {
fen[cur++] = *piece_to_char_color(piece); fen[cur++] = *piece_to_fen(piece);
# ifdef DEBUG_FEN
printf("f1=%d r=%d c=%c t=%d c=%d \n", f, r,
*(piece_to_fen(piece)), PIECE(piece), COLOR(piece));
# endif
f++; f++;
} }
} }

View File

@@ -86,15 +86,16 @@ void hyperbola_init()
} }
/** /**
* hyperbola_rank_moves() - generate rank moves for a sliding piece. * hyperbola_rank_moves() - get rank moves for a sliding piece.
* @pieces: occupation bitboard * @pieces: occupation bitboard
* @sq: piece square * @sq: piece square
* *
* Rank attacks are not handled by HQ, so we do it with a * Rank attacks are not handled by HQ, so this function uses a pre-calculated
* rank attacks table (@bb_rank_attacks).
* *
* @Return: The moves mask for piece * @Return: bitboard of @piece available pseudo-moves.
*/ */
static bitboard_t hyperbola_rank_moves(bitboard_t occ, square_t sq) bitboard_t hyperbola_rank_moves(bitboard_t occ, square_t sq)
{ {
u32 rank = sq & SQ_RANKMASK; u32 rank = sq & SQ_RANKMASK;
u32 file = sq & SQ_FILEMASK; u32 file = sq & SQ_FILEMASK;
@@ -109,16 +110,19 @@ static bitboard_t hyperbola_rank_moves(bitboard_t occ, square_t sq)
} }
/** /**
* hyperbola_moves() - generate hyperbola moves mask for a given sliding piece * hyperbola_moves() - get hyperbola pseudo-moves for a sliding piece
* @pieces: occupation bitboard * @pieces: occupation bitboard
* @sq: piece square * @sq: piece square
* @mask: mask considered * @mask: the appropriate mask (pre-calculated)
* *
* See https://www.chessprogramming.org/Hyperbola_Quintessence * This function can be used for files, diagonal, and anti-diagonal attacks.
* @mask is the corresponding pre-calculated table (@bb_sqfile, @bb_sqdiag,
* or @bb_sqanti).
* See https://www.chessprogramming.org/Hyperbola_Quintessence for details.
* *
* @Return: The moves mask for piece * @Return: bitboard of piece available pseudo-moves.
*/ */
static bitboard_t hyperbola_moves(const bitboard_t pieces, const square_t sq, bitboard_t hyperbola_moves(const bitboard_t pieces, const square_t sq,
const bitboard_t mask) const bitboard_t mask)
{ {
bitboard_t o = pieces & mask; bitboard_t o = pieces & mask;
@@ -130,32 +134,77 @@ static bitboard_t hyperbola_moves(const bitboard_t pieces, const square_t sq,
& mask; & mask;
} }
static bitboard_t hyperbola_file_moves(bitboard_t occ, square_t sq) /**
* hyperbola_file_moves() - get file pseudo-moves for a sliding piece.
* @pieces: occupation bitboard
* @sq: piece square
*
* @Return: bitboard of piece available pseudo-moves on its file.
*/
bitboard_t hyperbola_file_moves(const bitboard_t occ, const square_t sq)
{ {
return hyperbola_moves(occ, sq, bb_sqfile[sq]); return hyperbola_moves(occ, sq, bb_sqfile[sq]);
} }
static bitboard_t hyperbola_diag_moves(bitboard_t occ, square_t sq) /**
* hyperbola_diag_moves() - get diagonal pseudo-moves for a sliding piece.
* @pieces: occupation bitboard
* @sq: piece square
*
* @Return: bitboard of piece available pseudo-moves on its diagonal.
*/
bitboard_t hyperbola_diag_moves(const bitboard_t occ, const square_t sq)
{ {
return hyperbola_moves(occ, sq, bb_sqdiag[sq]); return hyperbola_moves(occ, sq, bb_sqdiag[sq]);
} }
static bitboard_t hyperbola_anti_moves(bitboard_t occ, square_t sq) /**
* hyperbola_anti_moves() - get anti-diagonal pseudo-moves for a sliding piece.
* @pieces: occupation bitboard
* @sq: piece square
*
* @Return: bitboard of piece available pseudo-moves on its anti-diagonal.
*/
bitboard_t hyperbola_anti_moves(const bitboard_t occ, const square_t sq)
{ {
return hyperbola_moves(occ, sq, bb_sqanti[sq]); return hyperbola_moves(occ, sq, bb_sqanti[sq]);
} }
bitboard_t hyperbola_bishop_moves(bitboard_t occ, square_t sq) /**
* hyperbola_bishop_moves() - get bitboard of bishop pseudo-moves
* @occ: occupation bitboard
* @sq: bishop square
*
* @Return: bitboard of bishop available pseudo-moves.
*/
bitboard_t hyperbola_bishop_moves(const bitboard_t occ, const square_t sq)
{ {
return hyperbola_diag_moves(occ, sq) | hyperbola_anti_moves(occ, sq); return hyperbola_diag_moves(occ, sq) | hyperbola_anti_moves(occ, sq);
} }
bitboard_t hyperbola_rook_moves(bitboard_t occ, square_t sq) /**
* hyperbola_rook_moves() - get bitboard of rook pseudo-moves
* @occ: occupation bitboard
* @sq: rook square
*
* @Return: bitboard of rook available pseudo-moves.
*/
bitboard_t hyperbola_rook_moves(const bitboard_t occ, const square_t sq)
{ {
return hyperbola_file_moves(occ, sq) | hyperbola_rank_moves(occ, sq); return hyperbola_file_moves(occ, sq) | hyperbola_rank_moves(occ, sq);
} }
bitboard_t hyperbola_queen_moves(bitboard_t occ, square_t sq) /**
* hyperbola_queen_moves() - get bitboard of queen pseudo-moves
* @occ: occupation bitboard
* @sq: queen square
*
* This function is a wrapper over @hyperbola_bishop_moves() and
* @hyperbola_rook_moves().
*
* @Return: bitboard of queen available pseudo-moves.
*/
bitboard_t hyperbola_queen_moves(const bitboard_t occ, const square_t sq)
{ {
return hyperbola_bishop_moves(occ, sq) | hyperbola_rook_moves(occ, sq); return hyperbola_bishop_moves(occ, sq) | hyperbola_rook_moves(occ, sq);
} }

View File

@@ -17,9 +17,17 @@
#include "board.h" #include "board.h"
#include "bitboard.h" #include "bitboard.h"
void hyperbola_init(void); extern void hyperbola_init(void);
extern bitboard_t hyperbola_bishop_moves(bitboard_t occ, square_t sq);
extern bitboard_t hyperbola_rook_moves(bitboard_t occ, square_t sq); extern bitboard_t hyperbola_rank_moves(const bitboard_t occ, const square_t sq);
extern bitboard_t hyperbola_queen_moves(bitboard_t occ, square_t sq); extern bitboard_t hyperbola_moves(const bitboard_t pieces, const square_t sq,
const bitboard_t mask);
extern bitboard_t hyperbola_file_moves(const bitboard_t occ, const square_t sq);
extern bitboard_t hyperbola_diag_moves(const bitboard_t occ, const square_t sq);
extern bitboard_t hyperbola_anti_moves(const bitboard_t occ, const square_t sq);
extern bitboard_t hyperbola_bishop_moves(const bitboard_t occ, const square_t sq);
extern bitboard_t hyperbola_rook_moves(const bitboard_t occ, const square_t sq);
extern bitboard_t hyperbola_queen_moves(const bitboard_t occ, const square_t sq);
#endif /* _HYPERBOLA_QUINTESSENCE_H */ #endif /* _HYPERBOLA_QUINTESSENCE_H */

172
src/move-do.c Normal file
View File

@@ -0,0 +1,172 @@
/* move-do.c - move do/undo.
*
* Copyright (C) 2021-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 <malloc.h>
#include <ctype.h>
#include <stdlib.h>
#include "brlib.h"
#include "likely.h"
#include "chessdefs.h"
#include "move.h"
#include "position.h"
/**
* move_do() - do move.
* @pos: &pos_t position
* @move: move to apply
* @state: &state_t address where irreversible changes will be saved
*
* @move is applied to @pos:
* - bitboards and board are updated
* - counters are updated:
* - move count
* - 50-moves rule count
* - flags are possibly updated:
* - castling
* - en-passant
* - captured piece (excl. en-passant)
*
* @return: 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;
}

20
src/move-do.h Normal file
View File

@@ -0,0 +1,20 @@
/* move-do.h - move do/undo.
*
* Copyright (C) 2021 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>
*
*/
#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);
#endif /* MOVE_DO_H */

View File

@@ -27,95 +27,154 @@
#include "move-gen.h" #include "move-gen.h"
/**
* gen_castle() - generate c
* @pos: position
*
* Generate all pseudo pawn pushes.
*/
//int gen_pawn_push(pos_t *pos, bitboard_t occ)
//{
//}
/** /**
* pseudo_is_legal() - check if a move is legal. * pseudo_is_legal() - check if a move is legal.
* @pos: position * @pos: position
* @move: move_t * @move: move_t to verify
*
* We check all possible invalid moves:
* (1) King:
* - K moves to a controlled square
* - Castling:
* - K passes a controlled square - already done in pseudomove gen
*
* (2) En-passant:
* - pinned taking pawn, done in (3)
* - taking and taken pawn on same rank than king, discovered check on rank
*
* (3) Pinned pieces:
* - if destination square quits "pinned line"
* *
* @return: true if move is valid, false otherwise. * @return: true if move is valid, false otherwise.
*/ */
bool pseudo_is_legal(pos_t *pos, move_t move) bool pseudo_is_legal(const pos_t *pos, const move_t move)
{ {
int from = move_from(move); color_t us = pos->turn, them = OPPONENT(us);
int to = move_to(move); square_t from = move_from(move), to = move_to(move), king = pos->king[us], sq;
int us = pos->turn; piece_type_t piece = PIECE(pos->board[from]);
int them = OPPONENT(us); bitboard_t kingbb = pos->bb[us][KING], tmp;
int piece = pos->board[from];
/* (1) - castling, skipped */ /* (1) - King
* For castling, we already tested intermediate squares attacks
/* (2) - King */ * in pseudo move generation, so we only care destination square here.
* Attention: We need to exclude king from occupation bitboard !
*/
if (piece == KING) { if (piece == KING) {
/* For castling, we already intermediate and destination squares bitboard_t occ = pos_occ(pos) ^ kingbb;
* attacks in pseudo move generation, so we only care destination return !sq_attackers(pos, occ, to, them);
* square here.
*/
return sq_attackers(pos, to, them) ? false: true;
} }
/* 3 - en-passant */ /* (2) - King is in check
* Double-check is already handled, as only K moves were generated.
* Here, allowed moves are only on King-attacker line, including
/* * attacker.We can move a piece on
* if (bb_test(pins, from) && !bb_test(Ray[king][from], to)) * in pseudo move generation, so we only care destination square here.
* return false;
* // En-passant special case: also illegal if self-check through the en-passant captured pawn
* if (to == pos->epSquare && piece == PAWN) {
* const int us = pos->turn, them = opposite(us);
* bitboard_t occ = pos_pieces(pos);
* bb_clear(&occ, from);
* bb_set(&occ, to);
* bb_clear(&occ, to + push_inc(them));
* return !(bb_rook_attacks(king, occ) & pos_pieces_cpp(pos, them, ROOK, QUEEN)) &&
* !(bb_bishop_attacks(king, occ) & pos_pieces_cpp(pos, them, BISHOP, QUEEN));
* } else
* return true;
*/ */
if (pos->checkers) {
if (popcount64(pos->checkers) == 1) { /* one checker */
square_t checker = ctz64(pos->checkers);
bitboard_t between = bb_between[king][checker];
return mask(to) & between;
}
return false; /* double check */
}
/* (3) - pinned pieces
* We verify here that pinned piece P stays on line King-P.
*/
if (mask(from) & pos->blockers & pos->bb[us][ALL_PIECES]) {
bitboard_t line = bb_line[from][king];
return line & mask(to); /* to is not on pin line */
}
/* (4) - En-passant
* We only care the situation where our King and enemy R/Q are on
* 5th relative rank. To do so, we create an occupation bb without
* the 2 pawns.
*/
if (is_enpassant(move)) {
/* from rank bitboard */
bitboard_t rank5 = bb_sqrank[from];
/* enemy rooks/queens on from rank */
bitboard_t rooks = (pos->bb[them][ROOK] | pos->bb[them][QUEEN]) & rank5;
if ((kingbb & rank5) && rooks) { /* K and enemy R/Q on rank */
/* captured pawn square (beside from square) */
square_t captured = sq_make(sq_file(pos->en_passant), sq_rank(from));
/* occupation bitboard without the two "disappearing" pawns */
bitboard_t occ = pos_occ(pos) ^ mask(from) ^ mask(captured);
bit_for_each64(sq, tmp, rooks) /* check all rooks/queens */
if (hyperbola_rank_moves(occ, sq) & kingbb)
return false;
}
return true;
}
return true; return true;
} }
/** /**
* gen_all_pseudomoves() - generate all pseudo moves * pos_next_legal() - get next legal move in position.
* @pos: position
* @start: &int, starting position in move list
*
* Get next valid move in @pos move list, from move @start, or MOVE_NONE.
* @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.
*/
move_t pos_next_legal(const pos_t *pos, int *start)
{
const int nmoves = pos->moves.nmoves;
const move_t *moves = pos->moves.move;
move_t move;
while (*start < nmoves) {
if (pseudo_is_legal(pos, (move = moves[(*start)++] )))
return move;
}
return MOVE_NONE;
}
/**
* pos_all_legal() - get the list of legal moves from pseudo-legal.
* @pos: position
* @dest: destination &movelist_t
*
* The pseudo-legal moves must be already calculated before calling this function.
* No check is done on @dest limits.
*
* @Return: @dest
*/
movelist_t *pos_all_legal(const pos_t *pos, movelist_t *dest)
{
int tmp = dest->nmoves = 0;
move_t move;
//int tmp = 0;
while ((move = pos_next_legal(pos, &tmp)) != MOVE_NONE)
dest->move[dest->nmoves++] = move;
return dest;
}
/**
* pos_gen_pseudomoves() - generate position pseudo-legal moves
* @pos: position * @pos: position
* *
* Generate all moves, no check is done on validity due to castle rules, * Generate all @pos pseudo moves for player-to-move.
* or check (pinned pieces, etc...). * The @pos->moves table is filled with the moves.
* *
* @return: The number of moves. * Only a few validity checks are done here (i.e. moves are not generated):
* - castling, if king is in check
* - castling, if king passes an enemy-controlled square (not final square).
* When immediately known, a few move flags are also applied in these cases:
* - castling: M_CASTLE_{K,Q}
* - pawn capture (excl. en-passant): M_CAPTURE
* - en-passant: M_EN_PASSANT
* - pawn double push: M_DPUSH
* - promotion: M_PROMOTION
* - promotion and capture
*
* TODO: move code to specific functions (especially castling, pawn push/capture)
*
* @Return: The total number of moves.
*/ */
int gen_all_pseudomoves(pos_t *pos) int pos_gen_pseudomoves(pos_t *pos)
{ {
color_t us = pos->turn, them = OPPONENT(us); color_t us = pos->turn;
color_t them = OPPONENT(us);
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;
@@ -124,61 +183,58 @@ int gen_all_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 */ 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, tmp2, 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);
int en_passant = pos->en_passant == SQUARE_NONE? 0: pos->en_passant;
bitboard_t enemy_avail = bb_sq[en_passant] | enemy_pieces;
/* 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;
@@ -187,11 +243,38 @@ int gen_all_pseudomoves(pos_t *pos)
//printf("push %d->%d %s->%s", from, to, sq_to_string(from), sq_to_string(to)); //printf("push %d->%d %s->%s", from, to, sq_to_string(from), sq_to_string(to));
moves[nmoves++] = move_make(from, to); moves[nmoves++] = move_make(from, to);
} }
/* possible second push */
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 */
from_pawns = pos->bb[us][PAWN] & ~rel_rank7; // & ~rel_filea;
movebits = pawn_shift_upleft(from_pawns, us) & enemy_pieces;
bit_for_each64(to, tmp1, movebits) {
from = pawn_push_upleft(to, them); /* reverse capture */
moves[nmoves++] = move_make_capture(from, to);
}
/* pawn: ranks 2-6 captures right */
from_pawns = pos->bb[us][PAWN] & ~rel_rank7; // & ~rel_fileh;
movebits = pawn_shift_upright(from_pawns, us) & enemy_pieces;
bit_for_each64(to, tmp1, movebits) {
from = pawn_push_upright(to, them);
moves[nmoves++] = move_make_capture(from, to);
}
/* pawn: en-passant */
if ((to = pos->en_passant) != SQUARE_NONE) {
movebits = mask(to);
from_pawns = pos->bb[us][PAWN]
& (pawn_shift_upleft(movebits, them) | pawn_shift_upright(movebits, them));
bit_for_each64(from, tmp1, from_pawns) {
moves[nmoves++] = move_make_enpassant(from, to);
}
}
/* pawn: rank 7 push */ /* pawn: rank 7 push */
movebits = pawn_shift_up(pos->bb[us][PAWN] & rel_rank7, us) & empty; movebits = pawn_shift_up(pos->bb[us][PAWN] & rel_rank7, us) & empty;
bit_for_each64(to, tmp1, movebits) { bit_for_each64(to, tmp1, movebits) {
@@ -201,64 +284,48 @@ int gen_all_pseudomoves(pos_t *pos)
moves[nmoves++] = move_make_promote(from, to, BISHOP); moves[nmoves++] = move_make_promote(from, to, BISHOP);
moves[nmoves++] = move_make_promote(from, to, KNIGHT); moves[nmoves++] = move_make_promote(from, to, KNIGHT);
} }
/* pawn promotion: rank 7 captures left */
/* pawn: ranks 2-6 captures left, including en-passant */
from_pawns = pos->bb[us][PAWN] & ~rel_rank7; // & ~rel_filea;
movebits = pawn_shift_upleft(from_pawns, us) & enemy_avail;
bit_for_each64(to, tmp1, movebits) {
from = pawn_push_upleft(to, them); /* reverse capture */
moves[nmoves++] = move_make(from, to);
}
/* pawn: rank 7 captures left */
from_pawns = pos->bb[us][PAWN] & rel_rank7; // & ~rel_filea; from_pawns = pos->bb[us][PAWN] & rel_rank7; // & ~rel_filea;
movebits = pawn_shift_upleft(from_pawns, us) & enemy_avail; movebits = pawn_shift_upleft(from_pawns, us) & enemy_pieces;
bit_for_each64(to, tmp1, movebits) { bit_for_each64(to, tmp1, movebits) {
from = pawn_push_upleft(to, them); /* reverse capture */ from = pawn_push_upleft(to, them); /* reverse capture */
moves[nmoves++] = move_make_promote(from, to, QUEEN); moves[nmoves++] = move_make_promote_capture(from, to, QUEEN);
moves[nmoves++] = move_make_promote(from, to, ROOK); moves[nmoves++] = move_make_promote_capture(from, to, ROOK);
moves[nmoves++] = move_make_promote(from, to, BISHOP); moves[nmoves++] = move_make_promote_capture(from, to, BISHOP);
moves[nmoves++] = move_make_promote(from, to, KNIGHT); moves[nmoves++] = move_make_promote_capture(from, to, KNIGHT);
}
/* pawn: ranks 2-6 captures right, including en-passant */
from_pawns = pos->bb[us][PAWN] & ~rel_rank7; // & ~rel_fileh;
movebits = pawn_shift_upright(from_pawns, us) & enemy_avail;
bit_for_each64(to, tmp1, movebits) {
from = pawn_push_upright(to, them);
moves[nmoves++] = move_make(from, to);
} }
/* pawn: rank 7 captures right */ /* pawn: rank 7 captures right */
from_pawns = pos->bb[us][PAWN] & rel_rank7; // & ~rel_fileh; from_pawns = pos->bb[us][PAWN] & rel_rank7; // & ~rel_fileh;
movebits = pawn_shift_upright(from_pawns, us) & enemy_pieces; movebits = pawn_shift_upright(from_pawns, us) & enemy_pieces;
bit_for_each64(to, tmp1, movebits) { bit_for_each64(to, tmp1, movebits) {
from = pawn_push_upright(to, them); /* reverse capture */ from = pawn_push_upright(to, them); /* reverse capture */
moves[nmoves++] = move_make_promote(from, to, QUEEN); moves[nmoves++] = move_make_promote_capture(from, to, QUEEN);
moves[nmoves++] = move_make_promote(from, to, ROOK); moves[nmoves++] = move_make_promote_capture(from, to, ROOK);
moves[nmoves++] = move_make_promote(from, to, BISHOP); moves[nmoves++] = move_make_promote_capture(from, to, BISHOP);
moves[nmoves++] = move_make_promote(from, to, KNIGHT); moves[nmoves++] = move_make_promote_capture(from, to, KNIGHT);
} }
/* castle - Attention ! We consider that castle flags are 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, 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, from-1, them)) { /* d1/d8 */ !sq_attackers(pos, occ, from-1, them)) { /* d1/d8 */
moves[nmoves++] = move_make_flags(from, from - 2, M_CASTLE_Q); moves[nmoves++] = move_make_flags(from, from - 2, M_CASTLE_Q);
} }
} }

View File

@@ -21,7 +21,9 @@
#include "piece.h" #include "piece.h"
#include "move.h" #include "move.h"
bool pseudo_is_legal(pos_t *pos, move_t move); bool pseudo_is_legal(const pos_t *pos, const move_t move);
int gen_all_pseudomoves(pos_t *pos); move_t pos_next_legal(const pos_t *pos, int *start);
movelist_t *pos_all_legal(const pos_t *pos, movelist_t *dest);
int pos_gen_pseudomoves(pos_t *pos);
#endif /* MOVEGEN_H */ #endif /* MOVEGEN_H */

View File

@@ -89,8 +89,9 @@
*/ */
/** /**
* move_print() - print a move * move_str() - get a move string
* @pos: position * @dst: destination memory
* @move: move
* @flags: moves selection and display options. * @flags: moves selection and display options.
* *
* Possible flags are: * Possible flags are:
@@ -101,17 +102,40 @@
* M_PR_NL: print a newline after move * M_PR_NL: print a newline after move
* M_PR_EVAL: print move eval * M_PR_EVAL: print move eval
*/ */
void moves_print(pos_t *pos, __unused int flags) char *move_str(char *dst, const move_t move, __unused const int flags)
{ {
move_t *moves = pos->moves.move; square_t from = move_from(move);
int nmoves = pos->moves.nmoves; square_t to = move_to(move);
int len;
sprintf(dst, "%s%s%n", sq_to_string(from), sq_to_string(to), &len);
printf("%2d:", nmoves); if (is_promotion(move)) {
for (int m = 0; m < nmoves; ++m) { piece_t promoted = (piece_t) move_promoted(move);
square_t from = move_from(moves[m]); sprintf(dst + len, "%s", piece_to_low(promoted));
square_t to = move_to(moves[m]);
printf(" %s-%s", sq_to_string(from), sq_to_string(to));
} }
return dst;
}
/**
* moves_print() - print movelist moves.
* @moves: &movelist_t moves list
* @flags: moves selection and display options.
*
* Possible flags are:
* M_PR_CAPT: print move if capture
* M_PR_NCAPT: print move if non capture
* M_PR_NUM: print also move number
* M_PR_LONG: print long notation
* M_PR_NL: print a newline after each move
* M_PR_EVAL: print move eval
*/
void moves_print(movelist_t *moves, __unused int flags)
{
char str[16];
//printf("%2d:", moves->nmoves);
for (int m = 0; m < moves->nmoves; ++m)
printf("%s ", move_str(str, moves->move[m], flags));
printf("\n"); printf("\n");
} }
@@ -123,747 +147,26 @@ 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_type_t prom1 = move_promoted(m1);
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 */
if (t1 < t2) return -1; if (t1 < t2) return -1;
if (t1 > t2) return 1; if (t1 > t2) return 1;
/* t1 == t2 */
if (prom1 < prom2) return -1;
if (prom1 > prom2) return 1;
return 0; return 0;
} }
/** /**
* move_sort_by_sq() - sort moves by from/to squares ascending * move_sort_by_sq() - sort moves list by from/to squares ascending
* @pos: position. * @moves: &movelist_t
* *
* Used for perft, for easier comparison. * Used for perft, for easier comparison.
*/ */
void move_sort_by_sq(pos_t *pos) void move_sort_by_sq(movelist_t *moves)
{ {
qsort(pos->moves.move, pos->moves.nmoves, sizeof(move_t), _moves_cmp_bysquare); qsort(moves->move, moves->nmoves, sizeof(move_t), _moves_cmp_bysquare);
} }
/*
* static move_t *move_add(pos_t *pos, piece_t piece, square_t from,
* square_t to)
* {
* board_t *board = pos->board;
* move_t *move;
* int color = COLOR(pos->board[from].piece);
*
* # ifdef DEBUG_MOVE
* log_i(3, "piece_color=%d turn=%d from=%c%c to=%c%c\n",
* color, pos->turn,
* FILE2C(F88(from)),
* RANK2C(R88(from)),
* FILE2C(F88(to)),
* RANK2C(R88(to)));
* # endif
* /\* invalid position if opponent king is attacked
* *\/
* if (board[to].piece & KING) {
* # ifdef DEBUG_MOVE
* log_i(2, "invalid position: opponent king [%c%c] is in check.\n",
* FILE2C(F88(to)), RANK2C(R88(to)));
* # endif
* return NULL;
* }
* if (!(move = pool_get(moves_pool)))
* return NULL;
* list_add(&move->list, &pos->moves[color]);
*
* move->piece = piece | color;
* move->from = from;
* move->to = to;
* move->capture = board[to].piece;
* if (PIECE(move->capture) == KING)
* pos->check[color]++;
*
* move->flags = M_NORMAL;
* if (move->capture)
* move->flags |= M_CAPTURE;
* move->pos = NULL;
* # ifdef DEBUG_MOVE
* log_i(3, "added %s %s move from %c%c to %c%c\n",
* COLOR(move->piece)? "black": "white",
* P_NAME(PIECE(move->piece)),
* FILE2C(F88(move->from)), RANK2C(R88(move->from)),
* FILE2C(F88(move->to)), RANK2C(R88(move->to)));
* # endif
* return move;
* }
*
* /\**
* * move_del() - delete a move from list.
* * @ptr: move &list_head
* *
* * Remove the move whose 'list' element address is @ptr.
* *\/
* void move_del(struct list_head *ptr)
* {
* move_t *move = list_entry(ptr, move_t, list);
*
* # ifdef DEBUG_MOVE
* log_i(3, "delete move from %c%c to %c%c\n",
* FILE2C(F88(move->from)), RANK2C(R88(move->from)),
* FILE2C(F88(move->to)), RANK2C(R88(move->to)));
* # endif
*
* if (move->pos)
* pos_del(move->pos);
* list_del(ptr);
* pool_add(moves_pool, move);
* return;
* }
*
* /\**
* * move_del() - delete all position moves.
* * @ppos: &position.
* *
* * Remove all generated moves from @pos structure.
* *\/
* int moves_del(pos_t *pos)
* {
* struct list_head *p_cur, *tmp, *head;
* int count = 0;
*
* for (int color = WHITE; color <= BLACK; ++color) {
* head = &pos->moves[color];
*
* list_for_each_safe(p_cur, tmp, head) {
* move_del(p_cur);
* count++;
* }
* }
* # ifdef DEBUG_PIECE
* log_f(3, "%d moves removed\n", count);
* # endif
* return count;
* }
*
* /\* TODO: return nmoves *\/
* static move_t *move_pawn_add(pos_t *pos, piece_t piece, square_t from,
* square_t to, unsigned char rank7)
* {
* move_t *move;
* piece_t promote;
* unsigned char color = COLOR(piece);
*
* if (R88(from) == rank7) { /\* promotion *\/
* for (promote = QUEEN; promote > PAWN; promote >>= 1) {
* if ((move = move_add(pos, piece, from, to))) {
* move->flags |= M_PROMOTION;
* move->promotion = promote | color;
* }
* }
* } else {
* move = move_add(pos, piece, from, to);
* }
* return move;
* }
*
* /\**
* * pseudo_moves_pawn() - generate moves for pawn.
* * @pos: &position
* * @ppiece: &piece_list pawn structure pointer
* * @doit: add move to moves list
* *
* * Calculate all possible moves for @ppiece pawn.
* * If @doit is true, add moves to @pos' moves list.
* *\/
* int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit)
* {
* piece_t piece = PIECE(ppiece->piece);
* unsigned char color = COLOR(ppiece->piece);
* square_t square = ppiece->square, new;
* board_t *board = pos->board;
* unsigned char rank2, rank7, rank5;
* move_t *move = NULL;
* char dir;
* int count = 0;
*
* /\* setup direction *\/
* if (IS_WHITE(color)) {
* dir = 1;
* rank2 = 1;
* rank7 = 6;
* rank5 = 4;
* } else {
* dir = -1;
* rank2 = 6;
* rank7 = 1;
* rank5 = 3;
* }
*
* # ifdef DEBUG_MOVE
* log_f(2, "pos:%p turn:%s piece:%d [%s %s] dir:%d at %#04x[%c%c]\n",
* pos,
* IS_WHITE(pos->turn)? "white": "black",
* piece,
* IS_WHITE(color)? "white": "black",
* P_NAME(piece),
* dir,
* square,
* FILE2C(F88(square)), RANK2C(R88(square)));
* # endif
*
* /\* normal push. We do not test for valid destination square here,
* * assuming position is valid. Is that correct ?
* *\/
* new = square + dir * 16;
* if (!board[new].piece) {
* # ifdef DEBUG_MOVE
* log_i(4, "pushing pawn %#04x\n", square);
* # endif
* pos->mobility[color]++;
* count++;
* if (doit)
* move_pawn_add(pos, piece | color, square, new, rank7);
* /\* push 2 squares *\/
* log(4, "R88(%#x)=%d R2=%d \n", square, R88(square), rank2);
* if (R88(square) == rank2) {
* new += dir * 16;
* if (SQ88_OK(new) && !board[new].piece) {
* # ifdef DEBUG_MOVE
* log_i(4, "pushing pawn %#04x 2 squares\n", square);
* # endif
* //log_f(2, "pawn move2 mobility\n");
* pos->mobility[color]++;
* count++;
* if (doit)
* move_pawn_add(pos, piece | color, square, new, rank7);
* }
* }
* }
*
* /\* en passant - not accounted for mobility. Correct ? *\/
* if (pos->en_passant && R88(square) == rank5) {
* unsigned char ep_file = F88(pos->en_passant);
* unsigned char sq_file = F88(square);
*
* # ifdef DEBUG_MOVE
* log_i(4, "possible en passant on rank %#x (current = %#0x)\n", ep_file,
* sq_file);
* # endif
* if (sq_file == ep_file - 1 || sq_file == ep_file + 1) {
* square_t t_square = SQ88(ep_file, rank5); /\* taken pawn square *\/
* piece_t captured = board[t_square].piece;
* move = move_pawn_add(pos, piece | color , square, pos->en_passant, rank7);
* move->flags |= M_EN_PASSANT | M_CAPTURE;
* move->capture = captured;
* pos->mobility[color]++;
* count++;
* }
*
* }
*
* /\* capture *\/
* int two=0;
* for (new = square + dir * 15; two < 2; new = square + dir * 17, two++) {
* # ifdef DEBUG_MOVE
* log_i(4, "pawn capture %#04x %#04x\n", square, new);
* # endif
* if (SQ88_NOK(new))
* continue;
* pos->controlled[color] |= SQ88_2_BB(new);
* if (board[new].piece && COLOR(board[new].piece) != color) {
* if (PIECE(board[new].piece) == KING)
* pos->check[color]++;
* //log_f(2, "pawn capture mobility\n");
* pos->mobility[color]++;
* count++;
* if (doit)
* move_pawn_add(pos, piece | color, square, new, rank7);
* }
* }
* # ifdef DEBUG_MOVE
* log_f(2, "pos:%p turn:%s piece:%d [%s %s] dir:%d at %#04x[%c%c] count=%d\n",
* pos,
* IS_WHITE(pos->turn)? "white": "black",
* piece,
* IS_WHITE(color)? "white": "black",
* P_NAME(piece),
* dir,
* square,
* FILE2C(F88(square)), RANK2C(R88(square)), count);
* # endif
* return count;
* }
*
* /\**
* * pseudo_moves_castle() - generate castle moves.
* * @pos: &position
* * @color: side for which to generate moves
* * @doit: add move to moves list
* * @do_king: count king moves in mobility
* *
* * Calculate the possible castle moves for @color side.
* * If @doit is true, add moves to @pos' moves list.
* * If @do_king is true, account king moves (incl. castle) to mobility.
* *
* * @return: The number of possible king moves.
* *\/
* int pseudo_moves_castle(pos_t *pos, bool color, bool doit, bool do_king)
* {
* board_t *board = pos->board;
* unsigned char rank1, castle_K, castle_Q;
* move_t *move = NULL;
* unsigned short count=0;
* struct can_castle *can_castle;
* bitboard_t controlled;
* bitboard_t occupied = pos->occupied[WHITE] | pos->occupied[BLACK];
* //pos_t *newpos;
*
* # ifdef DEBUG_MOVE
* log_f(2, "pos:%p turn:%s color:%s\n",
* pos,
* IS_WHITE(pos->turn)? "white": "black",
* IS_WHITE(color)? "white": "black");
* # endif
*
* if (IS_WHITE(color)) {
* rank1 = 0;
* castle_K = pos->castle & CASTLE_WK;
* castle_Q = pos->castle & CASTLE_WQ;
* can_castle = castle_squares+WHITE;
* controlled = pos->controlled[BLACK];
* } else {
* rank1 = 7;
* castle_K = pos->castle & CASTLE_BK;
* castle_Q = pos->castle & CASTLE_BQ;
* can_castle = castle_squares+BLACK;
* controlled = pos->controlled[WHITE];
* }
* if (castle_K) {
* if (occupied & can_castle->occupied[1]) {
* # ifdef DEBUG_MOVE
* log(5, "Cannot castle K side: occupied\n");
* # endif
* goto next;
* }
* if (controlled & can_castle->controlled[1]) {
* # ifdef DEBUG_MOVE
* log(5, "Cannot castle K side: controlled\n");
* # endif
* goto next;
* }
* if (do_king) {
* pos->mobility[color]++;
* count++;
* }
* if (doit) {
* move = move_add(pos, board[SQ88(4, rank1)].piece,
* SQ88(4, rank1), SQ88(6, rank1));
* if (move)
* move->flags |= M_CASTLE_K;
* }
* }
*
* next:
* if (castle_Q) {
* if (occupied & can_castle->occupied[0]) {
* # ifdef DEBUG_MOVE
* log(5, "Cannot castle Q side: occupied\n");
* # endif
* goto end;
* }
* if (controlled & can_castle->controlled[0]) {
* # ifdef DEBUG_MOVE
* log(5, "Cannot castle Q side: controlled\n");
* # endif
* goto end;
* }
* if (do_king) {
* pos->mobility[color]++;
* count++;
* }
* if (doit) {
* move = move_add(pos, board[SQ88(4, rank1)].piece,
* SQ88(4, rank1), SQ88(2, rank1));
* if (move)
* move->flags |= M_CASTLE_Q;
* }
* }
* end:
* return count;
* }
*
* /\**
* * pseudo_moves_gen() - general move generation for non pawn pieces
* * @pos: &position
* * @ppiece: &piece_list structure pointer
* * @doit: add move to moves list
* * @do_king: count king moves
* *
* * Calculate all possible moves for @ppiece.
* * If @doit is true, add moves to @pos' moves list.
* * If @do_king is true, account king moves (incl. castle) to mobility.
* *\/
* int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit, bool do_king)
* {
* piece_t piece = PIECE(ppiece->piece);
* unsigned char color = COLOR(ppiece->piece);
* struct vector *vector = vectors+piece;
* square_t square = ppiece->square;
* unsigned char ndirs = vector->ndirs;
* char slide = vector->slide;
* board_t *board = pos->board;
* //move_t *move;
* int count = 0;
* u64 bb_new;
*
* # ifdef DEBUG_MOVE
* log_f(2, "pos:%p turn:%s piece:%d [%s %s] at %#04x[%c%c]\n",
* pos,
* IS_WHITE(pos->turn)? "white": "black",
* piece,
* IS_WHITE(color)? "white": "black",
* P_NAME(piece),
* square,
* FILE2C(F88(square)), RANK2C(R88(square)));
* log_i(5, "vector=%ld ndirs=%d slide=%d\n", vector-vectors, ndirs, slide);
* # endif
*
* for (int curdir = 0; curdir < ndirs; ++curdir) {
* char dir = vector->dir[curdir];
* for (square_t new = square + dir; ; new = new + dir) {
* /\* outside board *\/
* if (SQ88_NOK(new)) {
* # ifdef DEBUG_MOVE
* log_i(4,"skipping %04x (invalid square)\n", new);
* # endif
* break;
* }
* bb_new = SQ88_2_BB(new);
* # ifdef DEBUG_MOVE
* log_i(2, "trying %#x=%c%c bb=%#lx\n",
* new, FILE2C(F88(new)), RANK2C(R88(new)),
* bb_new);
* //bitboard_print(new_bitboard);
* # endif
*
* pos->controlled[color] |= bb_new;;
*
* /\* king: do not move to opponent controlled square *\/
* if (piece == KING && pos->controlled[OPPONENT(color)] & bb_new) {
* # ifdef DEBUG_MOVE
* log_i(2, "%s king cannot move to %c%c\n",
* IS_WHITE(color)? "white": "black",
* FILE2C(F88(new)), RANK2C(R88(new)));
* # endif
* break;
* }
*
* if (bb_new & pos->occupied[color]) {
* //bitboard_print(pos->occupied[color]);
* //bitboard_print(pos->occupied[OPPONENT(color)]);
* # ifdef DEBUG_MOVE
* log_i(2, "BB: skipping %#lx [%c%c] (same color piece)\n",
* bb_new, FILE2C(F88(new)), RANK2C(R88(new)));
* # endif
* break;
* }
*
* /\* we are sure the move is valid : we create move *\/
* if (piece != KING || do_king) {
* pos->mobility[color]++;
* count++;
* }
* if (doit)
* move_add(pos, ppiece->piece, square, new);
* if (board[new].piece) { /\* stopper move *\/
* break;
* }
* if (!slide)
* break;
* }
* }
* # ifdef DEBUG_MOVE
* log_f(2, "pos:%p turn:%s piece:%d [%s %s] at %#04x[%c%c] count=%d\n",
* pos,
* IS_WHITE(pos->turn)? "white": "black",
* piece,
* IS_WHITE(color)? "white": "black",
* P_NAME(piece),
* square,
* FILE2C(F88(square)), RANK2C(R88(square)), count);
* # endif
* return count;
* }
*
* /\**
* * moves_gen() - move generation for one color
* * @pos: &position
* * @color: side
* * @doit: add move to moves list
* * @do_king: count king moves
* *
* * Calculate all possible moves for @color.
* * If @doit is true, add moves to @pos' moves list.
* * If @do_king is true, account king moves (incl. castle) to mobility.
* *\/
* int moves_gen(pos_t *pos, bool color, bool doit, bool do_king)
* {
* struct list_head *p_cur, *tmp, *piece_list;
* piece_list_t *piece;
* int count = 0;
*
* # ifdef DEBUG_MOVE
* log_f(2, "color:%s doit:%d\n", color? "Black": "White", doit);
* # endif
*
* /\* do not generate moves if already done for color *\/
* if (!list_empty(&pos->moves[color]))
* doit = false;
*
* piece_list = &pos->pieces[color];
* pos->mobility[color] = 0;
* pos->controlled[color] = 0;
* count += pseudo_moves_castle(pos, color, doit, do_king);
* list_for_each_safe(p_cur, tmp, piece_list) {
* piece = list_entry(p_cur, piece_list_t, list);
* if (PIECE(piece->piece) != PAWN)
* count += pseudo_moves_gen(pos, piece, doit, do_king);
* else
* count += pseudo_moves_pawn(pos, piece, doit);
*
* count++;
* }
* return count;
* }
*
* /\**
* * moves_gen_king_moves() - adjust king mobility
* * @pos: &position
* * @color: king color
* * @doit: add move to moves list
* *
* * Compute the number of king moves (incl. castle), after opponent controlled
* * are known.
* * If @doit is true, add moves to @pos' moves list.
* *
* * @return: The number of possible king moves.
* *\/
* int moves_gen_king_moves(pos_t *pos, bool color, bool doit)
* {
* int count = 0;
* piece_list_t *king = list_first_entry(&pos->pieces[color], piece_list_t, list);
* count = pseudo_moves_castle(pos, king, doit, true);
* count += pseudo_moves_gen(pos, king, doit, true);
* return count;
* }
*
* static int moves_cmp_eval(__unused void *data, const struct list_head *h1, const struct list_head *h2)
* {
* move_t *m1 = list_entry(h1, move_t, list);
* move_t *m2 = list_entry(h2, move_t, list);
* return m2->eval_simple - m1->eval_simple;
* }
*
* /\**
* * moves_sort() sort - sort moves list, best eval first.
* * @pos: &position.
* *\/
* void moves_sort(pos_t *pos)
* {
* list_sort(NULL, &pos->moves[pos->turn], moves_cmp_eval);
* }
*
* /\**
* * moves_gen_all_eval_sort() - calculate/generate/sort moves for side to play.
* * @pos: &position
* *
* * Generate positions for each move for player to move.
* * For each of them generate opponents moves, calculate eval, and sort the moves list.
* *\/
* void moves_gen_eval_sort(pos_t *pos)
* {
* move_t *move;
* pos_t *newpos;
*
* moves_gen_all(pos);
*
* list_for_each_entry(move, &pos->moves[pos->turn], list) {
* newpos = move_do(pos, move);
* move->pos = newpos;
* //move_print(0, move, 0);
* move->eval_simple = eval_simple(newpos);
* newpos->eval_simple = move->eval_simple;
* }
* moves_sort(pos);
* //moves_print(pos, 0);
* }
*
* /\**
* * moves_gen_all() - calculate all moves, and generate moves for side to play.
* * @pos: &position
* *
* * Compute pseudo moves for both sides, and generate moves for player to move.
* *\/
* void moves_gen_all(pos_t *pos)
* {
* //log_f(1, "turn=%d opponent=%d\n", pos->turn, OPPONENT(pos->turn));
* if (!pos->moves_generated) {
* if (!pos->moves_counted) {}
* moves_gen(pos, OPPONENT(pos->turn), false, false);
* moves_gen(pos, pos->turn, true, true);
* if (!pos->moves_counted)
* moves_gen_king_moves(pos, OPPONENT(pos->turn), false);
* pos->moves_counted = true;
* pos->moves_generated = true;
* }
* }
*
* /\**
* * moves_gen_all_nomoves() - calculate number of moves for each player.
* * @pos: &position
* *\/
* void moves_gen_all_nomoves(pos_t *pos)
* {
* //log_f(1, "turn=%d opponent=%d\n", pos->turn, OPPONENT(pos->turn));
* if (!pos->moves_counted) {
* moves_gen(pos, OPPONENT(pos->turn), false, false);
* moves_gen(pos, pos->turn, false, true);
* moves_gen_king_moves(pos, OPPONENT(pos->turn), false);
* pos->moves_counted = true;
* }
* }
*
* /\**
* * move_do() - execute move in a duplicated position.
* * @pos: &pos_t struct on which move will be applied
* * @move: &move_t struct to apply
* *
* * @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);
* piece_t piece = PIECE(move->piece), newpiece = piece, captured = move->capture;
* int color = COLOR(move->piece);
* square_t from = move->from, to = move->to;
* u64 bb_from = SQ88_2_BB(from), bb_to = SQ88_2_BB(to);
*
* if (move->capture || piece == PAWN) /\* 50 moves *\/
* 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);
* }
*/

View File

@@ -19,58 +19,116 @@
#include "board.h" #include "board.h"
/* move structure: /* move structure:
* 3 2 2 1 1 1 1 1 1 * 3 3 2 2 1 1 1 1 1 1
* 1 5 4 8 7 5 4 2 1 6 5 0 * 1 0 4 3 8 7 5 4 2 1 6 5 0
* UUUUUUU FFFFFFF ppp ccc tttttt ffffff * S UUUUUUU FFFFFF ccc ppp tttttt ffffff
* *
* bits range type mask get desc * bits len off range type mask get desc
* ffffff 0-5 square_t 3f &63 from * ffffff 6 0 0-5 square_t 077 &077 from
* tttttt 6-11 square_t fc0 (>>6)&63 to * tttttt 6 6 6-11 square_t 07700 (>>6) &077 to
* ccc 12-14 piece_type_t 7000 (>>12)&7 captured * ppp 3 12 12-14 piece_type_t 070000 (>>12) &07 promoted
* ppp 15-17 piece_type_t 38000 (>>15)&7 promoted * ccc 3 15 15-17 piece_type_t 0700000 (>>15) &07 captured
* FFFFFFF 18-24 N/A 1fc0000 N/A flags * FFFFFF 6 18 18-23 move_flags_t 077000000 (>>18) &077 N/A flags
* UUUUUUU 25-31 unused fe000000 N/A future usage ? * UUUUUUU 7 24 24-30 unused 017700000000 future usage
* S 1 31 31-31 - 020000000000 sign
*/ */
#define move_t u32 typedef s32 move_t;
/* move flags */ /* special move_t values */
#define M_FLAGS_BEG 18 #define MOVE_NONE (-1)
#define M_HAS_FLAGS mask(M_FLAGS_BEG + 0) /* probably unused */ #define MOVE_NULL (0) /* hack: from = to = A1 */
#define M_CAPTURE mask(M_FLAGS_BEG + 1)
#define M_EPASSANT mask(M_FLAGS_BEG + 2)
#define M_PROMOTE mask(M_FLAGS_BEG + 3)
#define M_CASTLE_K mask(M_FLAGS_BEG + 4)
#define M_CASTLE_Q mask(M_FLAGS_BEG + 5)
#define M_CHECK mask(M_FLAGS_BEG + 6) /* probably unknown/useless */
#define M_FLAGS (M_CAPTURE | M_ENPASSANT | M_PROMOTION | \ enum {
M_CASTLE_K | M_CASTLE_Q | M_CHECK) M_OFF_FROM = 0,
#define M_NORMAL (~M_FLAGS) M_OFF_TO = 6,
M_OFF_PROMOTED = 12,
M_OFF_CAPTURED = 15,
M_OFF_FLAGS = 18
};
typedef enum {
M_CAPTURE = mask(M_OFF_FLAGS + 0),
M_ENPASSANT = mask(M_OFF_FLAGS + 1),
M_PROMOTION = mask(M_OFF_FLAGS + 2),
M_CASTLE_K = mask(M_OFF_FLAGS + 3), /* 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_DPUSH = mask(M_OFF_FLAGS + 7) /* pawn double push */
} move_flags_t;
#define move_set_flags(move, flags) ((move) | (flags))
#define is_capture(m) ((m) & M_CAPTURE)
#define is_enpassant(m) ((m) & M_ENPASSANT)
#define is_promotion(m) ((m) & M_PROMOTION)
#define is_castle(m) ((m) & (M_CASTLE_K | M_CASTLE_Q))
#define is_castle_K(m) ((m) & M_CASTLE_K)
#define is_castle_Q(m) ((m) & M_CASTLE_Q)
#define is_check(m) ((m) & M_CHECK)
#define is_dpush(m) ((m) & M_DPUSH)
#define MOVES_MAX 256 #define MOVES_MAX 256
static inline move_t move_make(square_t from, square_t to) typedef struct __movelist_s {
{ move_t move[MOVES_MAX];
return (to << 6) | from; int nmoves; /* total moves (fill) */
} } movelist_t;
static inline move_t move_make_flags(square_t from, square_t to, int flags)
{
return move_make(from, to) | flags;
}
static inline move_t move_make_promote(square_t from, square_t to, piece_type_t piece)
{
return move_make(from, to) | M_PROMOTE | (piece << 15);
}
static inline square_t move_from(move_t move) static inline square_t move_from(move_t move)
{ {
return move & 077; return move & 077;
} }
static inline square_t move_to(move_t move) static inline square_t move_to(move_t move)
{ {
return (move >> 6) & 077; return (move >> M_OFF_TO) & 077;
}
static inline piece_type_t move_promoted(move_t move)
{
return (move >> M_OFF_PROMOTED) & 07;
}
static inline piece_type_t move_captured(move_t move)
{
return (move >> M_OFF_CAPTURED) & 07;
}
static inline move_t move_make(square_t from, square_t to)
{
return (to << M_OFF_TO) | from;
}
static inline move_t move_make_flags(square_t from, square_t to, move_flags_t flags)
{
return move_set_flags(move_make(from, to), flags);
}
static inline move_t move_make_capture(square_t from, square_t to)
{
return move_make_flags(from, to, M_CAPTURE);
}
static inline move_t move_make_enpassant(square_t from, square_t to)
{
return move_make_flags(from, to, M_ENPASSANT);
}
static inline move_t move_make_promote(square_t from, square_t to,
piece_type_t promoted)
{
return move_make_flags(from, to, M_PROMOTION) | (promoted << M_OFF_PROMOTED);
}
static inline move_t move_make_promote_capture(square_t from, square_t to,
piece_type_t promoted)
{
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
@@ -79,38 +137,14 @@ static inline square_t move_to(move_t move)
#define M_PR_NCAPT 0x02 #define M_PR_NCAPT 0x02
#define M_PR_NUM 0x04 #define M_PR_NUM 0x04
#define M_PR_NL 0x08 #define M_PR_NL 0x08
#define M_UCI 0x10 /* UCI style - e7e8q */
#define M_PR_EVAL 0x20 /* separate captures */ #define M_PR_EVAL 0x20 /* separate captures */
#define M_PR_SEPARATE 0x40 /* separate captures */ #define M_PR_SEPARATE 0x40 /* separate captures */
#define M_PR_LONG 0x80 #define M_PR_LONG 0x80
typedef struct __movelist_s {
move_t move[MOVES_MAX];
int nmoves; /* total moves (fill) */
int curmove; /* current move (use) */
} movelist_t;
//pool_t *moves_pool_init();
//void moves_pool_stats();
//int move_print(int movenum, move_t *move, move_flags_t flags); //int move_print(int movenum, move_t *move, move_flags_t flags);
char *move_str(char *dst, const move_t move, __unused const int flags);
extern void moves_print(pos_t *pos, int flags); void moves_print(movelist_t *moves, int flags);
extern void move_sort_by_sq(pos_t *pos); void move_sort_by_sq(movelist_t *moves);
//extern int pseudo_moves_castle(pos_t *pos, bool color, bool doit, bool do_king);
//int pseudo_moves_gen(pos_t *pos, piece_list_t *piece, bool doit, bool do_king);
//int pseudo_moves_pawn(pos_t *pos, piece_list_t *piece, bool doit);
//extern int moves_gen(pos_t *pos, bool color, bool doit, bool do_king);
//extern int moves_gen_king_moves(pos_t *pos, bool color, bool doit);
// extern void moves_sort(pos_t *pos);
//extern void moves_gen_eval_sort(pos_t *pos);
//extern void moves_gen_all(pos_t *pos);
//extern void moves_gen_all_nomoves(pos_t *pos);
//extern pos_t *move_do(pos_t *pos, move_t *move);
//extern void move_undo(pos_t *pos, move_t *move);
#endif /* MOVE_H */ #endif /* MOVE_H */

View File

@@ -25,22 +25,22 @@
* piece_details * piece_details
*/ */
const struct piece_details piece_details[PIECE_MAX] = { const struct piece_details piece_details[PIECE_MAX] = {
/* Abb Fen Sym SymC Name start value */ /* cap low fen sym name values */
[EMPTY] = { "", "", "", "", "", 0, 0, 0 }, [EMPTY] = { "", "", "", "", "", 0, 0, 0 },
[W_PAWN] = { "", "P", "", "", "Pawn", P_VAL_OPN, P_VAL_MID, P_VAL_END }, [W_PAWN] = { "", "", "P", "", "Pawn", P_VAL_OPN, P_VAL_MID, P_VAL_END },
[W_KNIGHT] = { "N", "N", "", "", "Knight", N_VAL_OPN, N_VAL_MID, N_VAL_END }, [W_KNIGHT] = { "N", "n", "N", "", "Knight", N_VAL_OPN, N_VAL_MID, N_VAL_END },
[W_BISHOP] = { "B", "B", "", "", "Bishop", B_VAL_OPN, B_VAL_MID, B_VAL_END }, [W_BISHOP] = { "B", "b", "B", "", "Bishop", B_VAL_OPN, B_VAL_MID, B_VAL_END },
[W_ROOK] = { "R", "R", "", "", "Rook", R_VAL_OPN, R_VAL_MID, R_VAL_END }, [W_ROOK] = { "R", "r", "R", "", "Rook", R_VAL_OPN, R_VAL_MID, R_VAL_END },
[W_QUEEN] = { "Q", "Q", "", "", "Queen", Q_VAL_OPN, Q_VAL_MID, Q_VAL_END }, [W_QUEEN] = { "Q", "q", "Q", "", "Queen", Q_VAL_OPN, Q_VAL_MID, Q_VAL_END },
[W_KING] = { "K", "K", "", "", "King", K_VAL_OPN, K_VAL_MID, K_VAL_END }, [W_KING] = { "K", "k", "K", "", "King", K_VAL_OPN, K_VAL_MID, K_VAL_END },
[7] = { "", "", "", "", "", 0, 0, 0 }, [7] = { "", "", "", "", "", 0, 0, 0 },
[8] = { "", "", "", "", "", 0, 0, 0 }, [8] = { "", "", "", "", "", 0, 0, 0 },
[B_PAWN] = { "", "p", "", "", "Pawn", P_VAL_OPN, P_VAL_MID, P_VAL_END }, [B_PAWN] = { "", "", "p", "", "Pawn", P_VAL_OPN, P_VAL_MID, P_VAL_END },
[B_KNIGHT] = { "N", "n", "", "", "Knight", P_VAL_OPN, N_VAL_MID, N_VAL_END }, [B_KNIGHT] = { "N", "n", "n", "", "Knight", P_VAL_OPN, N_VAL_MID, N_VAL_END },
[B_BISHOP] = { "B", "b", "", "", "Bishop", P_VAL_OPN, B_VAL_MID, B_VAL_END }, [B_BISHOP] = { "B", "b", "b", "", "Bishop", P_VAL_OPN, B_VAL_MID, B_VAL_END },
[B_ROOK] = { "R", "r", "", "", "Rook", P_VAL_OPN, R_VAL_MID, R_VAL_END }, [B_ROOK] = { "R", "r", "r", "", "Rook", P_VAL_OPN, R_VAL_MID, R_VAL_END },
[B_QUEEN] = { "Q", "q", "", "", "Queen", P_VAL_OPN, Q_VAL_MID, Q_VAL_END }, [B_QUEEN] = { "Q", "q", "q", "", "Queen", P_VAL_OPN, Q_VAL_MID, Q_VAL_END },
[B_KING] = { "K", "k", "", "", "King", P_VAL_OPN, K_VAL_MID, K_VAL_END }, [B_KING] = { "K", "k", "k", "", "King", P_VAL_OPN, K_VAL_MID, K_VAL_END },
}; };
const char pieces_str[6+6+1] = "PNBRQKpnbrqk"; const char pieces_str[6+6+1] = "PNBRQKpnbrqk";
@@ -51,24 +51,24 @@ bool piece_ok(piece_t p)
return !(p & ~(MASK_COLOR | MASK_PIECE)) && pt && (pt <= KING); return !(p & ~(MASK_COLOR | MASK_PIECE)) && pt && (pt <= KING);
} }
char *piece_to_char(piece_t p) char *piece_to_cap(piece_t p)
{ {
return piece_details[p].abbr; return piece_details[p].cap;
} }
char *piece_to_char_color(piece_t p) char *piece_to_char(piece_t p)
{ {
return piece_details[p].c_abbr; return piece_details[p].fen;
}
char *piece_to_low(piece_t p)
{
return piece_details[p].low;
} }
char *piece_to_sym(piece_t p) char *piece_to_sym(piece_t p)
{ {
return piece_details[PIECE(p)].sym; return piece_details[p].sym;
}
char *piece_to_sym_color(piece_t p)
{
return piece_details[p].c_sym;
} }
char *piece_to_name(piece_t p) char *piece_to_name(piece_t p)
@@ -76,98 +76,20 @@ char *piece_to_name(piece_t p)
return piece_details[p].name; return piece_details[p].name;
} }
piece_t char_to_piece(char c) piece_type_t piece_t_from_char(char c)
{ {
char *p = strchr(pieces_str, c); char *p = strchr(pieces_str, c);
return p? (p - pieces_str) % 6 + 1: EMPTY; return p? (p - pieces_str) % 6 + 1: NO_PIECE_TYPE;
} }
piece_t char_color_to_piece(char c) //piece_type_t piece_from_promotion(char c, color_t color)
//{
// piece_type_t piece = piece_t_from_char(c);
// return piece? SET_COLOR()p? (p - pieces_str) % 6 + 1: NO_PIECE_TYPE;
//}
piece_t piece_from_char(char c)
{ {
piece_t piece = char_to_piece(c); piece_type_t piece = piece_t_from_char(c);
return isupper(c)? piece: SET_BLACK(piece); return isupper(c)? SET_WHITE(piece): SET_BLACK(piece);
} }
/*
* void piece_list_print(struct list_head *list)
* {
* struct list_head *p_cur, *tmp;
* piece_list_t *piece;
*
* list_for_each_safe(p_cur, tmp, list) {
* piece = list_entry(p_cur, piece_list_t, list);
*
* printf("%s%c%c ", P_SYM(piece->piece),
* FILE2C(F88(piece->square)),
* RANK2C(R88(piece->square)));
* }
* printf("\n");
* }
*
* pool_t *piece_pool_init()
* {
* if (!pieces_pool)
* pieces_pool = pool_create("pieces", 128, sizeof(piece_list_t));
* return pieces_pool;
* }
*
* void piece_pool_stats()
* {
* if (pieces_pool)
* pool_stats(pieces_pool);
* }
*
* piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square)
* {
* piece_list_t *new;
* short color = COLOR(piece);
*
* # ifdef DEBUG_PIECE
* log_f(3, "piece=%02x square=%02x\n", piece, square);
* log_f(5, "Adding %s %s on %c%c\n", color? "Black": "White",
* P_NAME(piece), FILE2C(F88(square)), RANK2C(R88(square)));
* # endif
* if ((new = pool_get(pieces_pool))) {
* /\* first piece is always king *\/
* if (PIECE(piece) == KING)
* list_add(&new->list, &pos->pieces[color]);
* else
* list_add_tail(&new->list, &pos->pieces[color]);
* new->piece = piece;
* new->square = square;
* new->castle = 0;
* new-> value = piece_details[PIECE(piece)].value;
* }
*
* return new;
* }
*
* void piece_del(struct list_head *ptr)
* {
* piece_list_t *piece = list_entry(ptr, piece_list_t, list);
* # ifdef DEBUG_PIECE
* log_f(3, "piece=%02x square=%02x\n", piece->piece, piece->square);
* # endif
* list_del(ptr);
* pool_add(pieces_pool, piece);
* return;
* }
*
* int pieces_del(pos_t *pos, short color)
* {
* struct list_head *p_cur, *tmp, *head;
* int count = 0;
*
* head = &pos->pieces[color];
*
* list_for_each_safe(p_cur, tmp, head) {
* piece_del(p_cur);
* count++;
* }
* # ifdef DEBUG_PIECE
* log_f(3, "color=%d removed=%d\n", color, count);
* # endif
* return count;
* }
*/

View File

@@ -31,6 +31,7 @@ typedef enum {
typedef enum { typedef enum {
ALL_PIECES = 0, /* 'all pieces' bitboard */ ALL_PIECES = 0, /* 'all pieces' bitboard */
NO_PIECE_TYPE = 0,
PAWN = 1, KNIGHT, BISHOP, ROOK, QUEEN, KING, PAWN = 1, KNIGHT, BISHOP, ROOK, QUEEN, KING,
PIECE_TYPE_MAX = 7 /* bit 4 */ PIECE_TYPE_MAX = 7 /* bit 4 */
} piece_type_t; } piece_type_t;
@@ -80,10 +81,10 @@ typedef enum __piece_e {
* s64 end_value; * s64 end_value;
*/ */
extern const struct piece_details { extern const struct piece_details {
char *abbr; /* used for game notation */ char *cap; /* used for game notation */
char *c_abbr; /* lowercase = black */ char *low; /* used also for UCI promotion */
char *sym; /* used for game notation */ char *fen; /* cap=white, low=black */
char *c_sym; /* different W & B */ char *sym; /* UTF-8 symbol */
char *name; /* piece name */ char *name; /* piece name */
s64 opn_value; /* value opening */ s64 opn_value; /* value opening */
s64 mid_value; /* value midgame */ s64 mid_value; /* value midgame */
@@ -104,20 +105,26 @@ extern const char pieces_str[6+6+1]; /* to search from fen/user inp
#define IS_WHITE(p) (!COLOR(p)) #define IS_WHITE(p) (!COLOR(p))
#define IS_BLACK(p) (COLOR(p)) #define IS_BLACK(p) (COLOR(p))
#define SET_WHITE(p) ((p) &= ~MASK_COLOR) #define SET_WHITE(p) (piece_t)((p) &= ~MASK_COLOR)
#define SET_BLACK(p) ((p) |= MASK_COLOR) #define SET_BLACK(p) (piece_t)((p) |= MASK_COLOR)
#define SET_COLOR(p, c) (!(c)? SET_WHITE(p): SET_BLACK(p)) #define SET_COLOR(p, c) (piece_t)(!(c)? SET_WHITE(p): SET_BLACK(p))
extern bool piece_ok(piece_t p); bool piece_ok(piece_t p);
extern char *piece_to_char(piece_t p); char *piece_to_cap(piece_t p);
extern char *piece_to_char_color(piece_t p); char *piece_to_low(piece_t p);
extern char *piece_to_sym(piece_t p); char *piece_to_fen(piece_t p);
extern char *piece_to_sym_color(piece_t p); char *piece_to_sym(piece_t p);
extern char *piece_to_name(piece_t p); char *piece_to_name(piece_t p);
extern piece_t char_to_piece(char c); #define piece_to_char(c) piece_to_fen(c)
extern piece_t char_color_to_piece(char c); //#define piece_to_char_t(p) piece_to_uci(p)
//piece_type_t char_to_piece(char c);
piece_type_t piece_t_from_char(char c);
piece_t piece_from_fen(char c);
#define piece_from_char(c) piece_from_fen(c)
/* use short name or symbol - no effect /* use short name or symbol - no effect
*/ */

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)
{ {
@@ -95,27 +83,92 @@ pos_t *pos_clear(pos_t *pos)
# endif # endif
pos->node_count = 0; pos->node_count = 0;
pos->turn = WHITE; pos->turn = WHITE;
pos->clock_50 = 0;
pos->plycount = 0; /* 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->plycount = 0;
pos->captured = NO_PIECE;
for (square_t sq = A1; sq <= H8; ++sq)
pos->board[sq] = EMPTY;
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;
} }
for (square_t sq = A1; sq <= H8; ++sq)
pos->board[sq] = EMPTY;
pos->moves.curmove = 0;
pos->moves.nmoves = 0;
pos->checkers = 0; pos->checkers = 0;
pos->pinners = 0; pos->pinners = 0;
pos->blockers = 0;
//pos->moves.curmove = 0;
pos->moves.nmoves = 0;
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
@@ -128,60 +181,89 @@ pos_t *pos_clear(pos_t *pos)
*/ */
bitboard_t pos_checkers(const pos_t *pos, const color_t color) bitboard_t pos_checkers(const pos_t *pos, const color_t color)
{ {
return sq_attackers(pos, pos->king[color], OPPONENT(color)); bitboard_t occ = pos_occ(pos);
return sq_attackers(pos, occ, pos->king[color], OPPONENT(color));
} }
/** /**
* pos_pinners() - find all pinners on a king. * pos_king_pinners() - get the "pinners" on a king "pinners".
* @pos: &position * @pos: &position
* @color: king color * @color: king color.
* *
* Get a bitboard of all pinners on @color king. * get position "pinners" on @color king. Here, pinner means a piece separated from king
* Just a wrapper over @sq_pinners(). * by one piece (any color) only.
* This is just a wrapper over @sq_pinners().
* *
* @return: a bitboard of pinners. * @return: pinners bitboard.
*/ */
bitboard_t pos_pinners(const pos_t *pos, const color_t color) bitboard_t pos_king_pinners(const pos_t *pos, const color_t color)
{ {
return sq_pinners(pos, pos->king[color], OPPONENT(color)); return sq_pinners(pos, pos->king[color], OPPONENT(pos->turn));
} }
/** /**
* pos_check() - extensive position consistenci check. * pos_king_blockers() - get the pin blockers on a king.
* @pos: &position
* @color: king color.
* @pinners: pinners bitboard.
*
* get @pinners blockers pieces on @color king.
*
* @return: blockers bitboard.
*/
bitboard_t pos_king_blockers(const pos_t *pos, const color_t color, const bitboard_t pinners)
{
bitboard_t tmp, blockers = 0, occ = pos_occ(pos);
square_t pinner, king = pos->king[color];
bit_for_each64(pinner, tmp, pinners) {
bitboard_t blocker = bb_between_excl[pinner][king] & occ;
warn_on(popcount64(blocker) != 1);
if (popcount64(blocker) != 1) {
printf("n blockers = %d\n", popcount64(blocker));
bb_print("blockers", blocker);
}
blockers |= blocker;
}
return blockers;
}
/**
* pos_check() - extensive position consistency check.
* @pos: &position * @pos: &position
* @strict: if not zero, call bug_on() on any error. * @strict: if true, call bug_on() on any error.
* *
* Check (hopefully) if position is valid: * Perform some validity check on position @pos:
* - pawns on first or 8th rank * - pawns on 1st or 8th rank
* - number of pawns per color > 8 * - number of pawns > 8 (per color)
* - total number of pieces per color > 16 or zero * - total number of pieces > 16 or zero (per color)
* - number of kings per color != 1 * - number of kings != 1 (per color)
* - discrepancy between bitboards per piece and ALL_PIECES per color * - discrepancy between board and king (per color)
* - discrepancy between bitboards and board * - discrepancy between piece bitboards and ALL_PIECES bitboards (per color)
* - side-to-move already checking opponent king. * - discrepancy between bitboards and board (per color)
* - side to move in check more than twice * - side-to-move already checking opponent king
* - side-to-move in check more than twice
* - kings distance is 1
* *
* In case of errors, and @abort is true, @bug_on() is called, and program will * In case of errors, and @strict is true, @bug_on() is called, and program will
* be terminated. * be terminated.
* This function should be called with @abort == 0 during initialization phase * This function should be called with @strict == false during initialization phase
* (eg after fen parsing), and with @abort != 0 otherwise (as we have some data * (eg after fen parsing), and with @strict == true otherwise (as we have some data
* corruption). * corruption).
* *
* TODO: add more checks: * TODO: add more checks:
* - kings attacking each other * - kings attacking each other
* *
* @Return: 0 if no error detected * @Return: Number of detected error (only if @strict is false).
* the number of detected error if @abort == 0.
* this function does not return if @abort != 0 and errors are found.
*/ */
int pos_check(const pos_t *pos, const int fatal) int pos_check(const pos_t *pos, const bool strict)
{ {
int n, count = 0, bbcount = 0, error = 0; int n, count = 0, bbcount = 0, error = 0;
bitboard_t tmp;
/* pawns on 1st ot 8th rank */ /* pawns on 1st ot 8th rank */
n = popcount64((pos->bb[WHITE][PAWN] | pos->bb[BLACK][PAWN]) & tmp = (pos->bb[WHITE][PAWN] | pos->bb[BLACK][PAWN]) & (RANK_1bb | RANK_8bb);
(RANK_1bb | RANK_8bb)); error += warn_on(tmp);
error += warn_on(n != 0);
for (color_t color = WHITE; color <= BLACK; ++color) { for (color_t color = WHITE; color <= BLACK; ++color) {
/* pawn count */ /* pawn count */
@@ -190,6 +272,8 @@ int pos_check(const pos_t *pos, const int fatal)
/* king count */ /* king count */
n = popcount64(pos->bb[color][KING]); n = popcount64(pos->bb[color][KING]);
error += warn_on(n != 1); error += warn_on(n != 1);
/* king mismatch with board */
error += warn_on(PIECE(pos->board[pos->king[color]]) != KING);
/* pieces count */ /* pieces count */
n = popcount64(pos->bb[color][ALL_PIECES]); n = popcount64(pos->bb[color][ALL_PIECES]);
error += warn_on(n == 0 || n > 16); error += warn_on(n == 0 || n > 16);
@@ -212,8 +296,10 @@ int pos_check(const pos_t *pos, const int fatal)
error += warn_on(pos_checkers(pos, OPPONENT(pos->turn))); error += warn_on(pos_checkers(pos, OPPONENT(pos->turn)));
/* is color to play in check more than twice ? */ /* is color to play in check more than twice ? */
error += warn_on(popcount64(pos_checkers(pos, OPPONENT(pos->turn))) > 2); error += warn_on(popcount64(pos_checkers(pos, OPPONENT(pos->turn))) > 2);
/* kings distance is less than 2 */
error += warn_on(sq_dist(pos->king[WHITE], pos->king[BLACK]) < 2);
bug_on(fatal && error); bug_on(strict && error);
return error; return error;
} }
@@ -229,6 +315,7 @@ void pos_print(const pos_t *pos)
printf("fen %s\n", pos2fen(pos, str)); printf("fen %s\n", pos2fen(pos, str));
printf("checkers: %s\n", pos_checkers2str(pos, str, sizeof(str))); printf("checkers: %s\n", pos_checkers2str(pos, str, sizeof(str)));
printf("pinners : %s\n", pos_pinners2str(pos, str, sizeof(str))); printf("pinners : %s\n", pos_pinners2str(pos, str, sizeof(str)));
printf("blockers: %s\n", pos_blockers2str(pos, str, sizeof(str)));
} }
/** /**
@@ -261,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;
@@ -270,15 +357,14 @@ 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_char(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++;
} }
} }
printf(" "); printf(" ");
} }

View File

@@ -18,6 +18,7 @@
#include "brlib.h" #include "brlib.h"
#include "bitops.h" #include "bitops.h"
#include "struct-group.h"
#include "chessdefs.h" #include "chessdefs.h"
#include "bitboard.h" #include "bitboard.h"
@@ -28,21 +29,36 @@
typedef struct __pos_s { typedef struct __pos_s {
u64 node_count; /* evaluated nodes */ u64 node_count; /* evaluated nodes */
int turn; /* WHITE or BLACK */ int turn; /* WHITE or BLACK */
u16 clock_50;
u16 plycount; /* plies so far, start is 0 */
square_t king[2]; /* dup with bb, faster retrieval */ /* data which cannot be recovered by move_undo
square_t en_passant; * following data can be accessed either directly, either via "movesave"
castle_rights_t castle; * structure name.
* For example, pos->en_passant and pos->state.en_passant are the same.
* This allows a memcpy on this data (to save/restore position state).
*/
struct_group_tagged(state_s, state,
square_t en_passant;
castle_rights_t castle;
u16 clock_50;
u16 plycount; /* plies so far, start from 1 */
piece_t captured; /* only for move_undo */
);
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 */
bitboard_t checkers; /* opponent checkers */ bitboard_t checkers; /* opponent checkers */
bitboard_t pinners; /* opponent pinners */ bitboard_t pinners; /* opponent pinners */
piece_t board[BOARDSIZE]; bitboard_t blockers; /* pieces blocking pin */
movelist_t moves; movelist_t moves;
} pos_t; } pos_t;
typedef struct state_s state_t;
#define pos_pinned(p) (p->blockers & p->bb[p->turn][ALL_PIECES])
/** /**
* pos_set_sq - unconditionally set a piece on a square * pos_set_sq - unconditionally set a piece on a square
* @pos: position * @pos: position
@@ -77,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;
} }
/** /**
@@ -115,12 +133,12 @@ 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));
} }
/** /**
* pos_checkers2str() - get of string of checkers. * pos_checkers2str() - get of string of checkers.
* @pos: position * @p: position
* @str: destination string * @str: destination string
* @len: max @str len. * @len: max @str len.
* *
@@ -128,29 +146,33 @@ static __always_inline int pos_between_count(const pos_t *pos,
* *
* @return: @str. * @return: @str.
*/ */
#define pos_checkers2str(pos, str, len) bb_sq2str((pos)->checkers, (str), (len)) #define pos_checkers2str(p, str, len) bb_sq2str(p->checkers, str, len)
#define pos_pinners2str(pos, str, len) bb_sq2str((pos)->pinners, (str), (len)) #define pos_pinners2str(p, str, len) bb_sq2str(p->pinners, str, len)
#define pos_blockers2str(p, str, len) bb_sq2str(p->blockers, str, len)
//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_pinners(const pos_t *pos, const color_t color); bitboard_t pos_king_pinners(const pos_t *pos, const color_t color);
//extern char *pos_checkers2str(const pos_t *pos, char *str); bitboard_t pos_king_blockers(const pos_t *pos, const color_t color, const bitboard_t );
//extern char *pos_pinners2str(const pos_t *pos, char *str); //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 int 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 */

50
test/attack-test.c Normal file
View File

@@ -0,0 +1,50 @@
/* attack-test.c - basic square attack 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-gen.h"
#include "attack.h"
#include "common-test.h"
int main(int __unused ac, __unused char**av)
{
int i = 0;
char *fen;
pos_t *pos;//, *fishpos = pos_new();
setlinebuf(stdout); /* line-buffered stdout */
bitboard_init();
hyperbola_init();
while ((fen = next_fen(ATTACK))) {
//printf(">>>>> %s\n", test[i]);
printf("original fen %d: [%p][%s]\n", i, fen, fen);
if (!(pos = fen2pos(NULL, fen))) {
printf("wrong fen %d: [%s]\n", i, fen);
continue;
}
pos_print(pos);
pos_del(pos);
i++;
}
return 0;
}

View File

@@ -14,11 +14,12 @@
#include <stdio.h> #include <stdio.h>
#include "chessdefs.h" #include "chessdefs.h"
/* when below FENs are in a struct with selection per test */ /* when below FENs are in a struct with selection per test */
#define NOTEST 0 #define NOTEST 0
#define FEN 1 #define FEN 1
#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;
@@ -35,6 +36,41 @@ struct fentest {
*/ */
//"4k3/pppppppp/8/8/8/8/PPPPPPPP/2BRK3 w - - 0 1", //"4k3/pppppppp/8/8/8/8/PPPPPPPP/2BRK3 w - - 0 1",
//"4k3/pppppppp/8/8/8/8/PPPPPPPP/1B1R1K2 w - - 0 1", //"4k3/pppppppp/8/8/8/8/PPPPPPPP/1B1R1K2 w - - 0 1",
{ MOVEGEN,
"illegal white e.p.",
"3k4/8/8/2qpPK2/8/8/8/8 w - d6 0 1",
},
{ MOVEGEN,
"illegal black e.p.",
"8/8/8/8/2QPpk2/8/8/3K4 b - d3 0 1",
},
{ MOVEGEN,
"illegal white e.p.",
"3k4/8/5K2/3pP3/8/2b5/8/8 w - d6 0 1",
},
{ MOVEGEN,
"illegal black e.p.",
"8/8/2B5/8/3Pp3/5k2/8/3K4 b - d3 0 1",
},
{ MOVEGEN,
"legal white e.p.",
"1K1k4/8/8/3pP3/8/6b1/8/8 w - d6 0 1",
},
{ MOVEGEN,
"legal black e.p.",
"8/8/6B1/8/3Pp3/8/8/1k1K4 b - d3 0 1",
},
{ MOVEGEN,
"white mate.",
"1nbqkbn1/ppp1pppp/8/r1rpP1K1/8/8/PPPP1PPP/RNBQ1BNR w - d6 0 1",
},
{ MOVEGEN,
"illegal e.p.",
"1nbqkbn1/ppp1pppp/8/r1rpP1K1/8/8/PPPP1PPP/RNBQ1BNR w - d6 0 1",
},
{ ATTACK, { ATTACK,
"checkers: a1 h1", "checkers: a1 h1",
"1k6/8/8/8/8/8/8/r2K3r w - - 1 1" "1k6/8/8/8/8/8/8/r2K3r w - - 1 1"
@@ -61,10 +97,6 @@ struct fentest {
"checkers: g2 g7", "checkers: g2 g7",
"8/6R1/8/6k1/8/8/K5R1/8 b - - 1 1" "8/6R1/8/6k1/8/8/K5R1/8 b - - 1 1"
}, },
{ ATTACK,
"checkers: d5 e3, pinners: a1 h1 a4 h5",
"3k4/8/8/3r3b/b7/1N2n3/4B3/rN1K1R1r w - - 1 0"
},
{ ATTACK, { ATTACK,
"checkers: d5 e3, pinners: none (2 pieces between attacker & K)", "checkers: d5 e3, pinners: none (2 pieces between attacker & K)",
"3k4/8/8/3r3b/b7/1N2nn2/2n1B3/rNBK1Rbr w - - 1 1" "3k4/8/8/3r3b/b7/1N2nn2/2n1B3/rNBK1Rbr w - - 1 1"
@@ -167,7 +199,7 @@ struct fentest {
"6k1/6pp/R2p4/p1p5/8/1P1r3P/6P1/6K1 b - - 3 3" "6k1/6pp/R2p4/p1p5/8/1P1r3P/6P1/6K1 b - - 3 3"
}, },
// below tests are from: // some of tests below are from:
// - Rodent IV // - Rodent IV
// - https://www.chessprogramming.net/perfect-perft/ // - https://www.chessprogramming.net/perfect-perft/
{ FEN | MOVEGEN, { FEN | MOVEGEN,
@@ -178,6 +210,10 @@ struct fentest {
"", "",
"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1" "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1"
}, },
{ FEN | MOVEGEN,
"",
"8/2p5/3p4/Kp5r/1R3p1k/8/4P1P1/8 w - - 0 1"
},
{ FEN | MOVEGEN, { FEN | MOVEGEN,
"", "",
"4rrk1/pp1n3p/3q2pQ/2p1pb2/2PP4/2P3N1/P2B2PP/4RRK1 b - - 7 19" "4rrk1/pp1n3p/3q2pQ/2p1pb2/2PP4/2P3N1/P2B2PP/4RRK1 b - - 7 19"
@@ -332,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 }
}; };

View File

@@ -31,17 +31,19 @@ int main(__unused int ac, __unused char**av)
bitboard_init(); bitboard_init();
while ((fen = next_fen(FEN))) { while ((fen = next_fen(FEN))) {
printf("***** [%s] ", fen);
fflush(stdout);
if (!(pos = fen2pos(NULL, fen))) { if (!(pos = fen2pos(NULL, fen))) {
printf("[%s] **INVALID\n", fen); printf("**INVALID\n");
} else { } else {
pos_print(pos); //pos_print(pos);
pos2fen(pos, revfen); pos2fen(pos, revfen);
if (!strcmp(fen, revfen)) { if (!strcmp(fen, revfen)) {
printf("[%s] OK\n", fen); printf("OK\n");
} else { } else {
//printf("fen = [%s]\nrev = [%s]", fen, revfen); //printf("fen = [%s]\nrev = [%s]", fen, revfen);
//pos_print_raw(pos, 1); printf("-> [%s] **FIXED\n",revfen);
printf("[%s] -> [%s] **FIXED\n", fen, revfen); pos_print_raw(pos, 1);
} }
pos_del(pos); pos_del(pos);
} }

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;
}

View File

@@ -88,7 +88,7 @@ static void send_stockfish_fen(FILE *desc, pos_t *pos, char *fen)
move_t *moves = pos->moves.move; move_t *moves = pos->moves.move;
int nmoves = pos->moves.nmoves; int nmoves = pos->moves.nmoves;
//char nodescount[] = "Nodes searched"; //char nodescount[] = "Nodes searched";
printf("nmoves = %d\n", nmoves); //printf("nmoves = %d\n", nmoves);
fflush(stdout); fflush(stdout);
//sprintf(str, "stockfish \"position fen %s\ngo perft depth\n\"", fen); //sprintf(str, "stockfish \"position fen %s\ngo perft depth\n\"", fen);
fprintf(desc, "position fen %s\ngo perft 1\n", fen); fprintf(desc, "position fen %s\ngo perft 1\n", fen);
@@ -111,22 +111,46 @@ static void send_stockfish_fen(FILE *desc, pos_t *pos, char *fen)
// count); // count);
moves[nmoves++] = move_make(from, to); moves[nmoves++] = move_make(from, to);
} else if (sscanf(buf, "%*5s: %d", &count) == 1) {
square_t from = sq_from_string(buf);
square_t to = sq_from_string(buf + 2);
piece_type_t promoted = piece_t_from_char(*(buf + 4));
mycount += count;
//printf("move found: %c%c->%c%c %s->%s count=%d\n",
// buf[0], buf[1], buf[2], buf[3],
// sq_to_string(from), sq_to_string(to),
// count);
moves[nmoves++] = move_make_promote(from, to, promoted);
} }
} }
pos->moves.nmoves = nmoves; pos->moves.nmoves = nmoves;
// printf("fishcount=%d mycount=%d\n", fishcount, mycount); // printf("fishcount=%d mycount=%d\n", fishcount, mycount);
free(buf); free(buf);
} }
static void compare_moves(pos_t *fish, pos_t *me) static __unused bool movelists_equal(movelist_t *fish, movelist_t *me)
{
move_t *m1 = fish->move, *m2 = me->move;
int n1 = fish->nmoves, n2 = me->nmoves;
int mask = 077777;
if (n1 != n2)
return false;
for (int cur = 0; cur < n1; ++cur) {
if ((m1[cur] & mask) != (m2[cur] & mask))
return false;
}
return true;
}
static __unused void compare_moves(movelist_t *fish, movelist_t *me)
{ {
char str1[1024] = {0}, str2[1024] = {0}, tmpstr[1024]; char str1[1024] = {0}, str2[1024] = {0}, tmpstr[1024];
char *skip = " "; char *skip = " ";
move_t *m1 = fish->moves.move; move_t *m1 = fish->move;
move_t *m2 = me->moves.move; move_t *m2 = me->move;
int n1 = fish->moves.nmoves; int n1 = fish->nmoves;
int n2 = me->moves.nmoves; int n2 = me->nmoves;
#define f(c) move_from(c) #define f(c) move_from(c)
#define t(c) move_to(c) #define t(c) move_to(c)
@@ -191,9 +215,13 @@ int main(int __unused ac, __unused char**av)
FILE *outfd; FILE *outfd;
char *fen; char *fen;
pos_t *pos, *fishpos = pos_new(); pos_t *pos, *fishpos = pos_new();
movelist_t legal;
//bitboard_t wrong = 0x5088000040, tmp, loop; //bitboard_t wrong = 0x5088000040, tmp, loop;
//bit_for_each64(loop, tmp, ) //bit_for_each64(loop, tmp, )
//printf("fishpos 1=%p\n", fishpos); //printf("fishpos 1=%p\n", fishpos);
setlinebuf(stdout); /* line-buffered stdout */
bitboard_init(); bitboard_init();
hyperbola_init(); hyperbola_init();
outfd = open_stockfish(); outfd = open_stockfish();
@@ -201,16 +229,15 @@ int main(int __unused ac, __unused char**av)
while ((fen = next_fen(MOVEGEN))) { while ((fen = next_fen(MOVEGEN))) {
//printf(">>>>> %s\n", test[i]); //printf(">>>>> %s\n", test[i]);
//printf("fishpos 2=%p\n", fishpos); //printf("fishpos 2=%p\n", fishpos);
printf("original fen %d: [%p][%s]\n", i, fen, fen); //printf("original fen %d: [%p][%s]\n", i, fen, fen);
if (!(pos = fen2pos(NULL, fen))) { if (!(pos = fen2pos(NULL, fen))) {
printf("wrong fen %d: [%s]\n", i, fen); printf("wrong fen %d: [%s]\n", i, fen);
continue; continue;
} }
pos_print(pos);
/* print movelists */ /* print movelists */
send_stockfish_fen(outfd, fishpos, fen); send_stockfish_fen(outfd, fishpos, fen);
gen_all_pseudomoves(pos); pos_gen_pseudomoves(pos);
pos_all_legal(pos, &legal);
//printf("Fu "); //printf("Fu ");
//moves_print(fishpos, 0); //moves_print(fishpos, 0);
//fflush(stdout); //fflush(stdout);
@@ -219,8 +246,8 @@ int main(int __unused ac, __unused char**av)
//fflush(stdout); //fflush(stdout);
/* sort and print movelists */ /* sort and print movelists */
move_sort_by_sq(fishpos); move_sort_by_sq(&fishpos->moves);
move_sort_by_sq(pos); move_sort_by_sq(&legal);
// printf("\nFs "); // printf("\nFs ");
// moves_print(fishpos, 0); // moves_print(fishpos, 0);
// fflush(stdout); // fflush(stdout);
@@ -229,7 +256,20 @@ int main(int __unused ac, __unused char**av)
// fflush(stdout); // fflush(stdout);
/* compare movelists */ /* compare movelists */
compare_moves(fishpos, pos); if (!movelists_equal(&fishpos->moves, &legal)) {
pos_print(pos);
printf("F: ");
moves_print(&fishpos->moves, 0);
printf("M: ");
moves_print(&legal, 0);
} else {
printf("[%s]: OK (%d Moves)\n", fen, legal.nmoves);
//moves_print(&fishpos->moves, 0);
}
//compare_moves(&fishpos->moves, &legal);
//} else {
//printf("fen %d: [%s] - OK (%d moves)\n", i, fen, legal.nmoves);
//}
//pos_print_board_raw(pos, 1); //pos_print_board_raw(pos, 1);
//printf("%s\n", pos2fen(pos, str)); //printf("%s\n", pos2fen(pos, str));
//get_stockfish_moves(test[i]); //get_stockfish_moves(test[i]);

View File

@@ -1,55 +1,20 @@
#include <stdio.h> #include <stdio.h>
#include "debug.h" #include "brlib.h"
#include "../src/fen.h" #include "piece.h"
#include "../src/position.h"
#include "../src/bitboard.h"
int main(int ac, char**av) int main(__unused int ac, __unused char**av)
{ {
pos_t *pos;
printf("zobi\n");fflush(stdout);
debug_init(6, stderr, true);
log_f(5, "kfsjdhg\n");
pos_pool_init();
pos = pos_get();
piece_pool_init();
if (ac == 1) { piece_t p;
printf("zoba\n");fflush(stdout); char *test="PNBRQKpnbrqk";
pos_startpos(pos);
} else { for (u64 i = 0; i < sizeof(test); ++i) {
fen2pos(pos, av[1]); char c1 = test[i], *c2;
p = piece_from_fen(c1);
c2 = piece_to_fen(p);
printf("c1=%c c2=%c\n", c1, *c2);
} }
pos_print(pos); return 0;
pos_pieces_print(pos);
printf("0x1c = 11100 = C1-E1:\n");
bitboard_print(0x1c);
printf("0x70 = 111 = A1-C1\n");
bitboard_print(0x70);
printf("0x0e = 1110 = B1-D1\n");
bitboard_print(0x0e);
printf("0x60 = 1100000 = F1-G1\n");
bitboard_print(0x60);
printf("A1:\n");
bitboard_print(A1);
printf("1:\n");
bitboard_print(1L);
printf("H1:\n");
bitboard_print(H1);
printf("C1:\n");
bitboard_print(C1);
printf("D1:\n");
bitboard_print(D1);
printf("C1|D1:\n");
bitboard_print(C1|D1);
printf("H8:\n");
bitboard_print(H8);
} }

0
util.c
View File