Compare commits
24 Commits
17d1dc52f9
...
bitboard-s
Author | SHA1 | Date | |
---|---|---|---|
856e3e52da | |||
51a348c1e4 | |||
51c35e21f4 | |||
3a06407d5a | |||
ae6328ce26 | |||
08064dd1a1 | |||
49705bc707 | |||
8527c3dee1 | |||
dc916f5c56 | |||
748953a767 | |||
9c2e76442d | |||
49302c7a60 | |||
db38b507ff | |||
9d40a53aea | |||
260d8d34bd | |||
037d49e4ca | |||
4eb620a873 | |||
92dcb1e778 | |||
c8aea61529 | |||
b3f0dd0534 | |||
0f06ccb8db | |||
3e477f7442 | |||
aba0113344 | |||
cc7d91ebfb |
@@ -1,3 +1,3 @@
|
||||
((nil . ((compile-command . (concat "make -C "
|
||||
(vc-root-dir)
|
||||
" -k -j2")))))
|
||||
" -k -j2 testing")))))
|
||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -17,4 +17,5 @@ vgcore.*
|
||||
/tmp/
|
||||
/notused/
|
||||
valgrind.out
|
||||
gmon.out
|
||||
compile_commands.json
|
||||
|
19
Makefile
19
Makefile
@@ -64,10 +64,12 @@ CPPFLAGS += -DWARN_ON # brlib bug.h
|
||||
#CPPFLAGS += -DDEBUG_DEBUG # enable log() functions
|
||||
#CPPFLAGS += -DDEBUG_DEBUG_C # enable log() settings
|
||||
#CPPFLAGS += -DDEBUG_POOL # memory pools management
|
||||
#CPPFLAGS += -DDEBUG_FEN # FEN decoding
|
||||
#CPPFLAGS += -DDEBUG_POS # position.c
|
||||
#CPPFLAGS += -DDEBUG_MOVE # move generation
|
||||
|
||||
# fen.c
|
||||
#CPPFLAGS += -DDEBUG_FEN # FEN decoding
|
||||
|
||||
# attack.c
|
||||
#CPPFLAGS += -DDEBUG_ATTACK_ATTACKERS1 # sq_attackers details
|
||||
CPPFLAGS += -DDEBUG_ATTACK_ATTACKERS # sq_attackers
|
||||
@@ -284,8 +286,9 @@ memcheck: targets
|
||||
##################################### test binaries
|
||||
.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 \
|
||||
attack.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_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintessence.o \
|
||||
attack.o move.o move-gen.o
|
||||
MOVEDO_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintessence.o \
|
||||
attack.o move.o move-gen.o move-do.o
|
||||
|
||||
TEST := $(addprefix $(BINDIR)/,$(TEST))
|
||||
|
||||
PIECE_OBJS := $(addprefix $(OBJDIR)/,$(PIECE_OBJS))
|
||||
FEN_OBJS := $(addprefix $(OBJDIR)/,$(FEN_OBJS))
|
||||
BB_OBJS := $(addprefix $(OBJDIR)/,$(BB_OBJS))
|
||||
MOVEGEN_OBJS := $(addprefix $(OBJDIR)/,$(MOVEGEN_OBJS))
|
||||
ATTACK_OBJS := $(addprefix $(OBJDIR)/,$(ATTACK_OBJS))
|
||||
MOVEDO_OBJS := $(addprefix $(OBJDIR)/,$(MOVEDO_OBJS))
|
||||
|
||||
test:
|
||||
echo TEST=$(TEST)
|
||||
@@ -308,6 +315,10 @@ 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)
|
||||
@echo compiling $@ test executable.
|
||||
@$(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.
|
||||
@$(CC) $(ALL_CFLAGS) $< $(ATTACK_OBJS) $(ALL_LDFLAGS) -o $@
|
||||
|
||||
bin/movedo-test: test/movedo-test.c test/common-test.h $(MOVEDO_OBJS)
|
||||
@echo compiling $@ test executable.
|
||||
@$(CC) $(ALL_CFLAGS) $< $(MOVEDO_OBJS) $(ALL_LDFLAGS) -o $@
|
||||
|
||||
##################################### Makefile debug
|
||||
.PHONY: showflags wft
|
||||
|
||||
|
@@ -26,6 +26,7 @@
|
||||
/**
|
||||
* sq_attackers() - find attackers on a square
|
||||
* @pos: position
|
||||
* @occ: occupation mask used
|
||||
* @sq: square to test
|
||||
* @c: attacker color
|
||||
*
|
||||
@@ -39,11 +40,11 @@
|
||||
*
|
||||
* @Return: a bitboard of attackers.
|
||||
*/
|
||||
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)
|
||||
{
|
||||
bitboard_t attackers = 0, tmp;
|
||||
bitboard_t sqbb = mask(sq);
|
||||
bitboard_t occ = pos_occ(pos);
|
||||
//bitboard_t occ = pos_occ(pos);
|
||||
bitboard_t to;
|
||||
color_t opp = OPPONENT(c);
|
||||
|
||||
@@ -151,5 +152,6 @@ bitboard_t sq_pinners(const pos_t *pos, const square_t sq, const color_t color)
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@
|
||||
#include "chessdefs.h"
|
||||
#include "bitboard.h"
|
||||
|
||||
extern bitboard_t sq_attackers(const pos_t *pos, const square_t sq, const color_t c);
|
||||
extern 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_attackers(const pos_t *pos, const bitboard_t occ, const square_t sq, const color_t c);
|
||||
bitboard_t sq_attackers_all(const pos_t *pos, const square_t sq);
|
||||
bitboard_t sq_pinners(const pos_t *pos, const square_t sq, const color_t c);
|
||||
#endif
|
||||
|
@@ -173,10 +173,6 @@ static __always_inline bitboard_t bb_rank(int rank)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return RANK_1bb << (rank * 8);
|
||||
@@ -187,11 +183,13 @@ static __always_inline bitboard_t bb_file(int file)
|
||||
}
|
||||
*/
|
||||
|
||||
#define BB_RANK(r) ((u64) RANK_1bb << ((r) * 8))
|
||||
#define bb_rank(r) ((u64) RANK_1bb << ((r) * 8))
|
||||
#define BB_FILE(f) ((u64) FILE_Abb << (f))
|
||||
|
||||
#define BB_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))))
|
||||
#define bb_rel_rank(r, c) bb_rank(sq_rel_rank(r, 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).
|
||||
|
@@ -65,9 +65,9 @@ void board_print(const piece_t *board)
|
||||
for (int file = 0; file < 8; ++file) {
|
||||
piece_t pc = board[sq_make(file, rank)];
|
||||
# ifdef DIAGRAM_SYM
|
||||
printf(" %s |", pc? piece_to_sym_color(pc): " ");
|
||||
printf(" %s |", pc? piece_to_sym(pc): " ");
|
||||
# else
|
||||
printf(" %s |", pc? piece_to_sym_color(pc): " ");
|
||||
printf(" %s |", pc? piece_to_fen(pc): " ");
|
||||
# endif
|
||||
}
|
||||
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;
|
||||
printf("%s", set? REVERSE : " ");
|
||||
# ifdef DIAGRAM_SYM
|
||||
printf("%s", pc? piece_to_sym_color(pc): " ");
|
||||
printf("%s", pc? piece_to_sym(pc): " ");
|
||||
# else
|
||||
printf("%s", pc? piece_to_char_color(pc): " ");
|
||||
# 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) {
|
||||
piece_t p = board[sq_make(f, r)];
|
||||
if (type) {
|
||||
printf("%s ", p == EMPTY? ".": piece_to_char_color(p));
|
||||
printf("%s ", p == EMPTY? ".": piece_to_char(p));
|
||||
} else {
|
||||
printf("%02o ", p);
|
||||
}
|
||||
|
@@ -21,8 +21,30 @@
|
||||
#define mask(i) ( (u64) (ONE << (i)) )
|
||||
|
||||
#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
|
||||
*/
|
||||
@@ -35,16 +57,23 @@ typedef enum {
|
||||
CASTLE_W = (CASTLE_WK | CASTLE_WQ), /* 00000011 W castle mask */
|
||||
CASTLE_B = (CASTLE_BK | CASTLE_BQ), /* 00001100 B castle mask */
|
||||
|
||||
CASTLE_K = (1 << 0), /* generic K/Q, after doing : */
|
||||
CASTLE_Q = (1 << 1), /* flags >> (2 * color) */
|
||||
CASTLE_K = (1 << 0), /* generic K/Q, bits 0 and 1 */
|
||||
CASTLE_Q = (1 << 1),
|
||||
CASTLE_KQ = (CASTLE_K |CASTLE_Q)
|
||||
} castle_rights_t;
|
||||
|
||||
/* 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 CAN_OO(f, c) (NORM_CASTLE(f, c) & CASTLE_K)
|
||||
#define CAN_OOO(f, c) (NORM_CASTLE(f, c) & CASTLE_Q)
|
||||
#define CAN_CASTLE(f, c) (CAN_OO(f, c) | CAN_OOO(f, c))
|
||||
//#define NORM_CASTLE(f, c) ((f) >> (2 * (c))) /* shift flags to bits 0/1 */
|
||||
//#define
|
||||
//(NORM_CASTLE(f, c) & CASTLE_Q)
|
||||
#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
|
||||
*/
|
||||
|
24
src/fen.c
24
src/fen.c
@@ -81,9 +81,9 @@ static int fen_check(pos_t *pos)
|
||||
if (pos->en_passant != SQUARE_NONE) {
|
||||
rank_t eprank = sq_rank(pos->en_passant);
|
||||
file_t epfile = sq_file(pos->en_passant);
|
||||
rank_t rank5 = SQ_REL_RANK(RANK_5, pos->turn);
|
||||
rank_t rank6 = SQ_REL_RANK(RANK_6, pos->turn);
|
||||
rank_t rank7 = SQ_REL_RANK(RANK_7, pos->turn);
|
||||
rank_t rank5 = sq_rel_rank(RANK_5, pos->turn);
|
||||
rank_t rank6 = sq_rel_rank(RANK_6, pos->turn);
|
||||
rank_t rank7 = sq_rel_rank(RANK_7, pos->turn);
|
||||
piece_t pawn = pos->turn == WHITE? B_PAWN: W_PAWN;
|
||||
if (warn(eprank != rank6 ||
|
||||
pos->board[sq_make(epfile, rank5)] != pawn ||
|
||||
@@ -184,9 +184,9 @@ pos_t *fen2pos(pos_t *pos, const char *fen)
|
||||
file += *cur - '0';
|
||||
continue;
|
||||
}
|
||||
if ((piece = char_color_to_piece(*cur)) != EMPTY) {
|
||||
if ((piece = piece_from_fen(*cur)) != EMPTY) {
|
||||
# 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));
|
||||
# endif
|
||||
pos_set_sq(&tmppos, sq_make(file, rank), piece);
|
||||
@@ -296,13 +296,23 @@ char *pos2fen(const pos_t *pos, char *fen)
|
||||
for (file_t f = FILE_A; f <= FILE_H;) {
|
||||
square_t sq = sq_make(f, r);
|
||||
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;
|
||||
for (; f <= FILE_H && pos->board[sq_make(f, r)] == EMPTY; f++)
|
||||
len++;
|
||||
# ifdef DEBUG_FEN
|
||||
printf("empty=%d char=%c\n", len, '0' + len);
|
||||
# endif
|
||||
fen[cur++] = '0' + len;
|
||||
} 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++;
|
||||
}
|
||||
}
|
||||
|
172
src/move-do.c
Normal file
172
src/move-do.c
Normal 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
20
src/move-do.h
Normal 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 */
|
203
src/move-gen.c
203
src/move-gen.c
@@ -34,46 +34,65 @@
|
||||
*
|
||||
* @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)
|
||||
{
|
||||
color_t us = pos->turn, them = OPPONENT(us);
|
||||
square_t from = move_from(move), to = move_to(move);
|
||||
int piece = PIECE(pos->board[from]), sq;
|
||||
bitboard_t tmp;
|
||||
square_t from = move_from(move), to = move_to(move), king = pos->king[us], sq;
|
||||
piece_type_t piece = PIECE(pos->board[from]);
|
||||
bitboard_t kingbb = pos->bb[us][KING], tmp;
|
||||
|
||||
/* (1) - King
|
||||
* For castling, we already tested intermediate squares attacks
|
||||
* in pseudo move generation, so we only care destination square here.
|
||||
* Attention: We need to exclude king from occupation bitboard !
|
||||
*/
|
||||
if (piece == KING)
|
||||
return !sq_attackers(pos, to, them);
|
||||
if (piece == KING) {
|
||||
bitboard_t occ = pos_occ(pos) ^ kingbb;
|
||||
return !sq_attackers(pos, occ, to, them);
|
||||
}
|
||||
|
||||
/* (2) - pinned pieces
|
||||
/* (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
|
||||
* in pseudo move generation, so we only care destination square here.
|
||||
*/
|
||||
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][pos->king[us]];
|
||||
bitboard_t line = bb_line[from][king];
|
||||
return line & mask(to); /* to is not on pin line */
|
||||
}
|
||||
|
||||
/* (3) - En-passant
|
||||
/* (4) - En-passant
|
||||
* We only care the situation where our King and enemy R/Q are on
|
||||
* from rank ()
|
||||
* 5th relative rank. To do so, we create an occupation bb without
|
||||
* the 2 pawns.
|
||||
*/
|
||||
if (move & M_CAPTURE && PIECE(pos->board[to]) == EMPTY) {
|
||||
/* from square rank bitboard */
|
||||
bitboard_t rank5 = bb_sqrank[sq_rank(from)];
|
||||
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 ((pos->bb[us][KING] & rank5) && rooks) { /* K and enemy R/Q on rank */
|
||||
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) & pos->bb[us][KING])
|
||||
if (hyperbola_rank_moves(occ, sq) & kingbb)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -81,6 +100,51 @@ bool pseudo_is_legal(pos_t *pos, move_t move)
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -93,7 +157,9 @@ bool pseudo_is_legal(pos_t *pos, move_t move)
|
||||
* - 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 (incl. en-passant): M_CAPTURE
|
||||
* - pawn capture (excl. en-passant): M_CAPTURE
|
||||
* - en-passant: M_EN_PASSANT
|
||||
* - pawn double push: M_DPUSH
|
||||
* - promotion: M_PROMOTION
|
||||
* - promotion and capture
|
||||
*
|
||||
@@ -109,7 +175,6 @@ int pos_gen_pseudomoves(pos_t *pos)
|
||||
bitboard_t my_pieces = pos->bb[us][ALL_PIECES];
|
||||
bitboard_t not_my_pieces = ~my_pieces;
|
||||
bitboard_t enemy_pieces = pos->bb[them][ALL_PIECES];
|
||||
|
||||
bitboard_t occ = my_pieces | enemy_pieces;
|
||||
bitboard_t empty = ~occ;
|
||||
|
||||
@@ -118,61 +183,58 @@ int pos_gen_pseudomoves(pos_t *pos)
|
||||
|
||||
move_t *moves = pos->moves.move;
|
||||
int nmoves = pos->moves.nmoves;
|
||||
|
||||
int from, to;
|
||||
|
||||
/* king - MUST BE FIRST ! */
|
||||
bug_on(nmoves != 0);
|
||||
|
||||
/* king - MUST BE FIRST (we stop if doubler check) */
|
||||
from = pos->king[us];
|
||||
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);
|
||||
}
|
||||
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 */
|
||||
return (pos->moves.nmoves = nmoves);
|
||||
|
||||
/* 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;
|
||||
bit_for_each64(to, tmp2, movebits) {
|
||||
bit_for_each64(to, tmp2, movebits & empty) {
|
||||
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));
|
||||
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);
|
||||
}
|
||||
}
|
||||
/* TODO: remove this one, after movegen is validated */
|
||||
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);
|
||||
bit_for_each64(to, tmp2, movebits & enemy_pieces) {
|
||||
moves[nmoves++] = move_make_capture(from, to);
|
||||
}
|
||||
}
|
||||
|
||||
/* knight */
|
||||
bit_for_each64(from, tmp1, pos->bb[us][KNIGHT]) {
|
||||
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);
|
||||
}
|
||||
bit_for_each64(to, tmp2, movebits & enemy_pieces) {
|
||||
moves[nmoves++] = move_make_capture(from, to);
|
||||
}
|
||||
}
|
||||
|
||||
/* pawn: relative rank and files */
|
||||
bitboard_t rel_rank7 = us == WHITE ? RANK_7bb : RANK_2bb;
|
||||
bitboard_t rel_rank3 = us == WHITE ? RANK_3bb : RANK_6bb;
|
||||
//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;
|
||||
bitboard_t rel_rank7 = bb_rel_rank(RANK_7, us);
|
||||
bitboard_t rel_rank3 = bb_rel_rank(RANK_3, us);
|
||||
|
||||
/* pawn: ranks 2-6 push 1 and 2 squares */
|
||||
movebits = pawn_shift_up(pos->bb[us][PAWN] & ~rel_rank7, us) & empty;
|
||||
@@ -185,24 +247,34 @@ int pos_gen_pseudomoves(pos_t *pos)
|
||||
movebits = pawn_shift_up(movebits & rel_rank3, us) & empty;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
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, including en-passant */
|
||||
/* pawn: ranks 2-6 captures left */
|
||||
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) {
|
||||
from = pawn_push_upleft(to, them); /* reverse capture */
|
||||
moves[nmoves++] = move_make_capture(from, to);
|
||||
}
|
||||
/* pawn: ranks 2-6 captures right, including en-passant */
|
||||
/* pawn: ranks 2-6 captures right */
|
||||
from_pawns = pos->bb[us][PAWN] & ~rel_rank7; // & ~rel_fileh;
|
||||
movebits = pawn_shift_upright(from_pawns, us) & enemy_avail;
|
||||
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 */
|
||||
movebits = pawn_shift_up(pos->bb[us][PAWN] & rel_rank7, us) & empty;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
@@ -214,7 +286,7 @@ int pos_gen_pseudomoves(pos_t *pos)
|
||||
}
|
||||
/* pawn promotion: rank 7 captures left */
|
||||
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) {
|
||||
from = pawn_push_upleft(to, them); /* reverse capture */
|
||||
moves[nmoves++] = move_make_promote_capture(from, to, QUEEN);
|
||||
@@ -236,24 +308,24 @@ int pos_gen_pseudomoves(pos_t *pos)
|
||||
/* castle - Attention ! Castling flags are assumed correct
|
||||
*/
|
||||
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];
|
||||
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.
|
||||
* 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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
if (CAN_OOO(pos->castle, us)) {
|
||||
if (can_ooo(pos->castle, us)) {
|
||||
bitboard_t occmask = rel_rank1 & (FILE_Bbb | FILE_Cbb | FILE_Dbb);
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -271,24 +343,3 @@ int pos_gen_pseudomoves(pos_t *pos)
|
||||
*/
|
||||
return (pos->moves.nmoves = nmoves);
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_legalmoves() - generate position legal moves from pseudo-legal ones.
|
||||
* @pos: position
|
||||
* @dest: address where to store legal moves
|
||||
*
|
||||
* The pseudo-legal moves must be already calculated before calling this function.
|
||||
*
|
||||
* @Return: @dest
|
||||
*/
|
||||
movelist_t *pos_legalmoves(pos_t *pos, movelist_t *dest)
|
||||
{
|
||||
int pseudo;
|
||||
movelist_t *src = &pos->moves;
|
||||
dest->nmoves = 0;
|
||||
for (pseudo = 0; pseudo < src->nmoves; ++pseudo) {
|
||||
if (pseudo_is_legal(pos, src->move[pseudo]))
|
||||
dest->move[dest->nmoves++] = src->move[pseudo];
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
@@ -21,8 +21,9 @@
|
||||
#include "piece.h"
|
||||
#include "move.h"
|
||||
|
||||
extern bool pseudo_is_legal(pos_t *pos, move_t move);
|
||||
extern int pos_gen_pseudomoves(pos_t *pos);
|
||||
extern movelist_t *pos_legalmoves(pos_t *pos, movelist_t *dest);
|
||||
bool pseudo_is_legal(const pos_t *pos, const move_t move);
|
||||
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 */
|
||||
|
774
src/move.c
774
src/move.c
@@ -89,8 +89,9 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* moves_print() - print movelist moves.
|
||||
* @moves: &movelist_t moves list
|
||||
* move_str() - get a move string
|
||||
* @dst: destination memory
|
||||
* @move: move
|
||||
* @flags: moves selection and display options.
|
||||
*
|
||||
* Possible flags are:
|
||||
@@ -101,14 +102,40 @@
|
||||
* M_PR_NL: print a newline after move
|
||||
* M_PR_EVAL: print move eval
|
||||
*/
|
||||
char *move_str(char *dst, const move_t move, __unused const int flags)
|
||||
{
|
||||
square_t from = move_from(move);
|
||||
square_t to = move_to(move);
|
||||
int len;
|
||||
sprintf(dst, "%s%s%n", sq_to_string(from), sq_to_string(to), &len);
|
||||
|
||||
if (is_promotion(move)) {
|
||||
piece_t promoted = (piece_t) move_promoted(move);
|
||||
sprintf(dst + len, "%s", piece_to_low(promoted));
|
||||
}
|
||||
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)
|
||||
{
|
||||
printf("%2d:", moves->nmoves);
|
||||
for (int m = 0; m < moves->nmoves; ++m) {
|
||||
square_t from = move_from(moves->move[m]);
|
||||
square_t to = move_to(moves->move[m]);
|
||||
printf(" %s-%s", sq_to_string(from), sq_to_string(to));
|
||||
}
|
||||
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");
|
||||
}
|
||||
|
||||
@@ -120,12 +147,16 @@ static int _moves_cmp_bysquare(const void *p1, const void *p2)
|
||||
square_t t1 = move_to(m1);
|
||||
square_t f2 = move_from(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;
|
||||
/* f1 == f2 */
|
||||
if (t1 < t2) return -1;
|
||||
if (t1 > t2) return 1;
|
||||
/* t1 == t2 */
|
||||
if (prom1 < prom2) return -1;
|
||||
if (prom1 > prom2) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -139,728 +170,3 @@ void move_sort_by_sq(movelist_t *moves)
|
||||
{
|
||||
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);
|
||||
* }
|
||||
*/
|
||||
|
135
src/move.h
135
src/move.h
@@ -19,80 +19,89 @@
|
||||
#include "board.h"
|
||||
|
||||
/* move structure:
|
||||
* 3 2 2 1 1 1 1 1 1
|
||||
* 1 5 3 8 7 5 4 2 1 6 5 0
|
||||
* UUUUUUUU FFFFFF ppp ccc tttttt ffffff
|
||||
* 3 3 2 2 1 1 1 1 1 1
|
||||
* 1 0 4 3 8 7 5 4 2 1 6 5 0
|
||||
* S UUUUUUU FFFFFF ccc ppp tttttt ffffff
|
||||
*
|
||||
* bits len range type mask get desc
|
||||
* ffffff 6 0-5 square_t 3f &63 from
|
||||
* tttttt 6 6-11 square_t fc0 (>>6)&63 to
|
||||
* ccc 3 12-14 piece_type_t 7000 (>>12)&7 captured
|
||||
* ppp 3 15-17 piece_type_t 38000 (>>15)&7 promoted
|
||||
* FFFFFF 6 18-23 move_flags_t fc0000 N/A flags
|
||||
* UUUUUUUU 8 24-31 unused fe000000 N/A future usage ?
|
||||
* bits len off range type mask get desc
|
||||
* ffffff 6 0 0-5 square_t 077 &077 from
|
||||
* tttttt 6 6 6-11 square_t 07700 (>>6) &077 to
|
||||
* ppp 3 12 12-14 piece_type_t 070000 (>>12) &07 promoted
|
||||
* ccc 3 15 15-17 piece_type_t 0700000 (>>15) &07 captured
|
||||
* FFFFFF 6 18 18-23 move_flags_t 077000000 (>>18) &077 N/A flags
|
||||
* UUUUUUU 7 24 24-30 unused 017700000000 future usage
|
||||
* S 1 31 31-31 - 020000000000 sign
|
||||
*/
|
||||
typedef u32 move_t;
|
||||
typedef s32 move_t;
|
||||
|
||||
/* special move_t values */
|
||||
#define MOVE_NONE (-1)
|
||||
#define MOVE_NULL (0) /* hack: from = to = A1 */
|
||||
|
||||
enum {
|
||||
M_OFF_FROM = 0,
|
||||
M_OFF_TO = 6,
|
||||
M_OFF_PROMOTED = 12,
|
||||
M_OFF_CAPTURED = 15,
|
||||
M_OFF_FLAGS = 18
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
M_START = 18,
|
||||
M_CAPTURE = (1 << (M_START + 0)),
|
||||
M_ENPASSANT = (1 << (M_START + 1)),
|
||||
M_PROMOTION = (1 << (M_START + 2)),
|
||||
M_CASTLE_K = (1 << (M_START + 3)), /* maybe only one ? */
|
||||
M_CASTLE_Q = (1 << (M_START + 4)), /* maybe only one ? */
|
||||
M_CHECK = (1 << (M_START + 5)) /* maybe unknown/useless ? */
|
||||
} move_type_t;
|
||||
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
|
||||
|
||||
typedef struct __movelist_s {
|
||||
move_t move[MOVES_MAX];
|
||||
int nmoves; /* total moves (fill) */
|
||||
int curmove; /* current move (use) */
|
||||
} movelist_t;
|
||||
|
||||
|
||||
/* move flags */
|
||||
//#define M_FLAGS_BEG 18
|
||||
//#define M_HAS_FLAGS mask(M_FLAGS_BEG + 0) /* probably useless */
|
||||
//#define M_CAPTURE mask(M_FLAGS_BEG + 0)
|
||||
//#define M_EN_PASSANT mask(M_FLAGS_BEG + 1)
|
||||
//#define M_PROMOTION mask(M_FLAGS_BEG + 2)
|
||||
//#define M_CASTLE_K mask(M_FLAGS_BEG + 3) /* maybe only one ? */
|
||||
//#define M_CASTLE_Q mask(M_FLAGS_BEG + 4) /* maybe only one ? */
|
||||
//#define M_CHECK mask(M_FLAGS_BEG + 5) /* probably unknown/useless */
|
||||
|
||||
//#define M_FLAGS (M_CAPTURE | M_ENPASSANT | M_PROMOTION |
|
||||
// M_CASTLE_K | M_CASTLE_Q | M_CHECK)
|
||||
//#define M_NORMAL (~M_FLAGS)
|
||||
|
||||
static inline square_t move_from(move_t move)
|
||||
{
|
||||
return move & 077;
|
||||
}
|
||||
|
||||
static inline square_t move_to(move_t move)
|
||||
{
|
||||
return (move >> 6) & 077;
|
||||
return (move >> M_OFF_TO) & 077;
|
||||
}
|
||||
|
||||
static inline piece_t move_promoted(move_t move)
|
||||
static inline piece_type_t move_promoted(move_t move)
|
||||
{
|
||||
return (move >> 15) & 07;
|
||||
return (move >> M_OFF_PROMOTED) & 07;
|
||||
}
|
||||
|
||||
static inline piece_t move_captured(move_t move)
|
||||
static inline piece_type_t move_captured(move_t move)
|
||||
{
|
||||
return (move >> 12) & 07;
|
||||
return (move >> M_OFF_CAPTURED) & 07;
|
||||
}
|
||||
|
||||
static inline move_t move_make(square_t from, square_t to)
|
||||
{
|
||||
return (to << 6) | from;
|
||||
return (to << M_OFF_TO) | from;
|
||||
}
|
||||
|
||||
static inline move_t move_make_flags(square_t from, square_t to, move_type_t flags)
|
||||
static inline move_t move_make_flags(square_t from, square_t to, move_flags_t flags)
|
||||
{
|
||||
return move_make(from, to) | flags;
|
||||
return move_set_flags(move_make(from, to), flags);
|
||||
}
|
||||
|
||||
static inline move_t move_make_capture(square_t from, square_t to)
|
||||
@@ -100,16 +109,26 @@ 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 << 15);
|
||||
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_flags(from, to, M_CAPTURE | M_PROMOTION) | (promoted << 15);
|
||||
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
|
||||
@@ -118,32 +137,14 @@ static inline move_t move_make_promote_capture(square_t from, square_t to,
|
||||
#define M_PR_NCAPT 0x02
|
||||
#define M_PR_NUM 0x04
|
||||
#define M_PR_NL 0x08
|
||||
#define M_UCI 0x10 /* UCI style - e7e8q */
|
||||
#define M_PR_EVAL 0x20 /* separate captures */
|
||||
#define M_PR_SEPARATE 0x40 /* separate captures */
|
||||
#define M_PR_LONG 0x80
|
||||
|
||||
//pool_t *moves_pool_init();
|
||||
//void moves_pool_stats();
|
||||
|
||||
//int move_print(int movenum, move_t *move, move_flags_t flags);
|
||||
|
||||
extern void moves_print(movelist_t *moves, int flags);
|
||||
extern 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);
|
||||
char *move_str(char *dst, const move_t move, __unused const int flags);
|
||||
void moves_print(movelist_t *moves, int flags);
|
||||
void move_sort_by_sq(movelist_t *moves);
|
||||
|
||||
#endif /* MOVE_H */
|
||||
|
152
src/piece.c
152
src/piece.c
@@ -25,22 +25,22 @@
|
||||
* piece_details
|
||||
*/
|
||||
const struct piece_details piece_details[PIECE_MAX] = {
|
||||
/* Abb Fen Sym SymC Name start value */
|
||||
[EMPTY] = { "", "", "", "", "", 0, 0, 0 },
|
||||
[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_BISHOP] = { "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_QUEEN] = { "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 },
|
||||
[7] = { "", "", "", "", "", 0, 0, 0 },
|
||||
[8] = { "", "", "", "", "", 0, 0, 0 },
|
||||
[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_BISHOP] = { "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_QUEEN] = { "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 },
|
||||
/* cap low fen sym name values */
|
||||
[EMPTY] = { "", "", "", "", "", 0, 0, 0 },
|
||||
[W_PAWN] = { "", "", "P", "♙", "Pawn", P_VAL_OPN, P_VAL_MID, P_VAL_END },
|
||||
[W_KNIGHT] = { "N", "n", "N", "♘", "Knight", N_VAL_OPN, N_VAL_MID, N_VAL_END },
|
||||
[W_BISHOP] = { "B", "b", "B", "♗", "Bishop", B_VAL_OPN, B_VAL_MID, B_VAL_END },
|
||||
[W_ROOK] = { "R", "r", "R", "♖", "Rook", R_VAL_OPN, R_VAL_MID, R_VAL_END },
|
||||
[W_QUEEN] = { "Q", "q", "Q", "♕", "Queen", Q_VAL_OPN, Q_VAL_MID, Q_VAL_END },
|
||||
[W_KING] = { "K", "k", "K", "♔", "King", K_VAL_OPN, K_VAL_MID, K_VAL_END },
|
||||
[7] = { "", "", "", "", "", 0, 0, 0 },
|
||||
[8] = { "", "", "", "", "", 0, 0, 0 },
|
||||
[B_PAWN] = { "", "", "p", "♟", "Pawn", P_VAL_OPN, P_VAL_MID, P_VAL_END },
|
||||
[B_KNIGHT] = { "N", "n", "n", "♞", "Knight", P_VAL_OPN, N_VAL_MID, N_VAL_END },
|
||||
[B_BISHOP] = { "B", "b", "b", "♝", "Bishop", P_VAL_OPN, B_VAL_MID, B_VAL_END },
|
||||
[B_ROOK] = { "R", "r", "r", "♜", "Rook", P_VAL_OPN, R_VAL_MID, R_VAL_END },
|
||||
[B_QUEEN] = { "Q", "q", "q", "♛", "Queen", P_VAL_OPN, Q_VAL_MID, Q_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";
|
||||
@@ -51,24 +51,24 @@ bool piece_ok(piece_t p)
|
||||
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)
|
||||
{
|
||||
return piece_details[PIECE(p)].sym;
|
||||
}
|
||||
|
||||
char *piece_to_sym_color(piece_t p)
|
||||
{
|
||||
return piece_details[p].c_sym;
|
||||
return piece_details[p].sym;
|
||||
}
|
||||
|
||||
char *piece_to_name(piece_t p)
|
||||
@@ -76,98 +76,20 @@ char *piece_to_name(piece_t p)
|
||||
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);
|
||||
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);
|
||||
return isupper(c)? piece: SET_BLACK(piece);
|
||||
piece_type_t piece = piece_t_from_char(c);
|
||||
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;
|
||||
* }
|
||||
*/
|
||||
|
37
src/piece.h
37
src/piece.h
@@ -31,6 +31,7 @@ typedef enum {
|
||||
|
||||
typedef enum {
|
||||
ALL_PIECES = 0, /* 'all pieces' bitboard */
|
||||
NO_PIECE_TYPE = 0,
|
||||
PAWN = 1, KNIGHT, BISHOP, ROOK, QUEEN, KING,
|
||||
PIECE_TYPE_MAX = 7 /* bit 4 */
|
||||
} piece_type_t;
|
||||
@@ -80,10 +81,10 @@ typedef enum __piece_e {
|
||||
* s64 end_value;
|
||||
*/
|
||||
extern const struct piece_details {
|
||||
char *abbr; /* used for game notation */
|
||||
char *c_abbr; /* lowercase = black */
|
||||
char *sym; /* used for game notation */
|
||||
char *c_sym; /* different W & B */
|
||||
char *cap; /* used for game notation */
|
||||
char *low; /* used also for UCI promotion */
|
||||
char *fen; /* cap=white, low=black */
|
||||
char *sym; /* UTF-8 symbol */
|
||||
char *name; /* piece name */
|
||||
s64 opn_value; /* value opening */
|
||||
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_BLACK(p) (COLOR(p))
|
||||
|
||||
#define SET_WHITE(p) ((p) &= ~MASK_COLOR)
|
||||
#define SET_BLACK(p) ((p) |= MASK_COLOR)
|
||||
#define SET_COLOR(p, c) (!(c)? SET_WHITE(p): SET_BLACK(p))
|
||||
#define SET_WHITE(p) (piece_t)((p) &= ~MASK_COLOR)
|
||||
#define SET_BLACK(p) (piece_t)((p) |= MASK_COLOR)
|
||||
#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);
|
||||
extern char *piece_to_char_color(piece_t p);
|
||||
extern char *piece_to_sym(piece_t p);
|
||||
extern char *piece_to_sym_color(piece_t p);
|
||||
extern char *piece_to_name(piece_t p);
|
||||
char *piece_to_cap(piece_t p);
|
||||
char *piece_to_low(piece_t p);
|
||||
char *piece_to_fen(piece_t p);
|
||||
char *piece_to_sym(piece_t p);
|
||||
char *piece_to_name(piece_t p);
|
||||
|
||||
extern piece_t char_to_piece(char c);
|
||||
extern piece_t char_color_to_piece(char c);
|
||||
#define piece_to_char(c) piece_to_fen(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
|
||||
*/
|
||||
|
113
src/position.c
113
src/position.c
@@ -47,14 +47,7 @@ pos_t *pos_new(void)
|
||||
* pos_dup() - duplicate a position.
|
||||
* @pos: &position to duplicate.
|
||||
*
|
||||
* New position is the same as source one (with duplicated pieces list),
|
||||
* except:
|
||||
* - moves list is empty
|
||||
* - bestmove is NULL
|
||||
* - nodecount is set to zero
|
||||
* - eval is set to EVAL_INVALID
|
||||
* - moves_generated ans moves_counted are unset
|
||||
* - check is set to zero
|
||||
* Return a copy, allocated with malloc(1), of @pos.
|
||||
*
|
||||
* @Return: The new position.
|
||||
*
|
||||
@@ -64,14 +57,7 @@ pos_t *pos_dup(const pos_t *pos)
|
||||
{
|
||||
pos_t *newpos = safe_malloc(sizeof(pos_t));
|
||||
|
||||
//board = new->board;
|
||||
*newpos = *pos;
|
||||
//new->bestmove = NULL;
|
||||
newpos->node_count = 0;
|
||||
//new->eval = EVAL_INVALID;
|
||||
//new->moves_generated = false;
|
||||
//new->moves_counted = false;
|
||||
//new->check[WHITE] = new->check[BLACK] = 0;
|
||||
return newpos;
|
||||
}
|
||||
|
||||
@@ -87,6 +73,8 @@ void pos_del(pos_t *pos)
|
||||
/**
|
||||
* pos_clear() - clear a position.
|
||||
* @pos: &position.
|
||||
*
|
||||
* @return: @pos.
|
||||
*/
|
||||
pos_t *pos_clear(pos_t *pos)
|
||||
{
|
||||
@@ -95,27 +83,92 @@ pos_t *pos_clear(pos_t *pos)
|
||||
# endif
|
||||
pos->node_count = 0;
|
||||
pos->turn = WHITE;
|
||||
pos->clock_50 = 0;
|
||||
pos->plycount = 0;
|
||||
|
||||
/* move_do/undo position state */
|
||||
pos->en_passant = SQUARE_NONE;
|
||||
pos->castle = 0;
|
||||
pos->clock_50 = 0;
|
||||
pos->plycount = 0;
|
||||
pos->captured = NO_PIECE;
|
||||
|
||||
for (square_t sq = A1; sq <= H8; ++sq)
|
||||
pos->board[sq] = EMPTY;
|
||||
|
||||
for (color_t color = WHITE; color <= BLACK; ++color) {
|
||||
for (piece_type_t piece = 0; piece <= KING; ++piece)
|
||||
pos->bb[color][piece] = 0;
|
||||
pos->controlled[color] = 0;
|
||||
//pos->controlled[color] = 0;
|
||||
pos->king[color] = SQUARE_NONE;
|
||||
}
|
||||
|
||||
for (square_t sq = A1; sq <= H8; ++sq)
|
||||
pos->board[sq] = EMPTY;
|
||||
pos->moves.curmove = 0;
|
||||
pos->moves.nmoves = 0;
|
||||
pos->checkers = 0;
|
||||
pos->pinners = 0;
|
||||
pos->blockers = 0;
|
||||
|
||||
//pos->moves.curmove = 0;
|
||||
pos->moves.nmoves = 0;
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_cmp() - compare two positions..
|
||||
* @pos1, @pos2: The two &position.
|
||||
*
|
||||
* @return: true if equal, false otherwise.
|
||||
*/
|
||||
bool pos_cmp(const pos_t *pos1, const pos_t *pos2)
|
||||
{
|
||||
#define _cmpf(a) (pos1->a != pos2->a)
|
||||
bool ret = false;
|
||||
if (warn_on(_cmpf(node_count)))
|
||||
goto end;
|
||||
if (warn_on(_cmpf(turn)))
|
||||
goto end;
|
||||
|
||||
/* move_do/undo position state */
|
||||
if (warn_on(_cmpf(en_passant)))
|
||||
goto end;
|
||||
if (warn_on(_cmpf(castle)))
|
||||
goto end;
|
||||
if (warn_on(_cmpf(clock_50)))
|
||||
goto end;
|
||||
if (warn_on(_cmpf(plycount)))
|
||||
goto end;
|
||||
if (warn_on(_cmpf(captured)))
|
||||
goto end;
|
||||
|
||||
for (square_t sq = A1; sq <= H8; ++sq)
|
||||
if (warn_on(_cmpf(board[sq])))
|
||||
goto end;
|
||||
|
||||
for (color_t color = WHITE; color <= BLACK; ++color) {
|
||||
for (piece_type_t piece = 0; piece <= KING; ++piece)
|
||||
if (warn_on(_cmpf(bb[color][piece])))
|
||||
goto end;
|
||||
//pos->controlled[color] = 0;
|
||||
if (warn_on(_cmpf(king[color])))
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (warn_on(_cmpf(checkers)))
|
||||
goto end;
|
||||
if (warn_on(_cmpf(pinners)))
|
||||
goto end;
|
||||
if (warn_on(_cmpf(blockers)))
|
||||
goto end;
|
||||
|
||||
if (warn_on(_cmpf(moves.nmoves)))
|
||||
goto end;
|
||||
for (int i = 0; i < pos1->moves.nmoves; ++i)
|
||||
if (warn_on(_cmpf(moves.move[i])))
|
||||
goto end;
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
return ret;
|
||||
#undef _cmpf
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_checkers() - find all checkers on a king.
|
||||
* @pos: &position
|
||||
@@ -128,7 +181,8 @@ pos_t *pos_clear(pos_t *pos)
|
||||
*/
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -294,7 +348,7 @@ void pos_print_raw(const pos_t *pos, const int type)
|
||||
*/
|
||||
void pos_print_pieces(const pos_t *pos)
|
||||
{
|
||||
int bit, count, cur;
|
||||
int sq, count, cur;
|
||||
char *pname;
|
||||
u64 tmp;
|
||||
bitboard_t p;
|
||||
@@ -303,15 +357,14 @@ void pos_print_pieces(const pos_t *pos)
|
||||
p = pos->bb[color][piece];
|
||||
count = popcount64(p);
|
||||
cur = 0;
|
||||
pname = piece_to_char(piece);
|
||||
printf("%s(0)%s", pname, count? ":": "");
|
||||
pname = piece_to_char(p);
|
||||
printf("%s(%d)%s", pname, count, count? ":": "");
|
||||
if (count) {
|
||||
bit_for_each64(bit, tmp, p) {
|
||||
char cf = sq_file(bit), cr = sq_rank(bit);
|
||||
printf("%s%c%c", cur? ",": "", FILE2C(cf), RANK2C(cr));
|
||||
bit_for_each64(sq, tmp, p) {
|
||||
// char cf = sq_file(bit), cr = sq_rank(bit);
|
||||
printf("%s%s", cur? ",": "", sq_to_string(sq));
|
||||
cur++;
|
||||
}
|
||||
|
||||
}
|
||||
printf(" ");
|
||||
}
|
||||
|
@@ -18,6 +18,7 @@
|
||||
|
||||
#include "brlib.h"
|
||||
#include "bitops.h"
|
||||
#include "struct-group.h"
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "bitboard.h"
|
||||
@@ -28,22 +29,34 @@
|
||||
typedef struct __pos_s {
|
||||
u64 node_count; /* evaluated nodes */
|
||||
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 */
|
||||
square_t en_passant;
|
||||
castle_rights_t castle;
|
||||
/* data which cannot be recovered by move_undo
|
||||
* following data can be accessed either directly, either via "movesave"
|
||||
* 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 controlled[2]; /* unsure */
|
||||
//bitboard_t controlled[2]; /* unsure */
|
||||
square_t king[2]; /* dup with bb, faster retrieval */
|
||||
bitboard_t checkers; /* opponent checkers */
|
||||
bitboard_t pinners; /* opponent pinners */
|
||||
bitboard_t blockers; /* pieces blocking pin */
|
||||
piece_t board[BOARDSIZE];
|
||||
movelist_t moves;
|
||||
} pos_t;
|
||||
|
||||
typedef struct state_s state_t;
|
||||
|
||||
#define pos_pinned(p) (p->blockers & p->bb[p->turn][ALL_PIECES])
|
||||
|
||||
/**
|
||||
@@ -80,6 +93,8 @@ static __always_inline void pos_clr_sq(pos_t *pos, square_t square)
|
||||
pos->board[square] = EMPTY;
|
||||
pos->bb[color][type] &= ~mask(square);
|
||||
pos->bb[color][ALL_PIECES] &= ~mask(square);
|
||||
if (type == KING)
|
||||
pos->king[color] = SQUARE_NONE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,7 +133,7 @@ static __always_inline bitboard_t pos_between_occ(const pos_t *pos,
|
||||
static __always_inline int pos_between_count(const pos_t *pos,
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,25 +153,26 @@ static __always_inline int pos_between_count(const pos_t *pos,
|
||||
//void bitboard_print(bitboard_t bb, char *title);
|
||||
//void bitboard_print2(bitboard_t bb1, bitboard_t bb2, char *title);
|
||||
|
||||
extern pos_t *pos_new();
|
||||
extern pos_t *pos_dup(const pos_t *pos);
|
||||
extern void pos_del(pos_t *pos);
|
||||
extern pos_t *pos_clear(pos_t *pos);
|
||||
pos_t *pos_new();
|
||||
pos_t *pos_dup(const pos_t *pos);
|
||||
void pos_del(pos_t *pos);
|
||||
pos_t *pos_clear(pos_t *pos);
|
||||
bool pos_cmp(const pos_t *pos1, const pos_t *pos2);
|
||||
|
||||
extern bitboard_t pos_checkers(const pos_t *pos, const color_t color);
|
||||
extern bitboard_t pos_king_pinners(const pos_t *pos, const color_t color);
|
||||
extern bitboard_t pos_king_blockers(const pos_t *pos, const color_t color, const bitboard_t );
|
||||
//extern bitboard_t set_king_pinners_blockers(pos_t *pos);
|
||||
//extern char *pos_checkers2str(const pos_t *pos, char *str);
|
||||
//extern char *pos_pinners2str(const pos_t *pos, char *str);
|
||||
bitboard_t pos_checkers(const pos_t *pos, const color_t color);
|
||||
bitboard_t pos_king_pinners(const pos_t *pos, const color_t color);
|
||||
bitboard_t pos_king_blockers(const pos_t *pos, const color_t color, const bitboard_t );
|
||||
//bitboard_t set_king_pinners_blockers(pos_t *pos);
|
||||
//char *pos_checkers2str(const pos_t *pos, char *str);
|
||||
//char *pos_pinners2str(const pos_t *pos, char *str);
|
||||
|
||||
extern int pos_check(const pos_t *pos, const bool strict);
|
||||
int pos_check(const pos_t *pos, const bool strict);
|
||||
|
||||
extern void pos_print(const pos_t *pos);
|
||||
extern void pos_print_mask(const pos_t *pos, const bitboard_t mask);
|
||||
extern void pos_print_raw(const pos_t *pos, const int type);
|
||||
void pos_print(const pos_t *pos);
|
||||
void pos_print_mask(const pos_t *pos, const bitboard_t mask);
|
||||
void pos_print_raw(const pos_t *pos, const int type);
|
||||
|
||||
extern void pos_print_pieces(const pos_t *pos);
|
||||
void pos_print_pieces(const pos_t *pos);
|
||||
|
||||
|
||||
#endif /* POSITION_H */
|
||||
|
@@ -14,11 +14,12 @@
|
||||
#include <stdio.h>
|
||||
#include "chessdefs.h"
|
||||
/* when below FENs are in a struct with selection per test */
|
||||
#define NOTEST 0
|
||||
#define FEN 1
|
||||
#define BITBOARD 2
|
||||
#define MOVEGEN 4
|
||||
#define ATTACK 8
|
||||
#define NOTEST 0
|
||||
#define FEN 1
|
||||
#define BITBOARD 2
|
||||
#define MOVEGEN 4
|
||||
#define ATTACK 8
|
||||
#define MOVEDO 16
|
||||
|
||||
struct fentest {
|
||||
uint modules;
|
||||
@@ -35,6 +36,41 @@ struct fentest {
|
||||
*/
|
||||
//"4k3/pppppppp/8/8/8/8/PPPPPPPP/2BRK3 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,
|
||||
"checkers: a1 h1",
|
||||
"1k6/8/8/8/8/8/8/r2K3r w - - 1 1"
|
||||
@@ -163,7 +199,7 @@ struct fentest {
|
||||
"6k1/6pp/R2p4/p1p5/8/1P1r3P/6P1/6K1 b - - 3 3"
|
||||
},
|
||||
|
||||
// below tests are from:
|
||||
// some of tests below are from:
|
||||
// - Rodent IV
|
||||
// - https://www.chessprogramming.net/perfect-perft/
|
||||
{ FEN | MOVEGEN,
|
||||
@@ -174,6 +210,10 @@ struct fentest {
|
||||
"",
|
||||
"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,
|
||||
"",
|
||||
"4rrk1/pp1n3p/3q2pQ/2p1pb2/2PP4/2P3N1/P2B2PP/4RRK1 b - - 7 19"
|
||||
@@ -328,7 +368,18 @@ struct fentest {
|
||||
"illegal, SF crash",
|
||||
"2r1k3/3P4/8/8/8/8/8/4K3 w - - 0 1"
|
||||
},
|
||||
|
||||
{ MOVEDO,
|
||||
"simple movedo/undo: only 2 W knights",
|
||||
"8/1k6/8/8/8/8/6K1/1NN5 w - - 0 1"
|
||||
},
|
||||
{ MOVEDO,
|
||||
"simple movedo/undo: only 2 W knights",
|
||||
"8/1k6/8/8/8/8/6K1/1NN5 w - - 0 1"
|
||||
},
|
||||
{ MOVEDO,
|
||||
"simple movedo/undo: only 2 W knights",
|
||||
"5n2/1k6/8/8/5K2/8/P7/1N6 w - - 0 1"
|
||||
},
|
||||
{ 0, NULL, NULL }
|
||||
};
|
||||
|
||||
|
@@ -31,17 +31,19 @@ int main(__unused int ac, __unused char**av)
|
||||
bitboard_init();
|
||||
|
||||
while ((fen = next_fen(FEN))) {
|
||||
printf("***** [%s] ", fen);
|
||||
fflush(stdout);
|
||||
if (!(pos = fen2pos(NULL, fen))) {
|
||||
printf("[%s] **INVALID\n", fen);
|
||||
printf("**INVALID\n");
|
||||
} else {
|
||||
pos_print(pos);
|
||||
//pos_print(pos);
|
||||
pos2fen(pos, revfen);
|
||||
if (!strcmp(fen, revfen)) {
|
||||
printf("[%s] OK\n", fen);
|
||||
printf("OK\n");
|
||||
} else {
|
||||
//printf("fen = [%s]\nrev = [%s]", fen, revfen);
|
||||
//pos_print_raw(pos, 1);
|
||||
printf("[%s] -> [%s] **FIXED\n", fen, revfen);
|
||||
printf("-> [%s] **FIXED\n",revfen);
|
||||
pos_print_raw(pos, 1);
|
||||
}
|
||||
pos_del(pos);
|
||||
}
|
||||
|
80
test/movedo-test.c
Normal file
80
test/movedo-test.c
Normal 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;
|
||||
}
|
@@ -88,7 +88,7 @@ static void send_stockfish_fen(FILE *desc, pos_t *pos, char *fen)
|
||||
move_t *moves = pos->moves.move;
|
||||
int nmoves = pos->moves.nmoves;
|
||||
//char nodescount[] = "Nodes searched";
|
||||
printf("nmoves = %d\n", nmoves);
|
||||
//printf("nmoves = %d\n", nmoves);
|
||||
fflush(stdout);
|
||||
//sprintf(str, "stockfish \"position fen %s\ngo perft depth\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);
|
||||
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;
|
||||
// printf("fishcount=%d mycount=%d\n", fishcount, mycount);
|
||||
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 *skip = " ";
|
||||
move_t *m1 = fish->moves.move;
|
||||
move_t *m2 = me->moves.move;
|
||||
int n1 = fish->moves.nmoves;
|
||||
int n2 = me->moves.nmoves;
|
||||
move_t *m1 = fish->move;
|
||||
move_t *m2 = me->move;
|
||||
int n1 = fish->nmoves;
|
||||
int n2 = me->nmoves;
|
||||
|
||||
#define f(c) move_from(c)
|
||||
#define t(c) move_to(c)
|
||||
@@ -191,9 +215,13 @@ int main(int __unused ac, __unused char**av)
|
||||
FILE *outfd;
|
||||
char *fen;
|
||||
pos_t *pos, *fishpos = pos_new();
|
||||
movelist_t legal;
|
||||
//bitboard_t wrong = 0x5088000040, tmp, loop;
|
||||
//bit_for_each64(loop, tmp, )
|
||||
//printf("fishpos 1=%p\n", fishpos);
|
||||
|
||||
setlinebuf(stdout); /* line-buffered stdout */
|
||||
|
||||
bitboard_init();
|
||||
hyperbola_init();
|
||||
outfd = open_stockfish();
|
||||
@@ -201,15 +229,15 @@ int main(int __unused ac, __unused char**av)
|
||||
while ((fen = next_fen(MOVEGEN))) {
|
||||
//printf(">>>>> %s\n", test[i]);
|
||||
//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))) {
|
||||
printf("wrong fen %d: [%s]\n", i, fen);
|
||||
continue;
|
||||
}
|
||||
pos_print(pos);
|
||||
/* print movelists */
|
||||
send_stockfish_fen(outfd, fishpos, fen);
|
||||
gen_all_pseudomoves(pos);
|
||||
pos_gen_pseudomoves(pos);
|
||||
pos_all_legal(pos, &legal);
|
||||
//printf("Fu ");
|
||||
//moves_print(fishpos, 0);
|
||||
//fflush(stdout);
|
||||
@@ -218,8 +246,8 @@ int main(int __unused ac, __unused char**av)
|
||||
//fflush(stdout);
|
||||
|
||||
/* sort and print movelists */
|
||||
move_sort_by_sq(fishpos);
|
||||
move_sort_by_sq(pos);
|
||||
move_sort_by_sq(&fishpos->moves);
|
||||
move_sort_by_sq(&legal);
|
||||
// printf("\nFs ");
|
||||
// moves_print(fishpos, 0);
|
||||
// fflush(stdout);
|
||||
@@ -228,7 +256,20 @@ int main(int __unused ac, __unused char**av)
|
||||
// fflush(stdout);
|
||||
|
||||
/* 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);
|
||||
//printf("%s\n", pos2fen(pos, str));
|
||||
//get_stockfish_moves(test[i]);
|
||||
|
@@ -1,55 +1,20 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "brlib.h"
|
||||
|
||||
#include "../src/fen.h"
|
||||
#include "../src/position.h"
|
||||
#include "../src/bitboard.h"
|
||||
#include "piece.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) {
|
||||
printf("zoba\n");fflush(stdout);
|
||||
pos_startpos(pos);
|
||||
} else {
|
||||
fen2pos(pos, av[1]);
|
||||
piece_t p;
|
||||
char *test="PNBRQKpnbrqk";
|
||||
|
||||
for (u64 i = 0; i < sizeof(test); ++i) {
|
||||
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);
|
||||
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);
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user