16 Commits

Author SHA1 Message Date
856e3e52da add movedo-test 2024-03-21 07:00:20 +01:00
51a348c1e4 add pos_cmp (as a debug check after move_{do,undo}) 2024-03-21 06:58:56 +01:00
51c35e21f4 castling: lowercase macros, nove-{do,undo}: full (untested) version 2024-03-20 10:00:07 +01:00
3a06407d5a move: exclusive M_CAPTURE / M_EN_PASSANT 2024-03-20 09:58:42 +01:00
ae6328ce26 position: add captured piece 2024-03-20 09:57:10 +01:00
08064dd1a1 start bb-based move_do 2024-03-19 18:30:31 +01:00
49705bc707 rename few macros to lowercase, add M_DPUSH move flag 2024-03-19 18:30:10 +01:00
8527c3dee1 movegen: separate all captures (easier later handling) 2024-03-18 10:21:25 +01:00
dc916f5c56 move.h: revert flags to direct bit mask 2024-03-18 09:15:16 +01:00
748953a767 move-gen: separate pawn capture from en-passant (easier later testing) 2024-03-18 09:13:51 +01:00
9c2e76442d add a struct_group for move_do() irreversible changes 2024-03-17 17:34:21 +01:00
49302c7a60 lowercase move flags macros 2024-03-17 17:33:48 +01:00
db38b507ff add gmon.out 2024-03-17 17:30:51 +01:00
9d40a53aea simplify pos_next_legal() 2024-03-16 12:14:59 +01:00
260d8d34bd move.h: simplify flags. movegen.c: add pos_next_legal() 2024-03-16 10:06:55 +01:00
037d49e4ca move-do: copy from pre-bitboard version 2024-03-15 10:53:26 +01:00
18 changed files with 633 additions and 927 deletions

1
.gitignore vendored
View File

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

View File

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

View File

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

View File

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

View File

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

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

@@ -0,0 +1,172 @@
/* move-do.c - move do/undo.
*
* Copyright (C) 2021-2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING.
*
* You should have received a copy of the GNU General Public License along with this
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#include <malloc.h>
#include <ctype.h>
#include <stdlib.h>
#include "brlib.h"
#include "likely.h"
#include "chessdefs.h"
#include "move.h"
#include "position.h"
/**
* move_do() - do move.
* @pos: &pos_t position
* @move: move to apply
* @state: &state_t address where irreversible changes will be saved
*
* @move is applied to @pos:
* - bitboards and board are updated
* - counters are updated:
* - move count
* - 50-moves rule count
* - flags are possibly updated:
* - castling
* - en-passant
* - captured piece (excl. en-passant)
*
* @return: pos.
*/
pos_t *move_do(pos_t *pos, move_t move, state_t *state)
{
//# ifdef DEBUG_MOVE_DO
// move_print(move, M_PR_NL | M_PR_LONG);
//# endif
*state = pos->state; /* save irreversible changes */
color_t us = pos->turn, them = OPPONENT(us);
square_t from = move_from(move), to = move_to(move);
piece_t piece = pos->board[from];
piece_t new_piece = piece;
++pos->clock_50;
++pos->plycount;
pos->en_passant = SQUARE_NONE;
pos->turn = them;
if (is_promotion(move))
new_piece = MAKE_PIECE(move_promoted(move), us);
if (is_capture(move)) {
pos->clock_50 = 0;
pos->captured = pos->board[to]; /* save capture info */
pos_clr_sq(pos, to); /* clear square */
} else if (is_castle(move)) { /* handle rook move */
square_t rookfrom, rookto;
if (is_castle_K(move)) {
rookfrom = sq_rel(H1, us);
rookto = sq_rel(F1, us);
} else {
rookfrom = sq_rel(A1, us);
rookto = sq_rel(D1, us);
}
pos_set_sq(pos, rookto, pos->board[rookfrom]);
pos_clr_sq(pos, rookfrom);
pos->castle = clr_castle(pos->castle, us);
} else if (PIECE(piece) == PAWN) { /* pawn non capture or e.p. */
pos->clock_50 = 0;
if (is_dpush(move)) /* if pawn double push, set e.p. */
pos->en_passant = pawn_push_up(from, us);
else if (is_enpassant(move)) { /* clear grabbed pawn */
square_t grabbed = pawn_push_up(to, them);
pos_clr_sq(pos, grabbed);
}
}
pos_clr_sq(pos, from); /* always clear "from" and set "to" */
pos_set_sq(pos, to, new_piece);
/* update castling flags
* As we always consider flags are valid, we :
* - adjust our flags if relative from is "E1", "A1", H1"
* - adjust opp flags if relative to if "A8", H8"
*/
if (can_castle(pos->castle, us)) { /* do we save time with this test ? */
square_t rel_e1 = sq_rel(E1, us);
square_t rel_a1 = sq_rel(A1, us); square_t rel_h1 = sq_rel(H1, us);
if (from == rel_e1)
pos->castle = clr_castle(pos->castle, us);
else if (from == rel_a1)
pos->castle = clr_ooo(pos->castle, us);
else if (from == rel_h1)
pos->castle = clr_oo(pos->castle, us);
}
if (can_castle(pos->castle, them)) { /* do we save time with this test ? */
square_t rel_a8 = sq_rel(A8, us); square_t rel_h8 = sq_rel(H8, us);
if (to == rel_a8)
pos->castle = clr_ooo(pos->castle, them);
else if (to == rel_h8)
pos->castle = clr_oo(pos->castle, them);
}
return pos;
}
/**
* move_undo() - undo move.
* @pos: &pos_t position
* @move: move to undo
* @state: &state_t address where irreversible changes were saved
*
* @move is applied to @pos:
* - bitboards and board are updated
* - previous information is restored:
* - castling
* - en-passant
* - captured piece (excl. en-passant)
* - move count
* - 50-moves rule count
*
* @return: pos.
*/
pos_t *move_undo(pos_t *pos, const move_t move, const state_t *state)
{
//# ifdef DEBUG_MOVE
//log(1, "new move: ");
//move_print(0, move, M_PR_NL | M_PR_LONG);
//# endif
color_t them = pos->turn, us = OPPONENT(them);
square_t from = move_from(move), to = move_to(move);
piece_t piece = pos->board[to];
pos->state = *state; /* restore irreversible changes */
if (is_promotion(move))
piece = MAKE_PIECE(PAWN, us);
pos_clr_sq(pos, to); /* always clear "to" and set "from" */
pos_set_sq(pos, from, piece);
if (is_capture(move)) {
pos_set_sq(pos, to, pos->captured); /* restore captured piece */
} else if (is_castle(move)) { /* make reverse rook move */
square_t rookfrom, rookto;
if (is_castle_K(move)) {
rookfrom = sq_rel(F1, us);
rookto = sq_rel(H1, us);
} else {
rookfrom = sq_rel(D1, us);
rookto = sq_rel(A1, us);
}
pos_set_sq(pos, rookto, pos->board[rookfrom]);
pos_clr_sq(pos, rookfrom);
} else if (is_enpassant(move)) { /* restore grabbed pawn */
square_t grabbed = pawn_push_up(to, them);
pos_set_sq(pos, grabbed, MAKE_PIECE(PAWN, them));
}
return pos;
}

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

@@ -0,0 +1,20 @@
/* move-do.h - move do/undo.
*
* Copyright (C) 2021 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING.
*
* You should have received a copy of the GNU General Public License along with this
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#ifndef MOVE_DO_H
#define MOVE_DO_H
//extern pos_t *move_do(pos_t *pos, move_t *move);
//extern void move_undo(pos_t *pos, move_t *move);
#endif /* MOVE_DO_H */

View File

@@ -34,7 +34,7 @@
*
* @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), king = pos->king[us], sq;
@@ -79,7 +79,7 @@ bool pseudo_is_legal(pos_t *pos, move_t move)
* 5th relative rank. To do so, we create an occupation bb without
* the 2 pawns.
*/
if (move & M_CAPTURE && PIECE(pos->board[to]) == EMPTY) {
if (is_enpassant(move)) {
/* from rank bitboard */
bitboard_t rank5 = bb_sqrank[from];
/* enemy rooks/queens on from rank */
@@ -100,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
@@ -112,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
*
@@ -128,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;
@@ -137,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;
@@ -204,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) {
@@ -233,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);
@@ -255,21 +308,21 @@ 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, 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, occ, from-1, them)) { /* d1/d8 */
@@ -290,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;
}

View File

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

View File

@@ -107,11 +107,11 @@ 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);
sprintf(dst, "%s%s%n", sq_to_string(from), sq_to_string(to), &len);
if (move & M_PROMOTION) {
piece_t promoted = move_promoted(move);
sprintf(dst + len, "=%s", piece_to_low(promoted));
if (is_promotion(move)) {
piece_t promoted = (piece_t) move_promoted(move);
sprintf(dst + len, "%s", piece_to_low(promoted));
}
return dst;
}
@@ -127,7 +127,7 @@ char *move_str(char *dst, const move_t move, __unused const int flags)
* 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 move
* M_PR_NL: print a newline after each move
* M_PR_EVAL: print move eval
*/
void moves_print(movelist_t *moves, __unused int flags)
@@ -147,8 +147,8 @@ 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_t prom1 = move_promoted(m1);
piece_t prom2 = move_promoted(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 */
@@ -170,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);
* }
*/

View File

@@ -19,36 +19,59 @@
#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 ccc ppp 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
* ppp 3 15-17 piece_type_t 38000 (>>15)&7 promoted
* ccc 3 12-14 piece_type_t 7000 (>>12)&7 captured
* 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;
static inline square_t move_from(move_t move)
@@ -58,27 +81,27 @@ static inline square_t move_from(move_t move)
static inline square_t move_to(move_t move)
{
return (move >> 6) & 077;
return (move >> M_OFF_TO) & 077;
}
static inline piece_t move_promoted(move_t move)
static inline piece_type_t move_promoted(move_t move)
{
return (move >> 12) & 07;
return (move >> M_OFF_PROMOTED) & 07;
}
static inline piece_type_t move_captured(move_t move)
{
return (move >> 15) & 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)
@@ -86,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 << 12);
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 << 12);
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
@@ -109,28 +142,9 @@ static inline move_t move_make_promote_capture(square_t from, square_t to,
#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);
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);
//extern int pseudo_moves_castle(pos_t *pos, bool color, bool doit, bool do_king);
//int pseudo_moves_gen(pos_t *pos, piece_list_t *piece, bool doit, bool do_king);
//int pseudo_moves_pawn(pos_t *pos, piece_list_t *piece, bool doit);
//extern int moves_gen(pos_t *pos, bool color, bool doit, bool do_king);
//extern int moves_gen_king_moves(pos_t *pos, bool color, bool doit);
// extern void moves_sort(pos_t *pos);
//extern void moves_gen_eval_sort(pos_t *pos);
//extern void moves_gen_all(pos_t *pos);
//extern void moves_gen_all_nomoves(pos_t *pos);
//extern pos_t *move_do(pos_t *pos, move_t *move);
//extern void move_undo(pos_t *pos, move_t *move);
#endif /* MOVE_H */

View File

@@ -65,10 +65,6 @@ 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(piece_t p)
{

View File

@@ -47,14 +47,7 @@ pos_t *pos_new(void)
* pos_dup() - duplicate a position.
* @pos: &position to duplicate.
*
* New position is the same as source one (with duplicated pieces list),
* except:
* - moves list is empty
* - bestmove is NULL
* - nodecount is set to zero
* - eval is set to EVAL_INVALID
* - moves_generated ans moves_counted are unset
* - check is set to zero
* Return a copy, allocated with malloc(1), of @pos.
*
* @Return: The new position.
*
@@ -64,14 +57,7 @@ pos_t *pos_dup(const pos_t *pos)
{
pos_t *newpos = safe_malloc(sizeof(pos_t));
//board = new->board;
*newpos = *pos;
//new->bestmove = NULL;
newpos->node_count = 0;
//new->eval = EVAL_INVALID;
//new->moves_generated = false;
//new->moves_counted = false;
//new->check[WHITE] = new->check[BLACK] = 0;
return newpos;
}
@@ -87,6 +73,8 @@ void pos_del(pos_t *pos)
/**
* pos_clear() - clear a position.
* @pos: &position.
*
* @return: @pos.
*/
pos_t *pos_clear(pos_t *pos)
{
@@ -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
@@ -295,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;
@@ -304,12 +357,12 @@ void pos_print_pieces(const pos_t *pos)
p = pos->bb[color][piece];
count = popcount64(p);
cur = 0;
pname = piece_to_cap(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++;
}
}

View File

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

View File

@@ -14,11 +14,12 @@
#include <stdio.h>
#include "chessdefs.h"
/* when below FENs are in a struct with selection per test */
#define NOTEST 0
#define FEN 1
#define BITBOARD 2
#define MOVEGEN 4
#define ATTACK 8
#define NOTEST 0
#define FEN 1
#define BITBOARD 2
#define MOVEGEN 4
#define ATTACK 8
#define MOVEDO 16
struct fentest {
uint modules;
@@ -367,7 +368,18 @@ struct fentest {
"illegal, SF crash",
"2r1k3/3P4/8/8/8/8/8/4K3 w - - 0 1"
},
{ MOVEDO,
"simple movedo/undo: only 2 W knights",
"8/1k6/8/8/8/8/6K1/1NN5 w - - 0 1"
},
{ MOVEDO,
"simple movedo/undo: only 2 W knights",
"8/1k6/8/8/8/8/6K1/1NN5 w - - 0 1"
},
{ MOVEDO,
"simple movedo/undo: only 2 W knights",
"5n2/1k6/8/8/5K2/8/P7/1N6 w - - 0 1"
},
{ 0, NULL, NULL }
};

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

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

View File

@@ -237,7 +237,7 @@ int main(int __unused ac, __unused char**av)
/* print movelists */
send_stockfish_fen(outfd, fishpos, fen);
pos_gen_pseudomoves(pos);
pos_legalmoves(pos, &legal);
pos_all_legal(pos, &legal);
//printf("Fu ");
//moves_print(fishpos, 0);
//fflush(stdout);
@@ -263,8 +263,8 @@ int main(int __unused ac, __unused char**av)
printf("M: ");
moves_print(&legal, 0);
} else {
printf("[%s]\n\tMoves (OK): ", fen);
moves_print(&fishpos->moves, 0);
printf("[%s]: OK (%d Moves)\n", fen, legal.nmoves);
//moves_print(&fishpos->moves, 0);
}
//compare_moves(&fishpos->moves, &legal);
//} else {

0
util.c
View File