X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fposition.cpp;h=391e556879c14081139dddcc721de89cf46b1649;hp=1942abb0823c4add37a29bc8764319f4e4ec5578;hb=f40e481fd63d056cbdad78c2c3fcdad0ff3556d9;hpb=839088205edd88eee9852c21442424cc66941ccb diff --git a/src/position.cpp b/src/position.cpp index 1942abb0..391e5568 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -22,9 +22,11 @@ //// Includes //// +#include #include #include #include +#include #include #include @@ -42,6 +44,32 @@ using std::string; using std::cout; using std::endl; +static inline bool isZero(char c) { return c == '0'; } + +struct PieceLetters : std::map { + + PieceLetters() { + + operator[]('K') = WK; operator[]('k') = BK; + operator[]('Q') = WQ; operator[]('q') = BQ; + operator[]('R') = WR; operator[]('r') = BR; + operator[]('B') = WB; operator[]('b') = BB; + operator[]('N') = WN; operator[]('n') = BN; + operator[]('P') = WP; operator[]('p') = BP; + operator[](' ') = NO_PIECE; operator[]('.') = NO_PIECE_DARK_SQ; + } + + char from_piece(Piece p) const { + + std::map::const_iterator it; + for (it = begin(); it != end(); ++it) + if (it->second == p) + return it->first; + + assert(false); + return 0; + } +}; //// //// Variables @@ -55,7 +83,7 @@ Key Position::zobExclusion; Score Position::PieceSquareTable[16][64]; -static bool RequestPending = false; +static PieceLetters pieceLetters; /// Constructors @@ -142,15 +170,10 @@ void Position::from_fen(const string& fen) { 6) Fullmove number: The number of the full move. It starts at 1, and is incremented after Black's move. */ - static const string pieceLetters = "KQRBNPkqrbnp"; - static const Piece pieces[] = { WK, WQ, WR, WB, WN, WP, BK, BQ, BR, BB, BN, BP }; - + char token; + std::istringstream ss(fen); Rank rank = RANK_8; File file = FILE_A; - size_t idx; - - std::istringstream ss(fen); - char token; clear(); @@ -159,8 +182,7 @@ void Position::from_fen(const string& fen) { { if (isdigit(token)) { - // Skip the given number of files - file += token - '1' + 1; + file += token - '0'; // Skip the given number of files continue; } else if (token == '/') @@ -170,11 +192,10 @@ void Position::from_fen(const string& fen) { continue; } - idx = pieceLetters.find(token); - if (idx == string::npos) + if (pieceLetters.find(token) == pieceLetters.end()) goto incorrect_fen; - put_piece(pieces[idx], make_square(file, rank)); + put_piece(pieceLetters[token], make_square(file, rank)); file++; } @@ -205,15 +226,15 @@ void Position::from_fen(const string& fen) { Square fenEpSquare = make_square(file_from_char(col), rank_from_char(row)); Color them = opposite_color(sideToMove); - if (attacks_from(fenEpSquare, them) & this->pieces(PAWN, sideToMove)) + if (attacks_from(fenEpSquare, them) & pieces(PAWN, sideToMove)) st->epSquare = fenEpSquare; } // 5-6. Halfmove clock and fullmove number are not parsed // Various initialisations - castleRightsMask[make_square(initialKFile, RANK_1)] ^= (WHITE_OO|WHITE_OOO); - castleRightsMask[make_square(initialKFile, RANK_8)] ^= (BLACK_OO|BLACK_OOO); + castleRightsMask[make_square(initialKFile, RANK_1)] ^= WHITE_OO | WHITE_OOO; + castleRightsMask[make_square(initialKFile, RANK_8)] ^= BLACK_OO | BLACK_OOO; castleRightsMask[make_square(initialKRFile, RANK_1)] ^= WHITE_OO; castleRightsMask[make_square(initialKRFile, RANK_8)] ^= BLACK_OO; castleRightsMask[make_square(initialQRFile, RANK_1)] ^= WHITE_OOO; @@ -291,67 +312,59 @@ bool Position::set_castling_rights(char token) { } -/// Position::to_fen() converts the position object to a FEN string. This is -/// probably only useful for debugging. +/// Position::to_fen() returns a FEN representation of the position. In case +/// of Chess960 the Shredder-FEN notation is used. Mainly a debugging function. const string Position::to_fen() const { - static const string pieceLetters = " PNBRQK pnbrqk"; string fen; - int skip; + Square sq; + char emptyCnt = '0'; for (Rank rank = RANK_8; rank >= RANK_1; rank--) { - skip = 0; for (File file = FILE_A; file <= FILE_H; file++) { - Square sq = make_square(file, rank); - if (!square_is_occupied(sq)) - { skip++; - continue; - } - if (skip > 0) + sq = make_square(file, rank); + + if (square_is_occupied(sq)) { - fen += (char)skip + '0'; - skip = 0; - } - fen += pieceLetters[piece_on(sq)]; + fen += emptyCnt; + fen += pieceLetters.from_piece(piece_on(sq)); + emptyCnt = '0'; + } else + emptyCnt++; } - if (skip > 0) - fen += (char)skip + '0'; - - fen += (rank > RANK_1 ? '/' : ' '); + fen += emptyCnt; + fen += '/'; + emptyCnt = '0'; } - fen += (sideToMove == WHITE ? "w " : "b "); + + fen.erase(std::remove_if(fen.begin(), fen.end(), isZero), fen.end()); + fen.erase(--fen.end()); + fen += (sideToMove == WHITE ? " w " : " b "); + if (st->castleRights != NO_CASTLES) { - if (initialKFile == FILE_E && initialQRFile == FILE_A && initialKRFile == FILE_H) - { - if (can_castle_kingside(WHITE)) fen += 'K'; - if (can_castle_queenside(WHITE)) fen += 'Q'; - if (can_castle_kingside(BLACK)) fen += 'k'; - if (can_castle_queenside(BLACK)) fen += 'q'; - } - else - { - if (can_castle_kingside(WHITE)) - fen += char(toupper(file_to_char(initialKRFile))); - if (can_castle_queenside(WHITE)) - fen += char(toupper(file_to_char(initialQRFile))); - if (can_castle_kingside(BLACK)) - fen += file_to_char(initialKRFile); - if (can_castle_queenside(BLACK)) - fen += file_to_char(initialQRFile); - } - } else - fen += '-'; + const bool Chess960 = initialKFile != FILE_E + || initialQRFile != FILE_A + || initialKRFile != FILE_H; - fen += ' '; - if (ep_square() != SQ_NONE) - fen += square_to_string(ep_square()); - else + if (can_castle_kingside(WHITE)) + fen += Chess960 ? char(toupper(file_to_char(initialKRFile))) : 'K'; + + if (can_castle_queenside(WHITE)) + fen += Chess960 ? char(toupper(file_to_char(initialQRFile))) : 'Q'; + + if (can_castle_kingside(BLACK)) + fen += Chess960 ? file_to_char(initialKRFile) : 'k'; + + if (can_castle_queenside(BLACK)) + fen += Chess960 ? file_to_char(initialQRFile) : 'q'; + } else fen += '-'; + fen += (ep_square() == SQ_NONE ? " -" : " " + square_to_string(ep_square())); return fen; } @@ -359,44 +372,42 @@ const string Position::to_fen() const { /// Position::print() prints an ASCII representation of the position to /// the standard output. If a move is given then also the san is print. -void Position::print(Move m) const { +void Position::print(Move move) const { - static const string pieceLetters = " PNBRQK PNBRQK ."; + const char* dottedLine = "\n+---+---+---+---+---+---+---+---+\n"; + static bool requestPending = false; // Check for reentrancy, as example when called from inside // MovePicker that is used also here in move_to_san() - if (RequestPending) + if (requestPending) return; - RequestPending = true; + requestPending = true; - cout << endl; - if (m != MOVE_NONE) + if (move) { Position p(*this, thread()); - string col = (color_of_piece_on(move_from(m)) == BLACK ? ".." : ""); - cout << "Move is: " << col << move_to_san(p, m) << endl; + string dd = (color_of_piece_on(move_from(move)) == BLACK ? ".." : ""); + cout << "\nMove is: " << dd << move_to_san(p, move); } + for (Rank rank = RANK_8; rank >= RANK_1; rank--) { - cout << "+---+---+---+---+---+---+---+---+" << endl; + cout << dottedLine << '|'; for (File file = FILE_A; file <= FILE_H; file++) { Square sq = make_square(file, rank); + char c = (color_of_piece_on(sq) == BLACK ? '=' : ' '); Piece piece = piece_on(sq); - if (piece == EMPTY && square_color(sq) == WHITE) - piece = NO_PIECE; - char col = (color_of_piece_on(sq) == BLACK ? '=' : ' '); - cout << '|' << col << pieceLetters[piece] << col; + if (piece == NO_PIECE && square_color(sq) == DARK) + piece = NO_PIECE_DARK_SQ; + + cout << c << pieceLetters.from_piece(piece) << c << '|'; } - cout << '|' << endl; } - cout << "+---+---+---+---+---+---+---+---+" << endl - << "Fen is: " << to_fen() << endl - << "Key is: " << st->key << endl; - - RequestPending = false; + cout << dottedLine << "Fen is: " << to_fen() << "\nKey is: " << st->key << endl; + requestPending = false; } @@ -566,7 +577,7 @@ bool Position::pl_move_is_legal(Move m, Bitboard pinned) const { assert(to == ep_square()); assert(piece_on(from) == piece_of_color_and_type(us, PAWN)); assert(piece_on(capsq) == piece_of_color_and_type(them, PAWN)); - assert(piece_on(to) == EMPTY); + assert(piece_on(to) == NO_PIECE); clear_bit(&b, from); clear_bit(&b, capsq); @@ -814,7 +825,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI do_move_bb(&(byTypeBB[0]), move_bb); // HACK: byTypeBB[0] == occupied squares board[to] = board[from]; - board[from] = EMPTY; + board[from] = NO_PIECE; // Update piece lists, note that index[from] is not updated and // becomes stale. This works as long as index[] is accessed just @@ -943,10 +954,10 @@ void Position::do_capture_move(Key& key, PieceType capture, Color them, Square t assert(to == st->epSquare); assert(relative_rank(opposite_color(them), to) == RANK_6); - assert(piece_on(to) == EMPTY); + assert(piece_on(to) == NO_PIECE); assert(piece_on(capsq) == piece_of_color_and_type(them, PAWN)); - board[capsq] = EMPTY; + board[capsq] = NO_PIECE; } st->pawnKey ^= zobrist[them][PAWN][capsq]; } @@ -1040,7 +1051,7 @@ void Position::do_castle_move(Move m) { // Update board array Piece king = piece_of_color_and_type(us, KING); Piece rook = piece_of_color_and_type(us, ROOK); - board[kfrom] = board[rfrom] = EMPTY; + board[kfrom] = board[rfrom] = NO_PIECE; board[kto] = king; board[rto] = rook; @@ -1149,7 +1160,7 @@ void Position::undo_move(Move m) { do_move_bb(&(byTypeBB[0]), move_bb); // HACK: byTypeBB[0] == occupied squares board[from] = piece_of_color_and_type(us, pt); - board[to] = EMPTY; + board[to] = NO_PIECE; // Update piece list index[from] = index[to]; @@ -1237,7 +1248,7 @@ void Position::undo_castle_move(Move m) { set_bit(&(byTypeBB[0]), rfrom); // HACK: byTypeBB[0] == occupied squares // Update board - board[rto] = board[kto] = EMPTY; + board[rto] = board[kto] = NO_PIECE; board[rfrom] = piece_of_color_and_type(us, ROOK); board[kfrom] = piece_of_color_and_type(us, KING); @@ -1381,7 +1392,7 @@ int Position::see(Square from, Square to) const { // Handle en passant moves if (st->epSquare == to && type_of_piece_on(from) == PAWN) { - assert(capture == EMPTY); + assert(capture == NO_PIECE); Square capQq = (side_to_move() == WHITE)? (to - DELTA_N) : (to - DELTA_S); capture = piece_on(capQq); @@ -1494,6 +1505,7 @@ void Position::clear() { st = &startState; memset(st, 0, sizeof(StateInfo)); st->epSquare = SQ_NONE; + startPosPlyCounter = 0; memset(byColorBB, 0, sizeof(Bitboard) * 2); memset(byTypeBB, 0, sizeof(Bitboard) * 8); @@ -1501,7 +1513,7 @@ void Position::clear() { memset(index, 0, sizeof(int) * 64); for (int i = 0; i < 64; i++) - board[i] = EMPTY; + board[i] = NO_PIECE; for (int i = 0; i < 8; i++) for (int j = 0; j < 16; j++) @@ -1528,6 +1540,10 @@ void Position::reset_game_ply() { st->gamePly = 0; } +void Position::inc_startpos_ply_counter() { + + startPosPlyCounter++; +} /// Position::put_piece() puts a piece on the given square of the board, /// updating the board array, bitboards, and piece counts.