1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-05-01 01:03:09 +00:00

Space inflate movegen.cpp

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2008-10-17 15:11:19 +02:00
parent cf8ee79b76
commit 158911425b
2 changed files with 228 additions and 250 deletions

View file

@ -32,21 +32,16 @@
namespace { namespace {
int generate_white_pawn_captures(const Position &pos, MoveStack *mlist); int generate_white_pawn_captures(const Position&, MoveStack*);
int generate_black_pawn_captures(const Position &pos, MoveStack *mlist); int generate_black_pawn_captures(const Position&, MoveStack*);
int generate_white_pawn_noncaptures(const Position &pos, MoveStack *mlist); int generate_white_pawn_noncaptures(const Position&, MoveStack*);
int generate_black_pawn_noncaptures(const Position &pos, MoveStack *mlist); int generate_black_pawn_noncaptures(const Position&, MoveStack*);
int generate_knight_moves(const Position &pos, MoveStack *mlist, int generate_knight_moves(const Position&, MoveStack*, Color side, Bitboard t);
Color side, Bitboard target); int generate_bishop_moves(const Position&, MoveStack*, Color side, Bitboard t);
int generate_bishop_moves(const Position &pos, MoveStack *mlist, int generate_rook_moves(const Position&, MoveStack*, Color side, Bitboard t);
Color side, Bitboard target); int generate_queen_moves(const Position&, MoveStack*, Color side, Bitboard t);
int generate_rook_moves(const Position &pos, MoveStack *mlist, int generate_king_moves(const Position&, MoveStack*, Square from, Bitboard t);
Color side, Bitboard target); int generate_castle_moves(const Position&, MoveStack*, Color us);
int generate_queen_moves(const Position &pos, MoveStack *mlist,
Color side, Bitboard target);
int generate_king_moves(const Position &pos, MoveStack *mlist,
Square from, Bitboard target);
int generate_castle_moves(const Position &pos, MoveStack *mlist, Color us);
} }
@ -60,23 +55,24 @@ namespace {
/// promotions. The return value is the number of moves generated. /// promotions. The return value is the number of moves generated.
int generate_captures(const Position& pos, MoveStack* mlist) { int generate_captures(const Position& pos, MoveStack* mlist) {
Color us = pos.side_to_move();
Bitboard target = pos.pieces_of_color(opposite_color(us));
int n = 0;
assert(pos.is_ok()); assert(pos.is_ok());
assert(!pos.is_check()); assert(!pos.is_check());
Color us = pos.side_to_move();
Bitboard target = pos.pieces_of_color(opposite_color(us));
int n;
if (us == WHITE) if (us == WHITE)
n += generate_white_pawn_captures(pos, mlist); n = generate_white_pawn_captures(pos, mlist);
else else
n += generate_black_pawn_captures(pos, mlist); n = generate_black_pawn_captures(pos, mlist);
n += generate_knight_moves(pos, mlist+n, us, target); n += generate_knight_moves(pos, mlist+n, us, target);
n += generate_bishop_moves(pos, mlist+n, us, target); n += generate_bishop_moves(pos, mlist+n, us, target);
n += generate_rook_moves(pos, mlist+n, us, target); n += generate_rook_moves(pos, mlist+n, us, target);
n += generate_queen_moves(pos, mlist+n, us, target); n += generate_queen_moves(pos, mlist+n, us, target);
n += generate_king_moves(pos, mlist+n, pos.king_square(us), target); n += generate_king_moves(pos, mlist+n, pos.king_square(us), target);
return n; return n;
} }
@ -85,24 +81,25 @@ int generate_captures(const Position &pos, MoveStack *mlist) {
/// underpromotions. The return value is the number of moves generated. /// underpromotions. The return value is the number of moves generated.
int generate_noncaptures(const Position& pos, MoveStack *mlist) { int generate_noncaptures(const Position& pos, MoveStack *mlist) {
Color us = pos.side_to_move();
Bitboard target = pos.empty_squares();
int n = 0;
assert(pos.is_ok()); assert(pos.is_ok());
assert(!pos.is_check()); assert(!pos.is_check());
Color us = pos.side_to_move();
Bitboard target = pos.empty_squares();
int n;
if (us == WHITE) if (us == WHITE)
n += generate_white_pawn_noncaptures(pos, mlist); n = generate_white_pawn_noncaptures(pos, mlist);
else else
n += generate_black_pawn_noncaptures(pos, mlist); n = generate_black_pawn_noncaptures(pos, mlist);
n += generate_knight_moves(pos, mlist+n, us, target); n += generate_knight_moves(pos, mlist+n, us, target);
n += generate_bishop_moves(pos, mlist+n, us, target); n += generate_bishop_moves(pos, mlist+n, us, target);
n += generate_rook_moves(pos, mlist+n, us, target); n += generate_rook_moves(pos, mlist+n, us, target);
n += generate_queen_moves(pos, mlist+n, us, target); n += generate_queen_moves(pos, mlist+n, us, target);
n += generate_king_moves(pos, mlist+n, pos.king_square(us), target); n += generate_king_moves(pos, mlist+n, pos.king_square(us), target);
n += generate_castle_moves(pos, mlist+n, us); n += generate_castle_moves(pos, mlist+n, us);
return n; return n;
} }
@ -112,14 +109,15 @@ int generate_noncaptures(const Position &pos, MoveStack *mlist) {
/// number of generated moves. /// number of generated moves.
int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) { int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
assert(pos.is_ok());
assert(!pos.is_check());
Color us, them; Color us, them;
Square ksq, from, to; Square ksq, from, to;
Bitboard empty, checkSqs, b1, b2, b3; Bitboard empty, checkSqs, b1, b2, b3;
int n = 0; int n = 0;
assert(pos.is_ok());
assert(!pos.is_check());
us = pos.side_to_move(); us = pos.side_to_move();
them = opposite_color(us); them = opposite_color(us);
@ -132,7 +130,8 @@ int generate_checks(const Position &pos, MoveStack *mlist, Bitboard dc) {
// Pawn moves. This is somewhat messy, and we use separate code for white // Pawn moves. This is somewhat messy, and we use separate code for white
// and black, because we can't shift by negative numbers in C/C++. :-( // and black, because we can't shift by negative numbers in C/C++. :-(
if(us == WHITE) { if (us == WHITE)
{
// Pawn moves which give discovered check. This is possible only if the // Pawn moves which give discovered check. This is possible only if the
// pawn is not on the same file as the enemy king, because we don't // pawn is not on the same file as the enemy king, because we don't
// generate captures. // generate captures.
@ -338,15 +337,16 @@ int generate_checks(const Position &pos, MoveStack *mlist, Bitboard dc) {
/// function is very ugly, and needs cleaning up some time later. FIXME /// function is very ugly, and needs cleaning up some time later. FIXME
int generate_evasions(const Position &pos, MoveStack *mlist) { int generate_evasions(const Position &pos, MoveStack *mlist) {
assert(pos.is_ok());
assert(pos.is_check());
Color us, them; Color us, them;
Bitboard checkers = pos.checkers(); Bitboard checkers = pos.checkers();
Bitboard pinned, b1, b2; Bitboard pinned, b1, b2;
Square ksq, from, to; Square ksq, from, to;
int n = 0; int n = 0;
assert(pos.is_ok());
assert(pos.is_check());
us = pos.side_to_move(); us = pos.side_to_move();
them = opposite_color(us); them = opposite_color(us);
@ -576,27 +576,25 @@ int generate_evasions(const Position &pos, MoveStack *mlist) {
/// we don't need it. /// we don't need it.
int generate_legal_moves(const Position& pos, MoveStack* mlist) { int generate_legal_moves(const Position& pos, MoveStack* mlist) {
assert(pos.is_ok()); assert(pos.is_ok());
if (pos.is_check()) if (pos.is_check())
return generate_evasions(pos, mlist); return generate_evasions(pos, mlist);
else {
int i, n;
Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
// Generate pseudo-legal moves: // Generate pseudo-legal moves:
n = generate_captures(pos, mlist); int n = generate_captures(pos, mlist);
n += generate_noncaptures(pos, mlist + n); n += generate_noncaptures(pos, mlist + n);
Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
// Remove illegal moves from the list: // Remove illegal moves from the list:
for(i = 0; i < n; i++) { for (int i = 0; i < n; i++)
if (!pos.move_is_legal(mlist[i].move, pinned)) if (!pos.move_is_legal(mlist[i].move, pinned))
mlist[i--].move = mlist[--n].move; mlist[i--].move = mlist[--n].move;
}
return n; return n;
} }
}
/// generate_move_if_legal() takes a position and a (not necessarily /// generate_move_if_legal() takes a position and a (not necessarily
@ -606,56 +604,46 @@ int generate_legal_moves(const Position &pos, MoveStack *mlist) {
/// only be used when the side to move is not in check. /// only be used when the side to move is not in check.
Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) { Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
Color us, them;
Square from, to;
Piece pc;
assert(pos.is_ok()); assert(pos.is_ok());
assert(!pos.is_check()); assert(!pos.is_check());
assert(move_is_ok(m)); assert(move_is_ok(m));
us = pos.side_to_move(); Color us = pos.side_to_move();
them = opposite_color(us); Color them = opposite_color(us);
from = move_from(m); Square from = move_from(m);
pc = pos.piece_on(from); Piece pc = pos.piece_on(from);
// If the from square is not occupied by a piece belonging to the side to // If the from square is not occupied by a piece belonging to the side to
// move, the move is obviously not legal. // move, the move is obviously not legal.
if (color_of_piece(pc) != us) if (color_of_piece(pc) != us)
return MOVE_NONE; return MOVE_NONE;
to = move_to(m); Square to = move_to(m);
// En passant moves: // En passant moves
if(move_is_ep(m)) { if (move_is_ep(m))
{
// The piece must be a pawn: // The piece must be a pawn and destination square must be the
if(type_of_piece(pc) != PAWN) // en passant square.
return MOVE_NONE; if ( type_of_piece(pc) != PAWN
|| to != pos.ep_square())
// The destination square must be the en passant square:
if(to != pos.ep_square())
return MOVE_NONE; return MOVE_NONE;
assert(pos.square_is_empty(to)); assert(pos.square_is_empty(to));
assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them)); assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
// The move is pseudo-legal. If it is legal, return it. // The move is pseudo-legal. If it is legal, return it.
if(pos.move_is_legal(m)) return (pos.move_is_legal(m) ? m : MOVE_NONE);
return m;
else
return MOVE_NONE;
} }
// Castling moves: // Castling moves
else if(move_is_short_castle(m)) { if (move_is_short_castle(m))
{
// The piece must be a king: // The piece must be a king and side to move must still have
if(type_of_piece(pc) != KING) // the right to castle kingside.
return MOVE_NONE; if ( type_of_piece(pc) != KING
||!pos.can_castle_kingside(us))
// The side to move must still have the right to castle kingside:
if(!pos.can_castle_kingside(us))
return MOVE_NONE; return MOVE_NONE;
assert(from == pos.king_square(us)); assert(from == pos.king_square(us));
@ -667,27 +655,28 @@ Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
Square s; Square s;
bool illegal = false; bool illegal = false;
// Check if any of the squares between king and rook
// is occupied or under attack.
for (s = Min(from, g1); s <= Max(from, g1); s++) for (s = Min(from, g1); s <= Max(from, g1); s++)
if((s != from && s != to && !pos.square_is_empty(s)) || if ( (s != from && s != to && !pos.square_is_empty(s))
pos.square_is_attacked(s, them)) || pos.square_is_attacked(s, them))
illegal = true; illegal = true;
// Check if any of the squares between king and rook
// is occupied.
for (s = Min(to, f1); s <= Max(to, f1); s++) for (s = Min(to, f1); s <= Max(to, f1); s++)
if (s != from && s != to && !pos.square_is_empty(s)) if (s != from && s != to && !pos.square_is_empty(s))
illegal = true; illegal = true;
if(!illegal) return (!illegal ? m : MOVE_NONE);
return m;
else
return MOVE_NONE;
} }
else if(move_is_long_castle(m)) {
// The piece must be a king: if (move_is_long_castle(m))
if(type_of_piece(pc) != KING) {
return MOVE_NONE; // The piece must be a king and side to move must still have
// the right to castle kingside.
// The side to move must still have the right to castle kingside: if ( type_of_piece(pc) != KING
if(!pos.can_castle_queenside(us)) ||!pos.can_castle_queenside(us))
return MOVE_NONE; return MOVE_NONE;
assert(from == pos.king_square(us)); assert(from == pos.king_square(us));
@ -700,27 +689,25 @@ Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
bool illegal = false; bool illegal = false;
for (s = Min(from, c1); s <= Max(from, c1); s++) for (s = Min(from, c1); s <= Max(from, c1); s++)
if((s != from && s != to && !pos.square_is_empty(s)) || if( (s != from && s != to && !pos.square_is_empty(s))
pos.square_is_attacked(s, them)) || pos.square_is_attacked(s, them))
illegal = true; illegal = true;
for (s = Min(to, d1); s <= Max(to, d1); s++) for (s = Min(to, d1); s <= Max(to, d1); s++)
if(s != from && s != to && !pos.square_is_empty(s)) if(s != from && s != to && !pos.square_is_empty(s))
illegal = true; illegal = true;
if(square_file(to) == FILE_B &&
(pos.piece_on(to + DELTA_W) == rook_of_color(them) || if ( square_file(to) == FILE_B
pos.piece_on(to + DELTA_W) == queen_of_color(them))) && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
|| pos.piece_on(to + DELTA_W) == queen_of_color(them)))
illegal = true; illegal = true;
if(!illegal) return (!illegal ? m : MOVE_NONE);
return m;
else
return MOVE_NONE;
} }
// Normal moves // Normal moves
else {
// The destination square cannot be occupied by a friendly piece: // The destination square cannot be occupied by a friendly piece
if (pos.color_of_piece_on(to) == us) if (pos.color_of_piece_on(to) == us)
return MOVE_NONE; return MOVE_NONE;
@ -729,19 +716,21 @@ Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
case PAWN: case PAWN:
// Pawn moves, as usual, are somewhat messy. // Pawn moves, as usual, are somewhat messy.
if(us == WHITE) { if (us == WHITE)
// If the destination square is on the 8th rank, the move must be a {
// promotion. // If the destination square is on the 8th rank, the move must
// be a promotion.
if (square_rank(to) == RANK_8 && !move_promotion(m)) if (square_rank(to) == RANK_8 && !move_promotion(m))
return MOVE_NONE; return MOVE_NONE;
// Proceed according to the square delta between the source and // Proceed according to the square delta between the source and
// destionation squares. // destionation squares.
switch(to - from) { switch (to - from)
{
case DELTA_NW: case DELTA_NE: case DELTA_NW:
// Capture. The destination square must be occupied by an enemy piece case DELTA_NE:
// (en passant captures was handled earlier). // Capture. The destination square must be occupied by an enemy
// piece (en passant captures was handled earlier).
if (pos.color_of_piece_on(to) != them) if (pos.color_of_piece_on(to) != them)
return MOVE_NONE; return MOVE_NONE;
break; break;
@ -756,8 +745,9 @@ Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
// Double pawn push. The destination square must be on the fourth // Double pawn push. The destination square must be on the fourth
// rank, and both the destination square and the square between the // rank, and both the destination square and the square between the
// source and destination squares must be empty. // source and destination squares must be empty.
if(square_rank(to) != RANK_4 || !pos.square_is_empty(to) || if ( square_rank(to) != RANK_4
!pos.square_is_empty(from + DELTA_N)) || !pos.square_is_empty(to)
|| !pos.square_is_empty(from + DELTA_N))
return MOVE_NONE; return MOVE_NONE;
break; break;
@ -765,19 +755,21 @@ Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
return MOVE_NONE; return MOVE_NONE;
} }
} }
else { // (us == BLACK) else // (us == BLACK)
// If the destination square is on the 1st rank, the move must be a {
// promotion. // If the destination square is on the 1th rank, the move must
// be a promotion.
if (square_rank(to) == RANK_1 && !move_promotion(m)) if (square_rank(to) == RANK_1 && !move_promotion(m))
return MOVE_NONE; return MOVE_NONE;
// Proceed according to the square delta between the source and // Proceed according to the square delta between the source and
// destionation squares. // destionation squares.
switch(to - from) { switch (to - from)
{
case DELTA_SW: case DELTA_SE: case DELTA_SW:
// Capture. The destination square must be occupied by an enemy piece case DELTA_SE:
// (en passant captures was handled earlier). // Capture. The destination square must be occupied by an enemy
// piece (en passant captures was handled earlier).
if (pos.color_of_piece_on(to) != them) if (pos.color_of_piece_on(to) != them)
return MOVE_NONE; return MOVE_NONE;
break; break;
@ -792,8 +784,9 @@ Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
// Double pawn push. The destination square must be on the fifth // Double pawn push. The destination square must be on the fifth
// rank, and both the destination square and the square between the // rank, and both the destination square and the square between the
// source and destination squares must be empty. // source and destination squares must be empty.
if(square_rank(to) != RANK_5 || !pos.square_is_empty(to) || if ( square_rank(to) != RANK_5
!pos.square_is_empty(from + DELTA_S)) || !pos.square_is_empty(to)
|| !pos.square_is_empty(from + DELTA_S))
return MOVE_NONE; return MOVE_NONE;
break; break;
@ -802,57 +795,42 @@ Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
} }
} }
// The move is pseudo-legal. Return it if it is legal. // The move is pseudo-legal. Return it if it is legal.
if(pos.move_is_legal(m)) return (pos.move_is_legal(m) ? m : MOVE_NONE);
return m;
else
return MOVE_NONE;
break; break;
case KNIGHT: case KNIGHT:
if(pos.knight_attacks_square(from, to) && pos.move_is_legal(m) && return ( pos.knight_attacks_square(from, to)
!move_promotion(m)) && pos.move_is_legal(m)
return m; && !move_promotion(m) ? m : MOVE_NONE);
else
return MOVE_NONE;
break; break;
case BISHOP: case BISHOP:
if(pos.bishop_attacks_square(from, to) && pos.move_is_legal(m) && return ( pos.bishop_attacks_square(from, to)
!move_promotion(m)) && pos.move_is_legal(m)
return m; && !move_promotion(m) ? m : MOVE_NONE);
else
return MOVE_NONE;
break; break;
case ROOK: case ROOK:
if(pos.rook_attacks_square(from, to) && pos.move_is_legal(m) && return ( pos.rook_attacks_square(from, to)
!move_promotion(m)) && pos.move_is_legal(m)
return m; && !move_promotion(m) ? m : MOVE_NONE);
else
return MOVE_NONE;
break; break;
case QUEEN: case QUEEN:
if(pos.queen_attacks_square(from, to) && pos.move_is_legal(m) && return ( pos.queen_attacks_square(from, to)
!move_promotion(m)) && pos.move_is_legal(m)
return m; && !move_promotion(m) ? m : MOVE_NONE);
else
return MOVE_NONE;
break; break;
case KING: case KING:
if(pos.king_attacks_square(from, to) && pos.move_is_legal(m) && return ( pos.king_attacks_square(from, to)
!move_promotion(m)) && pos.move_is_legal(m)
return m; && !move_promotion(m) ? m : MOVE_NONE);
else
return MOVE_NONE;
break; break;
default: default:
assert(false); assert(false);
} }
}
assert(false); assert(false);
return MOVE_NONE; return MOVE_NONE;
} }

View file

@ -99,7 +99,7 @@ Move MovePicker::get_next_move() {
while (true) while (true)
{ {
// If we already have a list of generated moves, pick the best move from // If we already have a list of generated moves, pick the best move from
// the list, and return it: // the list, and return it.
move = pick_move_from_list(); move = pick_move_from_list();
if (move != MOVE_NONE) if (move != MOVE_NONE)
{ {
@ -107,7 +107,7 @@ Move MovePicker::get_next_move() {
return move; return move;
} }
// Next phase: // Next phase
phaseIndex++; phaseIndex++;
switch (PhaseTable[phaseIndex]) { switch (PhaseTable[phaseIndex]) {
@ -199,8 +199,7 @@ Move MovePicker::get_next_move(Lock &lock) {
/// MovePicker::score_captures(), MovePicker::score_noncaptures(), /// MovePicker::score_captures(), MovePicker::score_noncaptures(),
/// MovePicker::score_evasions() and MovePicker::score_qcaptures() assign a /// MovePicker::score_evasions() and MovePicker::score_qcaptures() assign a
/// numerical move ordering score to each move in a move list. The moves /// numerical move ordering score to each move in a move list. The moves
/// with highest scores will be picked first by /// with highest scores will be picked first by pick_move_from_list().
/// MovePicker::pick_move_from_list().
void MovePicker::score_captures() { void MovePicker::score_captures() {
// Winning and equal captures in the main search are ordered by MVV. // Winning and equal captures in the main search are ordered by MVV.
@ -321,7 +320,7 @@ Move MovePicker::pick_move_from_list() {
badCaptures[numOfBadCaptures++] = moves[i]; badCaptures[numOfBadCaptures++] = moves[i];
moves[i--] = moves[--numOfMoves]; moves[i--] = moves[--numOfMoves];
} }
else if (moves[i].score > bestScore) // FIXME >= 0 ?? else if (moves[i].score > bestScore)
{ {
bestIndex = i; bestIndex = i;
bestScore = moves[i].score; bestScore = moves[i].score;
@ -348,7 +347,7 @@ Move MovePicker::pick_move_from_list() {
// the entire move list for the best move. If many moves have already // the entire move list for the best move. If many moves have already
// been searched and it is not a PV node, we are probably failing low // been searched and it is not a PV node, we are probably failing low
// anyway, so we just pick the first move from the list. // anyway, so we just pick the first move from the list.
bestIndex = (pvNode || movesPicked < 12 ? find_best_index() : movesPicked); bestIndex = (movesPicked < 12 || pvNode ? find_best_index() : movesPicked);
if (bestIndex != -1) if (bestIndex != -1)
{ {
@ -435,6 +434,7 @@ Move MovePicker::pick_move_from_list() {
/// picked next move. It can be used in search to further differentiate /// picked next move. It can be used in search to further differentiate
/// according to the current move type: capture, non capture, escape, etc. /// according to the current move type: capture, non capture, escape, etc.
MovePicker::MovegenPhase MovePicker::current_move_type() const { MovePicker::MovegenPhase MovePicker::current_move_type() const {
return PhaseTable[phaseIndex]; return PhaseTable[phaseIndex];
} }