bug fix for moves count + move_add, clean debug

This commit is contained in:
2021-11-08 10:05:43 +01:00
parent d38e2f4543
commit e55c2e9cbe
2 changed files with 40 additions and 31 deletions

View File

@@ -40,19 +40,19 @@ pool_t *moves_pool_init()
return moves_pool; return moves_pool;
} }
void move_print(move_t *move, move_flags_t flags) int move_print(move_t *move, move_flags_t flags)
{ {
if (flags & M_PR_CAPT && !(move->flags & M_CAPTURE)) { if (flags & M_PR_CAPT && !(move->flags & M_CAPTURE)) {
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(4, "capture & %#04x\n", move->flags); log_i(5, "capture & %#04x\n", move->flags);
# endif # endif
return; return 0;
} }
if (flags & M_PR_NCAPT && move->flags & M_CAPTURE) { if (flags & M_PR_NCAPT && move->flags & M_CAPTURE) {
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(4, "!capture & %#04x\n", move->flags); log_i(5, "!capture & %#04x\n", move->flags);
# endif # endif
return; return 0;
} }
if (move->flags & M_CASTLE_K) { if (move->flags & M_CASTLE_K) {
printf("O-O"); printf("O-O");
@@ -81,6 +81,7 @@ void move_print(move_t *move, move_flags_t flags)
end: end:
printf(" "); printf(" ");
} }
return 1;
} }
void moves_print(pos_t *pos, move_flags_t flags) void moves_print(pos_t *pos, move_flags_t flags)
@@ -94,40 +95,43 @@ void moves_print(pos_t *pos, move_flags_t flags)
if (! (flags & M_PR_SEPARATE)) { if (! (flags & M_PR_SEPARATE)) {
list_for_each_safe(p_cur, tmp, &pos->moves) { list_for_each_safe(p_cur, tmp, &pos->moves) {
move = list_entry(p_cur, move_t, list); move = list_entry(p_cur, move_t, list);
i += move_print(move, details);
move_print(move, details);
i++;
} }
printf("\n\tTotal moves = %d\n", i); printf("\n\tTotal moves = %d\n", i);
} else { } else {
printf("captures: "); printf("captures: ");
list_for_each_safe(p_cur, tmp, &pos->moves) { list_for_each_safe(p_cur, tmp, &pos->moves) {
move = list_entry(p_cur, move_t, list); move = list_entry(p_cur, move_t, list);
i += move_print(move, details | M_PR_CAPT);
move_print(move, details | M_PR_CAPT);
i++;
} }
printf("\n\tothers : "); printf("\n\tothers : ");
list_for_each_safe(p_cur, tmp, &pos->moves) { list_for_each_safe(p_cur, tmp, &pos->moves) {
move = list_entry(p_cur, move_t, list); move = list_entry(p_cur, move_t, list);
i += move_print(move, details | M_PR_NCAPT);
move_print(move, details | M_PR_NCAPT);
i++;
} }
printf("\n\tTotal moves = %d\n", i); printf("\n\tTotal moves = %d\n", i);
} }
} }
inline static move_t *move_add(pos_t *pos, piece_t piece, square_t from, static move_t *move_add(pos_t *pos, piece_t piece, square_t from,
square_t to) square_t to)
{ {
board_t *board = pos->board; board_t *board = pos->board;
move_t *move; move_t *move;
# ifdef DEBUG_MOVE
log_i(2, "piece_color=%d turn=%d from=%c%c to=%c%c\n",
COLOR(piece), pos->turn,
FILE2C(GET_F(from)),
RANK2C(GET_R(from)),
FILE2C(GET_F(to)),
RANK2C(GET_R(to)));
# endif
if (COLOR(piece) != pos->turn) if (COLOR(piece) != pos->turn)
return NULL; return NULL;
if (!(move = pool_get(moves_pool))) if (!(move = pool_get(moves_pool)))
return NULL; return NULL;
move->piece = piece; move->piece = piece;
move->from = from; move->from = from;
move->to = to; move->to = to;
@@ -136,11 +140,18 @@ inline static move_t *move_add(pos_t *pos, piece_t piece, square_t from,
if (move->taken) if (move->taken)
move->flags |= M_CAPTURE; move->flags |= M_CAPTURE;
list_add(&move->list, &pos->moves); list_add(&move->list, &pos->moves);
# ifdef DEBUG_MOVE
log_i(2, "added move from %c%c to %c%c\n",
FILE2C(GET_F(move->from)),
RANK2C(GET_R(move->from)),
FILE2C(GET_F(move->to)),
RANK2C(GET_R(move->to)));
# endif
return move; return move;
} }
/* TODO: return nmoves */ /* TODO: return nmoves */
inline static move_t *move_pawn_add(pos_t *pos, piece_t piece, square_t from, static move_t *move_pawn_add(pos_t *pos, piece_t piece, square_t from,
square_t to, unsigned char rank7) square_t to, unsigned char rank7)
{ {
move_t *move; move_t *move;
@@ -308,10 +319,12 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece)
int count = 0; int count = 0;
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(4, "square=%#04x piece=%#04x color = %s\n", square, piece, log_f(2, "pos:%p turn:%s piece:%d [%s %s] at %#04x[%c%c]\n",
color==WHITE? "white": "black"); pos,
log_f(3, "pos:%p piece:%d [%s] at %#04x[%c%c]\n", pos, piece, pos->turn ==WHITE? "white": "black",
P_NAME(piece), square, piece,
color==WHITE? "white": "black", P_NAME(piece),
square,
FILE2C(GET_F(square)), RANK2C(GET_R(square))); FILE2C(GET_F(square)), RANK2C(GET_R(square)));
log_i(4, "vector=%ld ndirs=%d slide=%d\n", vector-vectors, ndirs, slide); log_i(4, "vector=%ld ndirs=%d slide=%d\n", vector-vectors, ndirs, slide);
# endif # endif
@@ -329,14 +342,13 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece)
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(3, "trying %c%c\n", FILE2C(GET_F(new)), RANK2C(GET_R(new))); log_i(3, "trying %c%c\n", FILE2C(GET_F(new)), RANK2C(GET_R(new)));
# endif # endif
/* own color on dest square */
pos->controlled[color] |= (1ULL << BB(FILE88(new), RANK88(new)));
if (board[new].piece) { if (board[new].piece) {
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(4, "color=%d color2=%d\n", color, COLOR(board[new].piece)); log_i(4, "color=%d color2=%d\n", color, COLOR(board[new].piece));
# endif # endif
} /* own color on dest square */
pos->controlled[color] |= (1ULL << BB(FILE88(new), RANK88(new)));
if (board[new].piece) {
if (COLOR(board[new].piece) == color) { if (COLOR(board[new].piece) == color) {
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(3, "skipping %04x (same color piece)\n", new); log_i(3, "skipping %04x (same color piece)\n", new);
@@ -347,13 +359,11 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece)
/* we are sure the move is valid : we create move */ /* we are sure the move is valid : we create move */
if (color == pos->turn) { if (color == pos->turn) {
if ((move = move_add(pos, piece, square, new))) { if ((move = move_add(pos, ppiece->piece, square, new))) {
count++; count++;
//if (move->taken)
// break;
} }
} }
if (board[new].piece) { if (board[new].piece) { /* stopper move */
break; break;
} }
if (!slide) if (!slide)
@@ -386,12 +396,11 @@ int moves_gen(pos_t *pos, bool color)
#ifdef BIN_move #ifdef BIN_move
#include "fen.h" #include "fen.h"
#include "debug.h"
int main(int ac, char**av) int main(int ac, char**av)
{ {
pos_t *pos; pos_t *pos;
debug_init(2); debug_init(1);
piece_pool_init(); piece_pool_init();
moves_pool_init(); moves_pool_init();
pos = pos_create(); pos = pos_create();

View File

@@ -51,7 +51,7 @@ typedef struct move_s {
} move_t; } move_t;
pool_t *moves_pool_init(); pool_t *moves_pool_init();
void move_print(move_t *move, move_flags_t flags); int move_print(move_t *move, move_flags_t flags);
void moves_print(pos_t *move, move_flags_t flags); void moves_print(pos_t *move, move_flags_t flags);
int pseudo_moves_castle(pos_t *pos); int pseudo_moves_castle(pos_t *pos);
int pseudo_moves_gen(pos_t *pos, piece_list_t *piece); int pseudo_moves_gen(pos_t *pos, piece_list_t *piece);