move.c: Code cleanup, add promotion in move_do
This commit is contained in:
114
src/move.c
114
src/move.c
@@ -112,7 +112,6 @@ 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;
|
||||||
//move_flags_t details = flags & M_PR_LONG;
|
|
||||||
int movenum;
|
int movenum;
|
||||||
|
|
||||||
for (int color=WHITE; color <= BLACK; ++color) {
|
for (int color=WHITE; color <= BLACK; ++color) {
|
||||||
@@ -145,7 +144,6 @@ void moves_print(pos_t *pos, move_flags_t flags)
|
|||||||
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)
|
||||||
{
|
{
|
||||||
//pos_t *newpos;
|
|
||||||
board_t *board = pos->board;
|
board_t *board = pos->board;
|
||||||
move_t *move;
|
move_t *move;
|
||||||
int color = COLOR(piece);
|
int color = COLOR(piece);
|
||||||
@@ -158,8 +156,6 @@ static move_t *move_add(pos_t *pos, piece_t piece, square_t from,
|
|||||||
FILE2C(F88(to)),
|
FILE2C(F88(to)),
|
||||||
RANK2C(R88(to)));
|
RANK2C(R88(to)));
|
||||||
# endif
|
# endif
|
||||||
//if (COLOR(piece) != pos->turn)
|
|
||||||
// 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) {
|
||||||
@@ -178,31 +174,8 @@ static move_t *move_add(pos_t *pos, piece_t piece, square_t 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->newpos = pos_dup(pos);
|
if (move->taken)
|
||||||
//newpos = move->newpos;
|
|
||||||
//SET_COLOR(newpos->turn, OPPONENT(move->piece));
|
|
||||||
//newpos->turn = OPPONENT(newpos->turn);
|
|
||||||
if (move->taken) {
|
|
||||||
move->flags |= M_CAPTURE;
|
move->flags |= M_CAPTURE;
|
||||||
/* remove taken piece from new position piece list
|
|
||||||
* this does not apply for en passant
|
|
||||||
*/
|
|
||||||
//piece_del(&newpos->board[to].s_piece->list);
|
|
||||||
/* remove occupied bitboard */
|
|
||||||
//newpos->occupied[OPPONENT(COLOR(piece))] ^= SQ88_2_BB(to);
|
|
||||||
}
|
|
||||||
/* always make "to" the piece square in new position */
|
|
||||||
//newpos->board[to] = newpos->board[from];
|
|
||||||
/* fix dest square */
|
|
||||||
//newpos->board[to].s_piece->square = to;
|
|
||||||
/* replace old occupied bitboard by new one */
|
|
||||||
//newpos->occupied[COLOR(piece)] ^= SQ88_2_BB(from);
|
|
||||||
//newpos->occupied[COLOR(piece)] |= SQ88_2_BB(to);
|
|
||||||
|
|
||||||
/* always make "from" square empty */
|
|
||||||
//newpos->board[from].piece = 0;
|
|
||||||
//newpos->board[from].s_piece = NULL;
|
|
||||||
|
|
||||||
# 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(F88(move->from)), RANK2C(R88(move->from)),
|
FILE2C(F88(move->from)), RANK2C(R88(move->from)),
|
||||||
@@ -211,6 +184,12 @@ static move_t *move_add(pos_t *pos, piece_t piece, square_t from,
|
|||||||
return move;
|
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)
|
void move_del(struct list_head *ptr)
|
||||||
{
|
{
|
||||||
move_t *move = list_entry(ptr, move_t, list);
|
move_t *move = list_entry(ptr, move_t, list);
|
||||||
@@ -221,15 +200,17 @@ void move_del(struct list_head *ptr)
|
|||||||
FILE2C(F88(move->to)), RANK2C(R88(move->to)));
|
FILE2C(F88(move->to)), RANK2C(R88(move->to)));
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
/* TODO: remove move->pos if non null */
|
|
||||||
//if (move->newpos) {
|
|
||||||
// pos_clear(move->newpos);
|
|
||||||
// }
|
|
||||||
list_del(ptr);
|
list_del(ptr);
|
||||||
pool_add(moves_pool, move);
|
pool_add(moves_pool, move);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* move_del() - delete all position moves.
|
||||||
|
* @ppos: &position.
|
||||||
|
*
|
||||||
|
* Remove all generated moves from @pos structure.
|
||||||
|
*/
|
||||||
int moves_del(pos_t *pos)
|
int moves_del(pos_t *pos)
|
||||||
{
|
{
|
||||||
struct list_head *p_cur, *tmp, *head;
|
struct list_head *p_cur, *tmp, *head;
|
||||||
@@ -244,7 +225,7 @@ int moves_del(pos_t *pos)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
# ifdef DEBUG_PIECE
|
# ifdef DEBUG_PIECE
|
||||||
log_f(3, "removed=%d\n", count);
|
log_f(3, "%d moves removed\n", count);
|
||||||
# endif
|
# endif
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
@@ -270,8 +251,14 @@ static move_t *move_pawn_add(pos_t *pos, piece_t piece, square_t from,
|
|||||||
return move;
|
return move;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* pawn moves. We do not test for valid destination square here,
|
/**
|
||||||
* assuming position is valid. Is that correct ?
|
* 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)
|
int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit)
|
||||||
{
|
{
|
||||||
@@ -317,7 +304,6 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit)
|
|||||||
# ifdef DEBUG_MOVE
|
# ifdef DEBUG_MOVE
|
||||||
log_i(4, "pushing pawn %#04x\n", square);
|
log_i(4, "pushing pawn %#04x\n", square);
|
||||||
# endif
|
# endif
|
||||||
//log_f(4, "pawn move mobility\n");
|
|
||||||
pos->mobility[color]++;
|
pos->mobility[color]++;
|
||||||
count++;
|
count++;
|
||||||
if (doit)
|
if (doit)
|
||||||
@@ -391,7 +377,20 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit)
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
int pseudo_moves_castle(pos_t *pos, bool color, bool doit, bool doking)
|
/**
|
||||||
|
* 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;
|
board_t *board = pos->board;
|
||||||
unsigned char rank1, castle_K, castle_Q;
|
unsigned char rank1, castle_K, castle_Q;
|
||||||
@@ -435,7 +434,7 @@ int pseudo_moves_castle(pos_t *pos, bool color, bool doit, bool doking)
|
|||||||
# endif
|
# endif
|
||||||
goto next;
|
goto next;
|
||||||
}
|
}
|
||||||
if (doking) {
|
if (do_king) {
|
||||||
pos->mobility[color]++;
|
pos->mobility[color]++;
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
@@ -461,7 +460,7 @@ next:
|
|||||||
# endif
|
# endif
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
if (doking) {
|
if (do_king) {
|
||||||
pos->mobility[color]++;
|
pos->mobility[color]++;
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
@@ -481,13 +480,13 @@ end:
|
|||||||
* @pos: &position
|
* @pos: &position
|
||||||
* @ppiece: &piece_list structure pointer
|
* @ppiece: &piece_list structure pointer
|
||||||
* @doit: add move to moves list
|
* @doit: add move to moves list
|
||||||
* @doking: count king moves
|
* @do_king: count king moves
|
||||||
*
|
*
|
||||||
* Calculate all possible moves for @ppiece.
|
* Calculate all possible moves for @ppiece.
|
||||||
* If @doit is true, add moves to @pos' moves list.
|
* If @doit is true, add moves to @pos' moves list.
|
||||||
* If @doking is true, account king moves (incl. castle) to mobility.
|
* 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 doking)
|
int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit, bool do_king)
|
||||||
{
|
{
|
||||||
piece_t piece = PIECE(ppiece->piece);
|
piece_t piece = PIECE(ppiece->piece);
|
||||||
unsigned char color = COLOR(ppiece->piece);
|
unsigned char color = COLOR(ppiece->piece);
|
||||||
@@ -553,14 +552,12 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit, bool doking)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* we are sure the move is valid : we create move */
|
/* we are sure the move is valid : we create move */
|
||||||
if (piece != KING || doking) {
|
if (piece != KING || do_king) {
|
||||||
pos->mobility[color]++;
|
pos->mobility[color]++;
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
if (doit) {
|
if (doit)
|
||||||
//move = move_add(pos, ppiece->piece, square, new);
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -586,13 +583,13 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit, bool doking)
|
|||||||
* @pos: &position
|
* @pos: &position
|
||||||
* @color: side
|
* @color: side
|
||||||
* @doit: add move to moves list
|
* @doit: add move to moves list
|
||||||
* @doking: count king moves
|
* @do_king: count king moves
|
||||||
*
|
*
|
||||||
* Calculate all possible moves for @color.
|
* Calculate all possible moves for @color.
|
||||||
* If @doit is true, add moves to @pos' moves list.
|
* If @doit is true, add moves to @pos' moves list.
|
||||||
* If @doking is true, account king moves (incl. castle) to mobility.
|
* If @do_king is true, account king moves (incl. castle) to mobility.
|
||||||
*/
|
*/
|
||||||
int moves_gen(pos_t *pos, bool color, bool doit, bool doking)
|
int moves_gen(pos_t *pos, bool color, bool doit, bool do_king)
|
||||||
{
|
{
|
||||||
struct list_head *p_cur, *tmp, *piece_list;
|
struct list_head *p_cur, *tmp, *piece_list;
|
||||||
piece_list_t *piece;
|
piece_list_t *piece;
|
||||||
@@ -605,11 +602,11 @@ int moves_gen(pos_t *pos, bool color, bool doit, bool doking)
|
|||||||
|
|
||||||
pos->mobility[color] = 0;
|
pos->mobility[color] = 0;
|
||||||
pos->controlled[color] = 0;
|
pos->controlled[color] = 0;
|
||||||
count += pseudo_moves_castle(pos, color, doit, doking);
|
count += pseudo_moves_castle(pos, color, doit, do_king);
|
||||||
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)
|
||||||
count += pseudo_moves_gen(pos, piece, doit, doking);
|
count += pseudo_moves_gen(pos, piece, doit, do_king);
|
||||||
else
|
else
|
||||||
count += pseudo_moves_pawn(pos, piece, doit);
|
count += pseudo_moves_pawn(pos, piece, doit);
|
||||||
|
|
||||||
@@ -648,7 +645,7 @@ int moves_gen_king_moves(pos_t *pos, bool color, bool doit)
|
|||||||
void moves_gen_all(pos_t *pos)
|
void moves_gen_all(pos_t *pos)
|
||||||
{
|
{
|
||||||
moves_gen(pos, OPPONENT(pos->turn), false, false);
|
moves_gen(pos, OPPONENT(pos->turn), false, false);
|
||||||
moves_gen(pos, pos->turn, false, true);
|
moves_gen(pos, pos->turn, true, true);
|
||||||
moves_gen_king_moves(pos, OPPONENT(pos->turn), true);
|
moves_gen_king_moves(pos, OPPONENT(pos->turn), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -722,9 +719,16 @@ pos_t *move_do(pos_t *pos, move_t *move)
|
|||||||
new->board[rook_from].s_piece = NULL;
|
new->board[rook_from].s_piece = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
new->board[to] = new->board[from];
|
if (move->flags & M_PROMOTION) {
|
||||||
/* fix dest square */
|
log(2, "promotion to %s\n", P_SYM(move->piece));
|
||||||
new->board[to].s_piece->square = to;
|
new->board[to].piece = move->promotion;
|
||||||
|
new->board[to].s_piece->square = to;
|
||||||
|
new->board[to].s_piece->piece = move->promotion;
|
||||||
|
} else {
|
||||||
|
new->board[to] = new->board[from];
|
||||||
|
/* fix dest square */
|
||||||
|
new->board[to].s_piece->square = to;
|
||||||
|
}
|
||||||
/* replace old occupied bitboard by new one */
|
/* replace old occupied bitboard by new one */
|
||||||
new->occupied[color] ^= SQ88_2_BB(from);
|
new->occupied[color] ^= SQ88_2_BB(from);
|
||||||
new->occupied[color] |= SQ88_2_BB(to);
|
new->occupied[color] |= SQ88_2_BB(to);
|
||||||
|
@@ -45,7 +45,6 @@ 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 *newpos; /* resulting position */
|
|
||||||
} move_t;
|
} move_t;
|
||||||
|
|
||||||
pool_t *moves_pool_init();
|
pool_t *moves_pool_init();
|
||||||
@@ -56,10 +55,10 @@ void moves_print(pos_t *move, move_flags_t flags);
|
|||||||
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, bool doking);
|
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 doking);
|
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);
|
int pseudo_moves_pawn(pos_t *pos, piece_list_t *piece, bool doit);
|
||||||
int moves_gen(pos_t *pos, bool color, bool doit, bool doking);
|
int moves_gen(pos_t *pos, bool color, bool doit, bool do_king);
|
||||||
int moves_gen_king_moves(pos_t *pos, bool color, bool doit);
|
int moves_gen_king_moves(pos_t *pos, bool color, bool doit);
|
||||||
void moves_gen_all(pos_t *pos);
|
void moves_gen_all(pos_t *pos);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user