moge gen: all moves count as mobility

This commit is contained in:
2023-06-25 16:01:13 +02:00
parent 5bd8f9042a
commit 1e4af66379
2 changed files with 128 additions and 89 deletions

View File

@@ -65,13 +65,13 @@ 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(5, "capture & %#04x\n", move->flags); log_i(9, "skipping capture & %#04x\n", move->flags);
# endif # endif
return 0; 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(5, "!capture & %#04x\n", move->flags); log_i(9, "skipping !capture & %#04x\n", move->flags);
# endif # endif
return 0; return 0;
} }
@@ -109,28 +109,29 @@ void moves_print(pos_t *pos, move_flags_t flags)
{ {
struct list_head *p_cur, *tmp; struct list_head *p_cur, *tmp;
move_t *move; move_t *move;
int i = 0;
move_flags_t details = flags & M_PR_LONG; move_flags_t details = flags & M_PR_LONG;
printf("%s pseudo-moves:\n\t", pos->turn == WHITE? "White": "Black"); for (int color=WHITE; color <= BLACK; ++color) {
int verif = 0;
printf("%s pseudo-moves:\n\t", color == WHITE? "White": "Black");
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[color]) {
move = list_entry(p_cur, move_t, list); move = list_entry(p_cur, move_t, list);
i += move_print(move, details); verif += move_print(move, details);
} }
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[color]) {
move = list_entry(p_cur, move_t, list); move = list_entry(p_cur, move_t, list);
i += move_print(move, details | M_PR_CAPT); verif += move_print(move, details | M_PR_CAPT);
} }
printf("\n\tothers : "); printf("\n\tothers : ");
list_for_each_safe(p_cur, tmp, &pos->moves) { list_for_each_safe(p_cur, tmp, &pos->moves[color]) {
move = list_entry(p_cur, move_t, list); move = list_entry(p_cur, move_t, list);
i += move_print(move, details | M_PR_NCAPT); verif += move_print(move, details | M_PR_NCAPT);
} }
printf("\n\tTotal moves = %d\n", i); }
printf("\n\tTotal moves = %d mobility=%u\n", verif, pos->mobility[color]);
} }
} }
@@ -140,17 +141,18 @@ static move_t *move_add(pos_t *pos, piece_t piece, square_t from,
board_t *board = pos->board; board_t *board = pos->board;
pos_t *newpos; pos_t *newpos;
move_t *move; move_t *move;
int color = COLOR(piece);
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(3, "piece_color=%d turn=%d from=%c%c to=%c%c\n", log_i(3, "piece_color=%d turn=%d from=%c%c to=%c%c\n",
COLOR(piece), pos->turn, color, pos->turn,
FILE2C(F88(from)), FILE2C(F88(from)),
RANK2C(R88(from)), RANK2C(R88(from)),
FILE2C(F88(to)), FILE2C(F88(to)),
RANK2C(R88(to))); RANK2C(R88(to)));
# endif # endif
if (COLOR(piece) != pos->turn) //if (COLOR(piece) != pos->turn)
return NULL; // return NULL;
/* invalid position if opponent king is attacked /* invalid position if opponent king is attacked
*/ */
if (board[to].piece & KING) { if (board[to].piece & KING) {
@@ -162,7 +164,7 @@ static move_t *move_add(pos_t *pos, piece_t piece, square_t from,
} }
if (!(move = pool_get(moves_pool))) if (!(move = pool_get(moves_pool)))
return NULL; return NULL;
list_add(&move->list, &pos->moves); list_add(&move->list, &pos->moves[color]);
move->piece = piece; move->piece = piece;
move->from = from; move->from = from;
@@ -226,12 +228,14 @@ int moves_del(pos_t *pos)
struct list_head *p_cur, *tmp, *head; struct list_head *p_cur, *tmp, *head;
int count = 0; int count = 0;
head = &pos->moves; for (int color = WHITE; color <= BLACK; ++color) {
head = &pos->moves[color];
list_for_each_safe(p_cur, tmp, head) { list_for_each_safe(p_cur, tmp, head) {
move_del(p_cur); move_del(p_cur);
count++; count++;
} }
}
# ifdef DEBUG_PIECE # ifdef DEBUG_PIECE
log_f(3, "removed=%d\n", count); log_f(3, "removed=%d\n", count);
# endif # endif
@@ -247,8 +251,8 @@ static move_t *move_pawn_add(pos_t *pos, piece_t piece, square_t from,
unsigned char color = COLOR(piece); unsigned char color = COLOR(piece);
pos_t *newpos; pos_t *newpos;
if (color != pos->turn) //if (color != pos->turn)
return NULL; // return NULL;
if (R88(from) == rank7) { /* promotion */ if (R88(from) == rank7) { /* promotion */
for (promote = QUEEN; promote > PAWN; promote >>= 1) { for (promote = QUEEN; promote > PAWN; promote >>= 1) {
if ((move = move_add(pos, piece, from, to))) { if ((move = move_add(pos, piece, from, to))) {
@@ -320,10 +324,12 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit)
# endif # endif
//log_f(4, "pawn move mobility\n"); //log_f(4, "pawn move mobility\n");
pos->mobility[color]++; pos->mobility[color]++;
if (doit && (move = move_pawn_add(pos, piece | color, square, new, rank7)))
count++; count++;
if (doit)
move_pawn_add(pos, piece | color, square, new, rank7);
/* push 2 squares */ /* push 2 squares */
if (move && R88(square) == rank2) { log(4, "R88(%#x)=%d R2=%d \n", square, R88(square), rank2);
if (R88(square) == rank2) {
new += dir * 16; new += dir * 16;
if (SQ88_OK(new) && !board[new].piece) { if (SQ88_OK(new) && !board[new].piece) {
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
@@ -331,16 +337,15 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit)
# endif # endif
//log_f(2, "pawn move2 mobility\n"); //log_f(2, "pawn move2 mobility\n");
pos->mobility[color]++; pos->mobility[color]++;
if (doit &&
(move = move_pawn_add(pos, piece | color, square, new, rank7))) {
count++; count++;
} if (doit)
move_pawn_add(pos, piece | color, square, new, rank7);
} }
} }
} }
/* en passant - not accounted for mobility. Correct ? */ /* en passant - not accounted for mobility. Correct ? */
if (doit && pos->en_passant && R88(square) == rank5) { if (pos->en_passant && R88(square) == rank5) {
unsigned char ep_file = F88(pos->en_passant); unsigned char ep_file = F88(pos->en_passant);
unsigned char sq_file = F88(square); unsigned char sq_file = F88(square);
@@ -352,7 +357,6 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit)
square_t t_square = SQ88(ep_file, rank5); /* taken pawn square */ square_t t_square = SQ88(ep_file, rank5); /* taken pawn square */
piece_t taken = board[t_square].piece; piece_t taken = board[t_square].piece;
move = move_pawn_add(pos, piece | color , square, pos->en_passant, rank7); move = move_pawn_add(pos, piece | color , square, pos->en_passant, rank7);
count++;
move->flags |= M_EN_PASSANT | M_CAPTURE; move->flags |= M_EN_PASSANT | M_CAPTURE;
move->taken = taken; move->taken = taken;
@@ -361,6 +365,7 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit)
move->newpos->board[t_square].piece = 0; move->newpos->board[t_square].piece = 0;
move->newpos->board[t_square].s_piece = NULL; move->newpos->board[t_square].s_piece = NULL;
pos->mobility[color]++;
count++; count++;
} }
@@ -378,17 +383,28 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit)
if (board[new].piece && COLOR(board[new].piece) != color) { if (board[new].piece && COLOR(board[new].piece) != color) {
//log_f(2, "pawn capture mobility\n"); //log_f(2, "pawn capture mobility\n");
pos->mobility[color]++; pos->mobility[color]++;
if (doit &&
((move = move_pawn_add(pos, piece | color, square, new, rank7))))
count++; 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; return count;
} }
int pseudo_moves_castle(pos_t *pos) int pseudo_moves_castle(pos_t *pos, bool color, bool doit)
{ {
unsigned char color = pos->turn; //unsigned char color = pos->turn;
board_t *board = pos->board; board_t *board = pos->board;
unsigned char rank1, castle_K, castle_Q; unsigned char rank1, castle_K, castle_Q;
move_t *move = NULL; move_t *move = NULL;
@@ -399,9 +415,10 @@ int pseudo_moves_castle(pos_t *pos)
pos_t *newpos; pos_t *newpos;
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_f(2, "pos:%p turn:%s\n", log_f(2, "pos:%p turn:%s color:%s\n",
pos, pos,
IS_WHITE(pos->turn)? "white": "black"); IS_WHITE(pos->turn)? "white": "black",
IS_WHITE(color)? "white": "black");
# endif # endif
if (IS_WHITE(color)) { if (IS_WHITE(color)) {
@@ -419,13 +436,20 @@ int pseudo_moves_castle(pos_t *pos)
} }
if (castle_K) { if (castle_K) {
if (occupied & can_castle->occupied[1]) { if (occupied & can_castle->occupied[1]) {
printf("Cannot castle K side: occupied\n"); # ifdef DEBUG_MOVE
log(5, "Cannot castle K side: occupied\n");
# endif
goto next; goto next;
} }
if (controlled & can_castle->controlled[1]) { if (controlled & can_castle->controlled[1]) {
printf("Cannot castle K side: controlled\n"); # ifdef DEBUG_MOVE
log(5, "Cannot castle K side: controlled\n");
# endif
goto next; goto next;
} }
pos->mobility[color]++;
count++;
if (doit) {
move = move_add(pos, board[SQ88(4, rank1)].piece, move = move_add(pos, board[SQ88(4, rank1)].piece,
SQ88(4, rank1), SQ88(6, rank1)); SQ88(4, rank1), SQ88(6, rank1));
if (move) { if (move) {
@@ -438,20 +462,27 @@ int pseudo_moves_castle(pos_t *pos)
newpos->board[SQ88(7, rank1)].piece = 0; newpos->board[SQ88(7, rank1)].piece = 0;
newpos->board[SQ88(7, rank1)].s_piece = NULL; newpos->board[SQ88(7, rank1)].s_piece = NULL;
count++; }
} }
} }
next: next:
if (castle_Q) { if (castle_Q) {
if (occupied & can_castle->occupied[0]) { if (occupied & can_castle->occupied[0]) {
printf("Cannot castle Q side: occupied\n"); # ifdef DEBUG_MOVE
log(5, "Cannot castle Q side: occupied\n");
# endif
goto end; goto end;
} }
if (controlled & can_castle->controlled[0]) { if (controlled & can_castle->controlled[0]) {
printf("Cannot castle Q side: controlled\n"); # ifdef DEBUG_MOVE
log(5, "Cannot castle Q side: controlled\n");
# endif
goto end; goto end;
} }
pos->mobility[color]++;
count++;
if (doit) {
move = move_add(pos, board[SQ88(4, rank1)].piece, move = move_add(pos, board[SQ88(4, rank1)].piece,
SQ88(4, rank1), SQ88(2, rank1)); SQ88(4, rank1), SQ88(2, rank1));
if (move) { if (move) {
@@ -462,8 +493,7 @@ next:
SETF88(newpos->board[SQ88(3, rank1)].s_piece->square, 3); SETF88(newpos->board[SQ88(3, rank1)].s_piece->square, 3);
newpos->board[SQ88(0, rank1)].piece = 0; newpos->board[SQ88(0, rank1)].piece = 0;
newpos->board[SQ88(0, rank1)].s_piece = NULL; newpos->board[SQ88(0, rank1)].s_piece = NULL;
}
count++;
} }
} }
end: end:
@@ -481,7 +511,7 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit)
unsigned char ndirs = vector->ndirs; unsigned char ndirs = vector->ndirs;
char slide = vector->slide; char slide = vector->slide;
board_t *board = pos->board; board_t *board = pos->board;
move_t *move; //move_t *move;
int count = 0; int count = 0;
u64 bb_new; u64 bb_new;
@@ -540,10 +570,10 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit)
/* we are sure the move is valid : we create move */ /* we are sure the move is valid : we create move */
//log_f(2, "piece mobility\n"); //log_f(2, "piece mobility\n");
pos->mobility[color]++; pos->mobility[color]++;
if (doit && color == pos->turn) {
if ((move = move_add(pos, ppiece->piece, square, new))) {
count++; count++;
} if (doit) {
//move = move_add(pos, ppiece->piece, square, new);
move_add(pos, ppiece->piece, square, new);
} }
if (board[new].piece) { /* stopper move */ if (board[new].piece) { /* stopper move */
break; break;
@@ -552,6 +582,16 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit)
break; 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; return count;
} }
@@ -568,15 +608,13 @@ int moves_gen(pos_t *pos, bool color, bool doit)
pos->mobility[color] = 0; pos->mobility[color] = 0;
pos->controlled[color] = 0; pos->controlled[color] = 0;
if (doit) count += pseudo_moves_castle(pos, color, doit);
pseudo_moves_castle(pos);
list_for_each_safe(p_cur, tmp, piece_list) { list_for_each_safe(p_cur, tmp, piece_list) {
piece = list_entry(p_cur, piece_list_t, list); piece = list_entry(p_cur, piece_list_t, list);
if (PIECE(piece->piece) != PAWN) if (PIECE(piece->piece) != PAWN)
pseudo_moves_gen(pos, piece, doit); count += pseudo_moves_gen(pos, piece, doit);
else else
pseudo_moves_pawn(pos, piece, doit); count += pseudo_moves_pawn(pos, piece, doit);
count++; count++;
} }
@@ -599,20 +637,21 @@ int main(int ac, char**av)
{ {
pos_t *pos; pos_t *pos;
debug_init(1); debug_level_set(5);
piece_pool_init(); piece_pool_init();
moves_pool_init(); moves_pool_init();
pos_pool_init(); pos_pool_init();
pos = pos_get(); pos = pos_get();
if (ac == 1) { if (ac == 1) {
pos_startpos(pos); fen2pos(pos, "rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2");
//pos_startpos(pos);
} else { } else {
fen2pos(pos, av[1]); fen2pos(pos, av[1]);
} }
//printf("turn = %d opponent = %d\n", pos->turn, OPPONENT(pos->turn)); //printf("turn = %d opponent = %d\n", pos->turn, OPPONENT(pos->turn));
moves_gen(pos, OPPONENT(pos->turn), false); moves_gen(pos, WHITE, false);
moves_gen(pos, pos->turn, true); moves_gen(pos, BLACK, false);
pos_print(pos); pos_print(pos);
pos_pieces_print(pos); pos_pieces_print(pos);
moves_print(pos, M_PR_SEPARATE); moves_print(pos, M_PR_SEPARATE);

View File

@@ -51,11 +51,11 @@ pool_t *moves_pool_init();
void moves_pool_stats(); void moves_pool_stats();
int 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);
void move_del(struct list_head *ptr); void move_del(struct list_head *ptr);
int moves_del(pos_t *pos); int moves_del(pos_t *pos);
int pseudo_moves_castle(pos_t *pos, bool color, bool doit);
int pseudo_moves_gen(pos_t *pos, piece_list_t *piece, bool doit); int pseudo_moves_gen(pos_t *pos, piece_list_t *piece, bool doit);
int pseudo_moves_pawn(pos_t *pos, piece_list_t *piece, bool doit); int pseudo_moves_pawn(pos_t *pos, piece_list_t *piece, bool doit);
int moves_gen(pos_t *pos, bool color, bool doit); int moves_gen(pos_t *pos, bool color, bool doit);