X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fposition.cpp;h=5f19403035313eddc8e7694c4f9ce52f9bdc60d6;hp=cdbd9f0d1963ff13486c986c401e340e1c960d03;hb=f032ba54c58c85332351d7f345ac46e2112d0a2e;hpb=d7ef09727acc11c91b4a25b775c7f6d0bf1d37d8 diff --git a/src/position.cpp b/src/position.cpp index cdbd9f0d..5f194030 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -599,19 +599,17 @@ bool Position::move_is_check(Move m, Bitboard dcCandidates) const { case BISHOP: return (dcCandidates && bit_is_set(dcCandidates, from)) - || ( direction_between_squares(ksq, to) != DIR_NONE - && bit_is_set(piece_attacks(ksq), to)); + || (direction_is_diagonal(ksq, to) && bit_is_set(piece_attacks(ksq), to)); case ROOK: return (dcCandidates && bit_is_set(dcCandidates, from)) - || ( direction_between_squares(ksq, to) != DIR_NONE - && bit_is_set(piece_attacks(ksq), to)); + || (direction_is_straight(ksq, to) && bit_is_set(piece_attacks(ksq), to)); case QUEEN: // Discovered checks are impossible! assert(!bit_is_set(dcCandidates, from)); - return ( direction_between_squares(ksq, to) != DIR_NONE - && bit_is_set(piece_attacks(ksq), to)); + return ( (direction_is_straight(ksq, to) && bit_is_set(piece_attacks(ksq), to)) + || (direction_is_diagonal(ksq, to) && bit_is_set(piece_attacks(ksq), to))); case KING: // Discovered check? @@ -651,20 +649,6 @@ bool Position::move_is_check(Move m, Bitboard dcCandidates) const { } -/// Position::move_is_capture() tests whether a move from the current -/// position is a capture. Move must not be MOVE_NONE. - -bool Position::move_is_capture(Move m) const { - - assert(m != MOVE_NONE); - - return ( !square_is_empty(move_to(m)) - && (color_of_piece_on(move_to(m)) != color_of_piece_on(move_from(m))) - ) - || move_is_ep(m); -} - - /// Position::update_checkers() udpates chekers info given the move. It is called /// in do_move() and is faster then find_checkers(). @@ -867,8 +851,6 @@ void Position::do_capture_move(PieceType capture, Color them, Square to) { st->mgValue -= pst(them, capture, to); st->egValue -= pst(them, capture, to); - assert(!move_promotion(m) || capture != PAWN); - // Update material if (capture != PAWN) npMaterial[them] -= piece_value_midgame(capture); @@ -1869,21 +1851,6 @@ Value Position::compute_non_pawn_material(Color c) const { } -/// Position::is_mate() returns true or false depending on whether the -/// side to move is checkmated. Note that this function is currently very -/// slow, and shouldn't be used frequently inside the search. - -bool Position::is_mate() const { - - if (is_check()) - { - MovePicker mp = MovePicker(*this, false, MOVE_NONE, EmptySearchStack, Depth(0)); - return mp.get_next_move() == MOVE_NONE; - } - return false; -} - - /// Position::is_draw() tests whether the position is drawn by material, /// repetition, or the 50 moves rule. It does not detect stalemates, this /// must be done by the search. @@ -1908,21 +1875,25 @@ bool Position::is_draw() const { } +/// Position::is_mate() returns true or false depending on whether the +/// side to move is checkmated. + +bool Position::is_mate() const { + + MoveStack moves[256]; + + return is_check() && !generate_evasions(*this, moves, pinned_pieces(sideToMove)); +} + + /// Position::has_mate_threat() tests whether a given color has a mate in one -/// from the current position. This function is quite slow, but it doesn't -/// matter, because it is currently only called from PV nodes, which are rare. +/// from the current position. bool Position::has_mate_threat(Color c) { StateInfo st1, st2; Color stm = side_to_move(); - // The following lines are useless and silly, but prevents gcc from - // emitting a stupid warning stating that u1.lastMove and u1.epSquare might - // be used uninitialized. - st1.lastMove = st->lastMove; - st1.epSquare = st->epSquare; - if (is_check()) return false; @@ -1933,18 +1904,26 @@ bool Position::has_mate_threat(Color c) { MoveStack mlist[120]; int count; bool result = false; + Bitboard dc = discovered_check_candidates(sideToMove); + Bitboard pinned = pinned_pieces(sideToMove); - // Generate legal moves - count = generate_legal_moves(*this, mlist); + // Generate pseudo-legal non-capture and capture check moves + count = generate_non_capture_checks(*this, mlist, dc); + count += generate_captures(*this, mlist + count); // Loop through the moves, and see if one of them is mate for (int i = 0; i < count; i++) { - do_move(mlist[i].move, st2); + Move move = mlist[i].move; + + if (!pl_move_is_legal(move, pinned)) + continue; + + do_move(move, st2); if (is_mate()) result = true; - undo_move(mlist[i].move); + undo_move(move); } // Undo null move, if necessary @@ -1960,26 +1939,26 @@ bool Position::has_mate_threat(Color c) { void Position::init_zobrist() { - for(Piece p = WP; p <= BK; p++) - for(Square s = SQ_A1; s <= SQ_H8; s++) - zobrist[color_of_piece(p)][type_of_piece(p)][s] = genrand_int64(); + for (int i = 0; i < 2; i++) + for (int j = 0; j < 8; j++) + for (int k = 0; k < 64; k++) + zobrist[i][j][k] = Key(genrand_int64()); - zobEp[0] = 0ULL; - for(int i = 1; i < 64; i++) - zobEp[i] = genrand_int64(); + for (int i = 0; i < 64; i++) + zobEp[i] = Key(genrand_int64()); - for(int i = 15; i >= 0; i--) - zobCastle[(i&8) | (i&1) | ((i&2) << 1) | ((i&4) >> 1)] = genrand_int64(); + for (int i = 0; i < 16; i++) + zobCastle[i] = genrand_int64(); zobSideToMove = genrand_int64(); for (int i = 0; i < 2; i++) for (int j = 0; j < 8; j++) for (int k = 0; k < 16; k++) - zobMaterial[i][j][k] = (k > 0)? genrand_int64() : 0LL; + zobMaterial[i][j][k] = (k > 0)? Key(genrand_int64()) : Key(0LL); for (int i = 0; i < 16; i++) - zobMaterial[0][KING][i] = zobMaterial[1][KING][i] = 0ULL; + zobMaterial[0][KING][i] = zobMaterial[1][KING][i] = Key(0ULL); }