BookEntry e;
uint16_t best = 0;
- unsigned sum = 0, bookMove = 0;
+ unsigned sum = 0;
+ Move move = MOVE_NONE;
uint64_t key = book_key(pos);
if (fileName != fName && !open(fName.c_str()))
// with lower score. Note that first entry is always chosen.
if ( (RKiss.rand<unsigned>() % sum < e.count)
|| (pickBest && e.count == best))
- bookMove = e.move;
+ move = Move(e.move);
}
- if (!bookMove)
+ if (!move)
return MOVE_NONE;
// A PolyGlot book move is encoded as follows:
// move is a promotion we have to convert to our representation, in all the
// other cases we can directly compare with a Move after having masked out
// the special Move's flags (bit 14-15) that are not supported by PolyGlot.
- int promotion = (bookMove >> 12) & 7;
+ int pt = (move >> 12) & 7;
+ if (pt)
+ move = make_promotion(from_sq(move), to_sq(move), PieceType(pt + 1));
- if (promotion)
- bookMove = make_promotion_move(move_from(Move(bookMove)),
- move_to(Move(bookMove)),
- PieceType(promotion + 1));
-
- // Convert bookMove to our internal Move format (and verify it is legal)
+ // Add 'special move' flags and verify it is legal
for (MoveList<MV_LEGAL> ml(pos); !ml.end(); ++ml)
- if (bookMove == unsigned(ml.move() & ~(3 << 14)))
+ if (move == (ml.move() & 0x3FFF))
return ml.move();
return MOVE_NONE;
const string move_to_uci(Move m, bool chess960) {
- Square from = move_from(m);
- Square to = move_to(m);
+ Square from = from_sq(m);
+ Square to = to_sq(m);
string promotion;
if (m == MOVE_NONE)
Bitboard attackers;
bool ambiguousMove, ambiguousFile, ambiguousRank;
- Square sq, from = move_from(m);
- Square to = move_to(m);
+ Square sq, from = from_sq(m);
+ Square to = to_sq(m);
PieceType pt = type_of(pos.piece_on(from));
string san;
if (is_castle(m))
- san = (move_to(m) < move_from(m) ? "O-O-O" : "O-O");
+ san = (to_sq(m) < from_sq(m) ? "O-O-O" : "O-O");
else
{
if (pt != PAWN)
to = pop_1st_bit(&b);
if (Type == MV_CAPTURE || Type == MV_EVASION)
- (*mlist++).move = make_promotion_move(to - Delta, to, QUEEN);
+ (*mlist++).move = make_promotion(to - Delta, to, QUEEN);
if (Type == MV_NON_CAPTURE || Type == MV_EVASION)
{
- (*mlist++).move = make_promotion_move(to - Delta, to, ROOK);
- (*mlist++).move = make_promotion_move(to - Delta, to, BISHOP);
- (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
+ (*mlist++).move = make_promotion(to - Delta, to, ROOK);
+ (*mlist++).move = make_promotion(to - Delta, to, BISHOP);
+ (*mlist++).move = make_promotion(to - Delta, to, KNIGHT);
}
// This is the only possible under promotion that can give a check
// not already included in the queen-promotion.
if ( Type == MV_CHECK
&& bit_is_set(pos.attacks_from<KNIGHT>(to), pos.king_square(Delta > 0 ? BLACK : WHITE)))
- (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
+ (*mlist++).move = make_promotion(to - Delta, to, KNIGHT);
else (void)pos; // Silence a warning under MSVC
}
return mlist;
while (b1)
{
to = pop_1st_bit(&b1);
- (*mlist++).move = make_enpassant_move(to, pos.ep_square());
+ (*mlist++).move = make_enpassant(to, pos.ep_square());
}
}
return mlist;
return mlist;
}
- (*mlist++).move = make_castle_move(kfrom, rfrom);
+ (*mlist++).move = make_castle(kfrom, rfrom);
return mlist;
}
for (MoveStack* cur = moves; cur != lastMove; cur++)
{
m = cur->move;
- cur->score = PieceValueMidgame[pos.piece_on(move_to(m))]
- - type_of(pos.piece_on(move_from(m)));
+ cur->score = PieceValueMidgame[pos.piece_on(to_sq(m))]
+ - type_of(pos.piece_on(from_sq(m)));
if (is_promotion(m))
cur->score += PieceValueMidgame[Piece(promotion_piece_type(m))];
for (MoveStack* cur = moves; cur != lastMove; cur++)
{
m = cur->move;
- from = move_from(m);
- cur->score = H.value(pos.piece_on(from), move_to(m));
+ from = from_sq(m);
+ cur->score = H.value(pos.piece_on(from), to_sq(m));
}
}
if ((seeScore = pos.see_sign(m)) < 0)
cur->score = seeScore - History::MaxValue; // Be sure we are at the bottom
else if (pos.is_capture(m))
- cur->score = PieceValueMidgame[pos.piece_on(move_to(m))]
- - type_of(pos.piece_on(move_from(m))) + History::MaxValue;
+ cur->score = PieceValueMidgame[pos.piece_on(to_sq(m))]
+ - type_of(pos.piece_on(from_sq(m))) + History::MaxValue;
else
- cur->score = H.value(pos.piece_on(move_from(m)), move_to(m));
+ cur->score = H.value(pos.piece_on(from_sq(m)), to_sq(m));
}
}
case PH_QRECAPTURES:
move = (curMove++)->move;
- if (move_to(move) == recaptureSquare)
+ if (to_sq(move) == recaptureSquare)
return move;
break;
assert(square_is_ok(s));
Bitboard occ, xray;
- Square from = move_from(m);
- Square to = move_to(m);
+ Square from = from_sq(m);
+ Square to = to_sq(m);
Piece piece = piece_on(from);
assert(!square_is_empty(from));
assert(pinned == pinned_pieces());
Color us = side_to_move();
- Square from = move_from(m);
+ Square from = from_sq(m);
assert(color_of(piece_on(from)) == us);
assert(piece_on(king_square(us)) == make_piece(us, KING));
if (is_enpassant(m))
{
Color them = flip(us);
- Square to = move_to(m);
+ Square to = to_sq(m);
Square capsq = to + pawn_push(them);
Square ksq = king_square(us);
Bitboard b = occupied_squares();
// square is attacked by the opponent. Castling moves are checked
// for legality during move generation.
if (type_of(piece_on(from)) == KING)
- return is_castle(m) || !(attackers_to(move_to(m)) & pieces(flip(us)));
+ return is_castle(m) || !(attackers_to(to_sq(m)) & pieces(flip(us)));
// A non-king move is legal if and only if it is not pinned or it
// is moving along the ray towards or away from the king.
return !pinned
|| !bit_is_set(pinned, from)
- || squares_aligned(from, move_to(m), king_square(us));
+ || squares_aligned(from, to_sq(m), king_square(us));
}
Color us = sideToMove;
Color them = flip(sideToMove);
- Square from = move_from(m);
- Square to = move_to(m);
+ Square from = from_sq(m);
+ Square to = to_sq(m);
Piece pc = piece_on(from);
// Use a slower but simpler function for uncommon cases
{
Bitboard b = occupied_squares();
clear_bit(&b, from);
- if (attackers_to(move_to(m), b) & pieces(flip(us)))
+ if (attackers_to(to_sq(m), b) & pieces(flip(us)))
return false;
}
else
// Our move must be a blocking evasion or a capture of the checking piece
target = squares_between(checksq, king_square(us)) | checkers();
- if (!bit_is_set(target, move_to(m)))
+ if (!bit_is_set(target, to_sq(m)))
return false;
}
}
assert(is_ok(m));
assert(ci.dcCandidates == discovered_check_candidates());
- assert(color_of(piece_on(move_from(m))) == side_to_move());
+ assert(color_of(piece_on(from_sq(m))) == side_to_move());
- Square from = move_from(m);
- Square to = move_to(m);
+ Square from = from_sq(m);
+ Square to = to_sq(m);
PieceType pt = type_of(piece_on(from));
// Direct check ?
Color us = side_to_move();
Color them = flip(us);
- Square from = move_from(m);
- Square to = move_to(m);
+ Square from = from_sq(m);
+ Square to = to_sq(m);
Piece piece = piece_on(from);
PieceType pt = type_of(piece);
PieceType capture = is_enpassant(m) ? PAWN : type_of(piece_on(to));
Color us = side_to_move();
Color them = flip(us);
- Square from = move_from(m);
- Square to = move_to(m);
+ Square from = from_sq(m);
+ Square to = to_sq(m);
Piece piece = piece_on(to);
PieceType pt = type_of(piece);
PieceType capture = st->capturedType;
Square kto, kfrom, rfrom, rto, kAfter, rAfter;
Color us = side_to_move();
- Square kBefore = move_from(m);
- Square rBefore = move_to(m);
+ Square kBefore = from_sq(m);
+ Square rBefore = to_sq(m);
// Find after-castle squares for king and rook
if (rBefore > kBefore) // O-O
assert(is_ok(m));
- Square from = move_from(m);
- Square to = move_to(m);
+ Square from = from_sq(m);
+ Square to = to_sq(m);
// Early return if SEE cannot be negative because captured piece value
// is not less then capturing one. Note that king moves always return
if (is_castle(m))
return 0;
- from = move_from(m);
- to = move_to(m);
+ from = from_sq(m);
+ to = to_sq(m);
capturedType = type_of(piece_on(to));
occ = occupied_squares();
inline bool Position::is_passed_pawn_push(Move m) const {
- return board[move_from(m)] == make_piece(sideToMove, PAWN)
- && pawn_is_passed(sideToMove, move_to(m));
+ return board[from_sq(m)] == make_piece(sideToMove, PAWN)
+ && pawn_is_passed(sideToMove, to_sq(m));
}
inline int Position::startpos_ply_counter() const {
inline bool Position::is_capture_or_promotion(Move m) const {
assert(is_ok(m));
- return is_special(m) ? !is_castle(m) : !square_is_empty(move_to(m));
+ return is_special(m) ? !is_castle(m) : !square_is_empty(to_sq(m));
}
inline bool Position::is_capture(Move m) const {
// Note that castle is coded as "king captures the rook"
assert(is_ok(m));
- return (!square_is_empty(move_to(m)) && !is_castle(m)) || is_enpassant(m);
+ return (!square_is_empty(to_sq(m)) && !is_castle(m)) || is_enpassant(m);
}
inline PieceType Position::captured_piece_type() const {
FORCE_INLINE bool is_dangerous(const Position& pos, Move m, bool captureOrPromotion) {
// Test for a pawn pushed to 7th or a passed pawn move
- if (type_of(pos.piece_on(move_from(m))) == PAWN)
+ if (type_of(pos.piece_on(from_sq(m))) == PAWN)
{
Color c = pos.side_to_move();
- if ( relative_rank(c, move_to(m)) == RANK_7
- || pos.pawn_is_passed(c, move_to(m)))
+ if ( relative_rank(c, to_sq(m)) == RANK_7
+ || pos.pawn_is_passed(c, to_sq(m)))
return true;
}
// Test for a capture that triggers a pawn endgame
if ( captureOrPromotion
- && type_of(pos.piece_on(move_to(m))) != PAWN
+ && type_of(pos.piece_on(to_sq(m))) != PAWN
&& ( pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK)
- - PieceValueMidgame[pos.piece_on(move_to(m))] == VALUE_ZERO)
+ - PieceValueMidgame[pos.piece_on(to_sq(m))] == VALUE_ZERO)
&& !is_special(m))
return true;
// Populate RootMoves with all the legal moves (default) or, if a SearchMoves
// is given, with the subset of legal moves to search.
for (MoveList<MV_LEGAL> ml(pos); !ml.end(); ++ml)
- if ( SearchMoves.empty()
- || count(SearchMoves.begin(), SearchMoves.end(), ml.move()))
+ if (SearchMoves.empty() || count(SearchMoves.begin(), SearchMoves.end(), ml.move()))
RootMoves.push_back(RootMove(ml.move()));
if (Options["OwnBook"])
{
Move bookMove = book.probe(pos, Options["Book File"], Options["Best Book Move"]);
- if ( bookMove != MOVE_NONE
- && count(RootMoves.begin(), RootMoves.end(), bookMove))
+ if (bookMove && count(RootMoves.begin(), RootMoves.end(), bookMove))
{
std::swap(RootMoves[0], *find(RootMoves.begin(), RootMoves.end(), bookMove));
- goto finish;
+ goto finalize;
}
}
pos.undo_move(RootMoves[0].pv[0]);
}
-finish:
+finalize:
// When we reach max depth we arrive here even without a StopRequest, but if
// we are pondering or in infinite search, we shouldn't print the best move
&& pos.captured_piece_type() == NO_PIECE_TYPE
&& !is_special(move))
{
- Square to = move_to(move);
+ Square to = to_sq(move);
H.update_gain(pos.piece_on(to), to, -(ss-1)->eval - ss->eval);
}
// but fixing this made program slightly weaker.
Depth predictedDepth = newDepth - reduction<PvNode>(depth, moveCount);
futilityValue = futilityBase + futility_margin(predictedDepth, moveCount)
- + H.gain(pos.piece_on(move_from(move)), move_to(move));
+ + H.gain(pos.piece_on(from_sq(move)), to_sq(move));
if (futilityValue < beta)
{
// Increase history value of the cut-off move
Value bonus = Value(int(depth) * int(depth));
- H.add(pos.piece_on(move_from(move)), move_to(move), bonus);
+ H.add(pos.piece_on(from_sq(move)), to_sq(move), bonus);
// Decrease history of all the other played non-capture moves
for (int i = 0; i < playedMoveCount - 1; i++)
{
Move m = movesSearched[i];
- H.add(pos.piece_on(move_from(m)), move_to(m), -bonus);
+ H.add(pos.piece_on(from_sq(m)), to_sq(m), -bonus);
}
}
}
// to search the moves. Because the depth is <= 0 here, only captures,
// queen promotions and checks (only if depth >= DEPTH_QS_CHECKS) will
// be generated.
- MovePicker mp(pos, ttMove, depth, H, move_to((ss-1)->currentMove));
+ MovePicker mp(pos, ttMove, depth, H, to_sq((ss-1)->currentMove));
CheckInfo ci(pos);
// Loop through the moves until no moves remain or a beta cutoff occurs
&& !pos.is_passed_pawn_push(move))
{
futilityValue = futilityBase
- + PieceValueEndgame[pos.piece_on(move_to(move))]
+ + PieceValueEndgame[pos.piece_on(to_sq(move))]
+ (is_enpassant(move) ? PawnValueEndgame : VALUE_ZERO);
if (futilityValue < beta)
Color them;
Value futilityValue, bv = *bestValue;
- from = move_from(move);
- to = move_to(move);
+ from = from_sq(move);
+ to = to_sq(move);
them = flip(pos.side_to_move());
ksq = pos.king_square(them);
kingAtt = pos.attacks_from<KING>(ksq);
assert(is_ok(m2));
// Case 1: The moving piece is the same in both moves
- f2 = move_from(m2);
- t1 = move_to(m1);
+ f2 = from_sq(m2);
+ t1 = to_sq(m1);
if (f2 == t1)
return true;
// Case 2: The destination square for m2 was vacated by m1
- t2 = move_to(m2);
- f1 = move_from(m1);
+ t2 = to_sq(m2);
+ f1 = from_sq(m1);
if (t2 == f1)
return true;
Square mfrom, mto, tfrom, tto;
- mfrom = move_from(m);
- mto = move_to(m);
- tfrom = move_from(threat);
- tto = move_to(threat);
+ mfrom = from_sq(m);
+ mto = to_sq(m);
+ tfrom = from_sq(threat);
+ tto = to_sq(threat);
// Case 1: Don't prune moves which move the threatened piece
if (mfrom == tto)
return c == WHITE ? DELTA_N : DELTA_S;
}
-inline Square move_from(Move m) {
+inline Square from_sq(Move m) {
return Square((m >> 6) & 0x3F);
}
-inline Square move_to(Move m) {
+inline Square to_sq(Move m) {
return Square(m & 0x3F);
}
return Move(to | (from << 6));
}
-inline Move make_promotion_move(Square from, Square to, PieceType promotion) {
- return Move(to | (from << 6) | (1 << 14) | ((promotion - 2) << 12)) ;
+inline Move make_promotion(Square from, Square to, PieceType pt) {
+ return Move(to | (from << 6) | (1 << 14) | ((pt - 2) << 12)) ;
}
-inline Move make_enpassant_move(Square from, Square to) {
+inline Move make_enpassant(Square from, Square to) {
return Move(to | (from << 6) | (2 << 14));
}
-inline Move make_castle_move(Square from, Square to) {
+inline Move make_castle(Square from, Square to) {
return Move(to | (from << 6) | (3 << 14));
}
inline bool is_ok(Move m) {
- return move_from(m) != move_to(m); // Catches also MOVE_NULL and MOVE_NONE
+ return from_sq(m) != to_sq(m); // Catches also MOVE_NULL and MOVE_NONE
}
#include <string>