X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fposition.cpp;h=3f6400a80daeda5ae7f8af243726f138ff322a4b;hp=8b6bafe31ad309cd8c42211098dc7ed227754ed6;hb=c9d7e99de682516c560009b550c41da9ae2008b8;hpb=d9113d127b491db0a427a416217d55d3d298c25e diff --git a/src/position.cpp b/src/position.cpp index 8b6bafe3..3f6400a8 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -160,7 +160,7 @@ void Position::detach() { /// string. This function is not very robust - make sure that input FENs are /// correct (this is assumed to be the responsibility of the GUI). -void Position::from_fen(const string& fen, bool c960) { +void Position::from_fen(const string& fen, bool isChess960) { /* A FEN string defines a particular position using only the ASCII character set. @@ -255,7 +255,7 @@ void Position::from_fen(const string& fen, bool c960) { castleRightsMask[make_square(initialQRFile, RANK_1)] ^= WHITE_OOO; castleRightsMask[make_square(initialQRFile, RANK_8)] ^= BLACK_OOO; - isChess960 = c960; + chess960 = isChess960; find_checkers(); st->key = compute_key(); @@ -368,16 +368,16 @@ const string Position::to_fen() const { if (st->castleRights != CASTLES_NONE) { if (can_castle_kingside(WHITE)) - fen += isChess960 ? char(toupper(file_to_char(initialKRFile))) : 'K'; + fen += chess960 ? char(toupper(file_to_char(initialKRFile))) : 'K'; if (can_castle_queenside(WHITE)) - fen += isChess960 ? char(toupper(file_to_char(initialQRFile))) : 'Q'; + fen += chess960 ? char(toupper(file_to_char(initialQRFile))) : 'Q'; if (can_castle_kingside(BLACK)) - fen += isChess960 ? file_to_char(initialKRFile) : 'k'; + fen += chess960 ? file_to_char(initialKRFile) : 'k'; if (can_castle_queenside(BLACK)) - fen += isChess960 ? file_to_char(initialQRFile) : 'q'; + fen += chess960 ? file_to_char(initialQRFile) : 'q'; } else fen += '-'; @@ -643,6 +643,115 @@ bool Position::pl_move_is_evasion(Move m, Bitboard pinned) const return bit_is_set(target, to) && pl_move_is_legal(m, pinned); } +/// Position::move_is_legal() takes a position and a (not necessarily pseudo-legal) +/// move and tests whether the move is legal. This version is not very fast and +/// should be used only in non time-critical paths. + +bool Position::move_is_legal(const Move m) const { + + MoveStack mlist[MAX_MOVES]; + MoveStack *cur, *last = generate(*this, mlist); + + for (cur = mlist; cur != last; cur++) + if (cur->move == m) + return pl_move_is_legal(m, pinned_pieces(sideToMove)); + + return false; +} + + +/// Fast version of Position::move_is_legal() that takes a position a move and +/// a bitboard of pinned pieces as input, and tests whether the move is legal. + +bool Position::move_is_legal(const Move m, Bitboard pinned) const { + + assert(is_ok()); + assert(pinned == pinned_pieces(sideToMove)); + + Color us = sideToMove; + Color them = opposite_color(sideToMove); + Square from = move_from(m); + Square to = move_to(m); + Piece pc = piece_on(from); + + // Use a slower but simpler function for uncommon cases + if (move_is_special(m)) + return move_is_legal(m); + + // If the from square is not occupied by a piece belonging to the side to + // move, the move is obviously not legal. + if (color_of_piece(pc) != us) + return false; + + // The destination square cannot be occupied by a friendly piece + if (color_of_piece_on(to) == us) + return false; + + // Handle the special case of a pawn move + if (type_of_piece(pc) == PAWN) + { + // Move direction must be compatible with pawn color + int direction = to - from; + if ((us == WHITE) != (direction > 0)) + return false; + + // We have already handled promotion moves, so destination + // cannot be on the 8/1th rank. + if (square_rank(to) == RANK_8 || square_rank(to) == RANK_1) + return false; + + // Proceed according to the square delta between the origin and + // destination squares. + switch (direction) + { + case DELTA_NW: + case DELTA_NE: + case DELTA_SW: + case DELTA_SE: + // Capture. The destination square must be occupied by an enemy + // piece (en passant captures was handled earlier). + if (color_of_piece_on(to) != them) + return false; + break; + + case DELTA_N: + case DELTA_S: + // Pawn push. The destination square must be empty. + if (!square_is_empty(to)) + return false; + break; + + case DELTA_NN: + // Double white pawn push. The destination square must be on the fourth + // rank, and both the destination square and the square between the + // source and destination squares must be empty. + if ( square_rank(to) != RANK_4 + || !square_is_empty(to) + || !square_is_empty(from + DELTA_N)) + return false; + break; + + case DELTA_SS: + // Double black pawn push. The destination square must be on the fifth + // rank, and both the destination square and the square between the + // source and destination squares must be empty. + if ( square_rank(to) != RANK_5 + || !square_is_empty(to) + || !square_is_empty(from + DELTA_S)) + return false; + break; + + default: + return false; + } + } + else if (!bit_is_set(attacks_from(pc, from), to)) + return false; + + // The move is pseudo-legal, check if it is also legal + return is_check() ? pl_move_is_evasion(m, pinned) : pl_move_is_legal(m, pinned); +} + /// Position::move_is_check() tests whether a pseudo-legal move is a check @@ -885,7 +994,6 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI // Update pawn hash key and prefetch in L1/L2 cache st->pawnKey ^= zobrist[us][PAWN][from] ^ zobrist[us][PAWN][to]; - prefetchPawn(st->pawnKey, threadID); // Set en passant square, only if moved pawn can be captured if ((to ^ from) == 16) @@ -938,6 +1046,9 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI } } + // Prefetch pawn and material hash tables + prefetchTables(st->pawnKey, st->materialKey, threadID); + // Update incremental scores st->value += pst_delta(piece, from, to); @@ -1684,54 +1795,11 @@ bool Position::is_draw() const { bool Position::is_mate() const { - MoveStack moves[MOVES_MAX]; + MoveStack moves[MAX_MOVES]; return is_check() && generate(*this, moves) == moves; } -/// Position::has_mate_threat() tests whether the side to move is under -/// a threat of being mated in one from the current position. - -bool Position::has_mate_threat() { - - MoveStack mlist[MOVES_MAX], *last, *cur; - StateInfo st1, st2; - bool mateFound = false; - - // If we are under check it's up to evasions to do the job - if (is_check()) - return false; - - // First pass the move to our opponent doing a null move - do_null_move(st1); - - // Then generate pseudo-legal moves that could give check - last = generate(*this, mlist); - last = generate(*this, last); - - // Loop through the moves, and see if one of them gives mate - Bitboard pinned = pinned_pieces(sideToMove); - CheckInfo ci(*this); - for (cur = mlist; cur != last && !mateFound; cur++) - { - Move move = cur->move; - if ( !pl_move_is_legal(move, pinned) - || !move_is_check(move, ci)) - continue; - - do_move(move, st2, ci, true); - - if (is_mate()) - mateFound = true; - - undo_move(move); - } - - undo_null_move(); - return mateFound; -} - - /// Position::init_zobrist() is a static member function which initializes at /// startup the various arrays used to compute hash keys. @@ -1975,7 +2043,6 @@ bool Position::is_ok(int* failedStep) const { if (failedStep) (*failedStep)++; if (debugPieceList) - { for (Color c = WHITE; c <= BLACK; c++) for (PieceType pt = PAWN; pt <= KING; pt++) for (int i = 0; i < pieceCount[c][pt]; i++) @@ -1986,13 +2053,15 @@ bool Position::is_ok(int* failedStep) const { if (index[piece_list(c, pt, i)] != i) return false; } - } if (failedStep) (*failedStep)++; - if (debugCastleSquares) { - for (Color c = WHITE; c <= BLACK; c++) { + if (debugCastleSquares) + { + for (Color c = WHITE; c <= BLACK; c++) + { if (can_castle_kingside(c) && piece_on(initial_kr_square(c)) != make_piece(c, ROOK)) return false; + if (can_castle_queenside(c) && piece_on(initial_qr_square(c)) != make_piece(c, ROOK)) return false; }