move list taken off from pos_t

This commit is contained in:
2024-03-21 22:09:06 +01:00
parent af593bdf87
commit 3ecd19eb10
8 changed files with 75 additions and 70 deletions

View File

@@ -1,6 +1,6 @@
# Makefile - brchess Makefile, **GNU make only**
#
# Copyright (C) 2021-2023 Bruno Raoult ("br")
# Copyright (C) 2021-2024 Bruno Raoult ("br")
# Licensed under the GNU General Public License v3.0 or later.
# Some rights reserved. See COPYING.
#

2
brlib

Submodule brlib updated: e55ac95301...8ff163dcf5

View File

@@ -103,7 +103,8 @@ bool pseudo_is_legal(const pos_t *pos, const move_t move)
/**
* pos_next_legal() - get next legal move in position.
* @pos: position
* @start: &int, starting position in move list
* @movelist: &pseudo-legal movelist
* @start: &int, starting position in @movelist
*
* 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.
@@ -111,10 +112,10 @@ bool pseudo_is_legal(const pos_t *pos, const move_t move)
*
* @return: move, or MOVE_NONE if no move.
*/
move_t pos_next_legal(const pos_t *pos, int *start)
move_t pos_next_legal(const pos_t *pos, movelist_t *movelist, int *start)
{
const int nmoves = pos->moves.nmoves;
const move_t *moves = pos->moves.move;
const int nmoves = movelist->nmoves;
const move_t *moves = movelist->move;
move_t move;
while (*start < nmoves) {
@@ -127,20 +128,20 @@ move_t pos_next_legal(const pos_t *pos, int *start)
/**
* pos_all_legal() - get the list of legal moves from pseudo-legal.
* @pos: position
* @dest: destination &movelist_t
* @movelist: &pseudo-legal movelist_t
* @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)
movelist_t *pos_all_legal(const pos_t *pos, movelist_t *movelist, movelist_t *dest)
{
int tmp = dest->nmoves = 0;
move_t move;
//int tmp = 0;
while ((move = pos_next_legal(pos, &tmp)) != MOVE_NONE)
while ((move = pos_next_legal(pos, movelist, &tmp)) != MOVE_NONE)
dest->move[dest->nmoves++] = move;
return dest;
}
@@ -148,9 +149,10 @@ movelist_t *pos_all_legal(const pos_t *pos, movelist_t *dest)
/**
* pos_gen_pseudomoves() - generate position pseudo-legal moves
* @pos: position
* @movelist: &movelist_t array to store pseudo-moves
*
* Generate all @pos pseudo moves for player-to-move.
* The @pos->moves table is filled with the moves.
* @movelist is filled with the moves.
*
* Only a few validity checks are done here (i.e. moves are not generated):
* - castling, if king is in check
@@ -167,7 +169,7 @@ movelist_t *pos_all_legal(const pos_t *pos, movelist_t *dest)
*
* @Return: The total number of moves.
*/
int pos_gen_pseudomoves(pos_t *pos)
int pos_gen_pseudomoves(pos_t *pos, movelist_t *movelist)
{
color_t us = pos->turn;
color_t them = OPPONENT(us);
@@ -181,43 +183,43 @@ int pos_gen_pseudomoves(pos_t *pos)
bitboard_t movebits, from_pawns;
bitboard_t tmp1, tmp2;
move_t *moves = pos->moves.move;
int nmoves = pos->moves.nmoves;
move_t *moves = movelist->move;
int *nmoves = &movelist->nmoves;
int from, to;
bug_on(nmoves != 0);
*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, tmp1, movebits & empty) {
moves[nmoves++] = move_make(from, to);
moves[(*nmoves)++] = move_make(from, to);
}
bit_for_each64(to, tmp1, movebits & enemy_pieces) {
moves[nmoves++] = move_make_capture(from, to);
moves[(*nmoves)++] = move_make_capture(from, to);
}
if (popcount64(pos->checkers) > 1) /* double check, we stop here */
return (pos->moves.nmoves = nmoves);
return *nmoves;
/* sliding pieces */
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 & empty) {
moves[nmoves++] = move_make(from, to);
moves[(*nmoves)++] = move_make(from, to);
}
bit_for_each64(to, tmp2, movebits & enemy_pieces) {
moves[nmoves++] = move_make_capture(from, to);
moves[(*nmoves)++] = move_make_capture(from, to);
}
}
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 & empty) {
moves[nmoves++] = move_make(from, to);
moves[(*nmoves)++] = move_make(from, to);
}
bit_for_each64(to, tmp2, movebits & enemy_pieces) {
moves[nmoves++] = move_make_capture(from, to);
moves[(*nmoves)++] = move_make_capture(from, to);
}
}
@@ -225,10 +227,10 @@ int pos_gen_pseudomoves(pos_t *pos)
bit_for_each64(from, tmp1, pos->bb[us][KNIGHT]) {
movebits = bb_knight_moves(not_my_pieces, from);
bit_for_each64(to, tmp2, movebits & empty) {
moves[nmoves++] = move_make(from, to);
moves[(*nmoves)++] = move_make(from, to);
}
bit_for_each64(to, tmp2, movebits & enemy_pieces) {
moves[nmoves++] = move_make_capture(from, to);
moves[(*nmoves)++] = move_make_capture(from, to);
}
}
@@ -241,13 +243,13 @@ int pos_gen_pseudomoves(pos_t *pos)
bit_for_each64(to, tmp1, movebits) {
from = pawn_push_up(to, them); /* reverse push */
//printf("push %d->%d %s->%s", from, to, sq_to_string(from), sq_to_string(to));
moves[nmoves++] = move_make(from, to);
moves[(*nmoves)++] = move_make(from, to);
}
/* possible second push */
movebits = pawn_shift_up(movebits & rel_rank3, us) & empty;
bit_for_each64(to, tmp1, movebits) {
from = pawn_push_up(pawn_push_up(to, them), them);
moves[nmoves++] = move_make_flags(from, to, M_DPUSH);
moves[(*nmoves)++] = move_make_flags(from, to, M_DPUSH);
}
/* pawn: ranks 2-6 captures left */
@@ -255,14 +257,14 @@ int pos_gen_pseudomoves(pos_t *pos)
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);
moves[(*nmoves)++] = move_make_capture(from, to);
}
/* pawn: ranks 2-6 captures right */
from_pawns = pos->bb[us][PAWN] & ~rel_rank7; // & ~rel_fileh;
movebits = pawn_shift_upright(from_pawns, us) & enemy_pieces;
bit_for_each64(to, tmp1, movebits) {
from = pawn_push_upright(to, them);
moves[nmoves++] = move_make_capture(from, to);
moves[(*nmoves)++] = move_make_capture(from, to);
}
/* pawn: en-passant */
@@ -271,7 +273,7 @@ int pos_gen_pseudomoves(pos_t *pos)
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);
moves[(*nmoves)++] = move_make_enpassant(from, to);
}
}
@@ -279,30 +281,30 @@ int pos_gen_pseudomoves(pos_t *pos)
movebits = pawn_shift_up(pos->bb[us][PAWN] & rel_rank7, us) & empty;
bit_for_each64(to, tmp1, movebits) {
from = pawn_push_up(to, them); /* reverse push */
moves[nmoves++] = move_make_promote(from, to, QUEEN);
moves[nmoves++] = move_make_promote(from, to, ROOK);
moves[nmoves++] = move_make_promote(from, to, BISHOP);
moves[nmoves++] = move_make_promote(from, to, KNIGHT);
moves[(*nmoves)++] = move_make_promote(from, to, QUEEN);
moves[(*nmoves)++] = move_make_promote(from, to, ROOK);
moves[(*nmoves)++] = move_make_promote(from, to, BISHOP);
moves[(*nmoves)++] = move_make_promote(from, to, KNIGHT);
}
/* pawn promotion: rank 7 captures left */
from_pawns = pos->bb[us][PAWN] & rel_rank7; // & ~rel_filea;
movebits = pawn_shift_upleft(from_pawns, us) & enemy_pieces;
bit_for_each64(to, tmp1, movebits) {
from = pawn_push_upleft(to, them); /* reverse capture */
moves[nmoves++] = move_make_promote_capture(from, to, QUEEN);
moves[nmoves++] = move_make_promote_capture(from, to, ROOK);
moves[nmoves++] = move_make_promote_capture(from, to, BISHOP);
moves[nmoves++] = move_make_promote_capture(from, to, KNIGHT);
moves[(*nmoves)++] = move_make_promote_capture(from, to, QUEEN);
moves[(*nmoves)++] = move_make_promote_capture(from, to, ROOK);
moves[(*nmoves)++] = move_make_promote_capture(from, to, BISHOP);
moves[(*nmoves)++] = move_make_promote_capture(from, to, KNIGHT);
}
/* pawn: rank 7 captures right */
from_pawns = pos->bb[us][PAWN] & rel_rank7; // & ~rel_fileh;
movebits = pawn_shift_upright(from_pawns, us) & enemy_pieces;
bit_for_each64(to, tmp1, movebits) {
from = pawn_push_upright(to, them); /* reverse capture */
moves[nmoves++] = move_make_promote_capture(from, to, QUEEN);
moves[nmoves++] = move_make_promote_capture(from, to, ROOK);
moves[nmoves++] = move_make_promote_capture(from, to, BISHOP);
moves[nmoves++] = move_make_promote_capture(from, to, KNIGHT);
moves[(*nmoves)++] = move_make_promote_capture(from, to, QUEEN);
moves[(*nmoves)++] = move_make_promote_capture(from, to, ROOK);
moves[(*nmoves)++] = move_make_promote_capture(from, to, BISHOP);
moves[(*nmoves)++] = move_make_promote_capture(from, to, KNIGHT);
}
/* castle - Attention ! Castling flags are assumed correct
@@ -319,14 +321,14 @@ int pos_gen_pseudomoves(pos_t *pos)
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);
moves[(*nmoves)++] = move_make_flags(from, from + 2, M_CASTLE_K);
}
}
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 */
moves[nmoves++] = move_make_flags(from, from - 2, M_CASTLE_Q);
moves[(*nmoves)++] = move_make_flags(from, from - 2, M_CASTLE_Q);
}
}
}
@@ -341,5 +343,5 @@ int pos_gen_pseudomoves(pos_t *pos)
* add function per piece, and type, for easier debug
*
*/
return (pos->moves.nmoves = nmoves);
return *nmoves;
}

View File

@@ -22,8 +22,8 @@
#include "move.h"
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);
move_t pos_next_legal(const pos_t *pos, movelist_t *movelist, int *start);
movelist_t *pos_all_legal(const pos_t *pos, movelist_t *movelist, movelist_t *dest);
int pos_gen_pseudomoves(pos_t *pos, movelist_t *movelist);
#endif /* MOVEGEN_H */

View File

@@ -105,8 +105,7 @@ pos_t *pos_clear(pos_t *pos)
pos->pinners = 0;
pos->blockers = 0;
//pos->moves.curmove = 0;
pos->moves.nmoves = 0;
//pos->moves.nmoves = 0;
return pos;
}
@@ -157,11 +156,13 @@ bool pos_cmp(const pos_t *pos1, const pos_t *pos2)
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;
/*
* 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:

View File

@@ -52,7 +52,7 @@ typedef struct __pos_s {
bitboard_t checkers; /* opponent checkers */
bitboard_t pinners; /* opponent pinners */
bitboard_t blockers; /* pieces blocking pin */
movelist_t moves;
//movelist_t moves;
} pos_t;
typedef struct state_s state_t;

View File

@@ -29,6 +29,7 @@ int main(int __unused ac, __unused char**av)
int i = 1;
char *fen, movebuf[8];;
pos_t *pos, *savepos;
movelist_t pseudo;
move_t move;
setlinebuf(stdout); /* line-buffered stdout */
@@ -41,14 +42,14 @@ int main(int __unused ac, __unused char**av)
printf("wrong fen %d: [%s]\n", i, fen);
continue;
}
pos_gen_pseudomoves(pos);
pos_gen_pseudomoves(pos, &pseudo);
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) {
while ((move = pos_next_legal(pos, &pseudo, &tmp)) != MOVE_NONE) {
state_t state;
pos_print(pos);

View File

@@ -76,7 +76,7 @@ static FILE *open_stockfish()
return out_desc;
}
static void send_stockfish_fen(FILE *desc, pos_t *pos, char *fen)
static void send_stockfish_fen(FILE *desc, pos_t *pos, movelist_t *movelist, char *fen)
{
char *buf = NULL;
int count, __unused mycount = 0, fishcount;
@@ -85,8 +85,9 @@ static void send_stockfish_fen(FILE *desc, pos_t *pos, char *fen)
pos_clear(pos);
move_t *moves = pos->moves.move;
int nmoves = pos->moves.nmoves;
move_t *moves = movelist->move;
int *nmoves = &movelist->nmoves;
*nmoves = 0;
//char nodescount[] = "Nodes searched";
//printf("nmoves = %d\n", nmoves);
fflush(stdout);
@@ -109,7 +110,7 @@ static void send_stockfish_fen(FILE *desc, pos_t *pos, char *fen)
// buf[0], buf[1], buf[2], buf[3],
// sq_to_string(from), sq_to_string(to),
// count);
moves[nmoves++] = move_make(from, to);
moves[(*nmoves)++] = move_make(from, to);
} else if (sscanf(buf, "%*5s: %d", &count) == 1) {
square_t from = sq_from_string(buf);
@@ -120,10 +121,10 @@ static void send_stockfish_fen(FILE *desc, pos_t *pos, char *fen)
// 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);
moves[(*nmoves)++] = move_make_promote(from, to, promoted);
}
}
pos->moves.nmoves = nmoves;
//pos->moves.nmoves = nmoves;
// printf("fishcount=%d mycount=%d\n", fishcount, mycount);
free(buf);
}
@@ -215,7 +216,7 @@ int main(int __unused ac, __unused char**av)
FILE *outfd;
char *fen;
pos_t *pos, *fishpos = pos_new();
movelist_t legal;
movelist_t pseudo, legal, fishmoves;
//bitboard_t wrong = 0x5088000040, tmp, loop;
//bit_for_each64(loop, tmp, )
//printf("fishpos 1=%p\n", fishpos);
@@ -235,9 +236,9 @@ int main(int __unused ac, __unused char**av)
continue;
}
/* print movelists */
send_stockfish_fen(outfd, fishpos, fen);
pos_gen_pseudomoves(pos);
pos_all_legal(pos, &legal);
send_stockfish_fen(outfd, fishpos, &fishmoves, fen);
pos_gen_pseudomoves(pos, &pseudo);
pos_all_legal(pos, &pseudo, &legal);
//printf("Fu ");
//moves_print(fishpos, 0);
//fflush(stdout);
@@ -246,7 +247,7 @@ int main(int __unused ac, __unused char**av)
//fflush(stdout);
/* sort and print movelists */
move_sort_by_sq(&fishpos->moves);
move_sort_by_sq(&fishmoves);
move_sort_by_sq(&legal);
// printf("\nFs ");
// moves_print(fishpos, 0);
@@ -256,10 +257,10 @@ int main(int __unused ac, __unused char**av)
// fflush(stdout);
/* compare movelists */
if (!movelists_equal(&fishpos->moves, &legal)) {
if (!movelists_equal(&fishmoves, &legal)) {
pos_print(pos);
printf("F: ");
moves_print(&fishpos->moves, 0);
moves_print(&fishmoves, 0);
printf("M: ");
moves_print(&legal, 0);
} else {