pod_dup(): duplicate also pieces list

This commit is contained in:
2021-11-15 11:23:37 +01:00
parent f0e1836f2f
commit a28cd5c492
5 changed files with 39 additions and 22 deletions

View File

@@ -21,6 +21,7 @@
typedef struct board_s { typedef struct board_s {
piece_t piece; piece_t piece;
piece_list_t *s_piece; piece_list_t *s_piece;
//struct list_head *s_piece;
} board_t; /* 0x88 board */ } board_t; /* 0x88 board */
#define BOARDSIZE (8*8*2) #define BOARDSIZE (8*8*2)

View File

@@ -147,10 +147,10 @@ static move_t *move_add(pos_t *pos, piece_t piece, square_t from,
FILE2C(GET_F(to)), FILE2C(GET_F(to)),
RANK2C(GET_R(to))); RANK2C(GET_R(to)));
# endif # endif
/* impossible if opponent king is attacked
*/
if (COLOR(piece) != pos->turn) if (COLOR(piece) != pos->turn)
return NULL; return NULL;
/* invalid position if opponent king is attacked
*/
if (board[to].piece & KING) { if (board[to].piece & KING) {
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(2, "invalid position: opponent king [%c%c] is in check.\n", log_i(2, "invalid position: opponent king [%c%c] is in check.\n",
@@ -160,22 +160,20 @@ 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);
move->piece = piece; move->piece = piece;
move->from = from; move->from = from;
move->to = to; move->to = to;
move->taken = board[to].piece; move->taken = board[to].piece;
move->flags = M_NORMAL; move->flags = M_NORMAL;
move->pos = NULL;
if (move->taken) if (move->taken)
move->flags |= M_CAPTURE; move->flags |= M_CAPTURE;
list_add(&move->list, &pos->moves); move->newpos = pos_dup(pos);
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(3, "added move from %c%c to %c%c\n", log_i(3, "added move from %c%c to %c%c\n",
FILE2C(GET_F(move->from)), FILE2C(GET_F(move->from)), RANK2C(GET_R(move->from)),
RANK2C(GET_R(move->from)), FILE2C(GET_F(move->to)), RANK2C(GET_R(move->to)));
FILE2C(GET_F(move->to)),
RANK2C(GET_R(move->to)));
# endif # endif
return move; return move;
} }
@@ -191,8 +189,8 @@ void move_del(struct list_head *ptr)
# endif # endif
/* TODO: remove move->pos if non null */ /* TODO: remove move->pos if non null */
if (move->pos) { if (move->newpos) {
pos_clear(move->pos); pos_clear(move->newpos);
} }
list_del(ptr); list_del(ptr);
pool_add(moves_pool, move); pool_add(moves_pool, move);
@@ -223,6 +221,7 @@ static move_t *move_pawn_add(pos_t *pos, piece_t piece, square_t from,
move_t *move; move_t *move;
piece_t promote; piece_t promote;
unsigned char color = COLOR(piece); unsigned char color = COLOR(piece);
pos_t *newpos;
if (color != pos->turn) if (color != pos->turn)
return NULL; return NULL;
@@ -231,6 +230,8 @@ static move_t *move_pawn_add(pos_t *pos, piece_t piece, square_t from,
if ((move = move_add(pos, piece, from, to))) { if ((move = move_add(pos, piece, from, to))) {
move->flags |= M_PROMOTION; move->flags |= M_PROMOTION;
move->promotion = promote | color; move->promotion = promote | color;
newpos = move->newpos;
//piece_del(&newpos->board[from].s_piece);
} }
} }
} else { } else {
@@ -300,8 +301,10 @@ 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[vcolor]++; pos->mobility[vcolor]++;
if (doit && (move = move_pawn_add(pos, piece | color, square, new, rank7))) if (doit &&
(move = move_pawn_add(pos, piece | color, square, new, rank7))) {
count++; count++;
}
} }
} }
} }

View File

@@ -44,7 +44,7 @@ typedef struct move_s {
piece_t promotion; /* promoted piece */ piece_t promotion; /* promoted piece */
move_flags_t flags; move_flags_t flags;
struct list_head list; /* next move */ struct list_head list; /* next move */
struct pos_s *pos; /* resulting position */ struct pos_s *newpos; /* resulting position */
} move_t; } move_t;
pool_t *moves_pool_init(); pool_t *moves_pool_init();

View File

@@ -117,7 +117,10 @@ void *pool_get(pool_t *pool)
} }
pool_stats(pool); pool_stats(pool);
} }
return _pool_get(pool); /* this is the effective address if the object (and also the
* pool list_head address)
*/
return _pool_get(pool);
} }
#ifdef BIN_pool #ifdef BIN_pool

View File

@@ -180,22 +180,32 @@ pos_t *pos_get()
/* TODO: merge with pos_get - NULL for init, non null for duplicate */ /* TODO: merge with pos_get - NULL for init, non null for duplicate */
pos_t *pos_dup(pos_t *pos) pos_t *pos_dup(pos_t *pos)
{ {
struct list_head *p_cur, *tmp, *piece_list;
piece_list_t *oldpiece;
board_t *board;
pos_t *new = pool_get(pos_pool); pos_t *new = pool_get(pos_pool);
if (new) {
//printf("sizeof(board)=%lu\n", sizeof (board_t));
//new->board = malloc(sizeof (board_t)*BOARDSIZE);
//if (!new->board) {
// pool_add(pos_pool, new);
// return NULL;
//}
if (new) {
board = new->board;
*new = *pos; *new = *pos;
INIT_LIST_HEAD(&new->pieces[WHITE]); INIT_LIST_HEAD(&new->pieces[WHITE]);
INIT_LIST_HEAD(&new->pieces[BLACK]); INIT_LIST_HEAD(&new->pieces[BLACK]);
INIT_LIST_HEAD(&new->moves); INIT_LIST_HEAD(&new->moves);
//printf("board mem: %p-%p\n", pos->board, pos->board+sizeof (board_t));
/* duplicate piece list */
for (int color=0; color<2; ++color) {
piece_list = &pos->pieces[color]; /* white/black piece list */
list_for_each_safe(p_cur, tmp, piece_list) {
oldpiece = list_entry(p_cur, piece_list_t, list);
board[oldpiece->square].s_piece =
piece_add(new, oldpiece->piece, oldpiece->square);
}
}
} }
return pos; return new;
} }
pool_t *pos_pool_init() pool_t *pos_pool_init()