X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fposition.cpp;h=8667d44bfce718500aadc73cd16fe8db34d72763;hb=f5b8db7a1efc4ecaa6e1b385d1579abf5f5840fe;hp=1bae3620087ff62edb5268ca6902617fb2745f25;hpb=188700d7f15a778919ac65268adfee57c870dcda;p=stockfish
diff --git a/src/position.cpp b/src/position.cpp
index 1bae3620..8667d44b 100644
--- a/src/position.cpp
+++ b/src/position.cpp
@@ -17,7 +17,6 @@
along with this program. If not, see .
*/
-#include
#include
#include
#include
@@ -77,8 +76,6 @@ namespace {
// Bonus for having the side to move (modified by Joona Kiiski)
const Score TempoValue = make_score(48, 22);
- bool isZero(char c) { return c == '0'; }
-
struct PieceLetters : public std::map {
PieceLetters() {
@@ -341,7 +338,7 @@ const string Position::to_fen() const {
Square sq;
char emptyCnt = '0';
- for (Rank rank = RANK_8; rank >= RANK_1; rank--)
+ for (Rank rank = RANK_8; rank >= RANK_1; rank--, fen += '/')
{
for (File file = FILE_A; file <= FILE_H; file++)
{
@@ -349,19 +346,23 @@ const string Position::to_fen() const {
if (square_is_occupied(sq))
{
- fen += emptyCnt;
+ if (emptyCnt != '0')
+ {
+ fen += emptyCnt;
+ emptyCnt = '0';
+ }
fen += pieceLetters.from_piece(piece_on(sq));
- emptyCnt = '0';
} else
emptyCnt++;
}
- fen += emptyCnt;
- fen += '/';
- emptyCnt = '0';
+
+ if (emptyCnt != '0')
+ {
+ fen += emptyCnt;
+ emptyCnt = '0';
+ }
}
- fen.erase(std::remove_if(fen.begin(), fen.end(), isZero), fen.end());
- fen.erase(--fen.end());
fen += (sideToMove == WHITE ? " w " : " b ");
if (st->castleRights != CASTLES_NONE)
@@ -500,7 +501,7 @@ Bitboard Position::attacks_from(Piece p, Square s) const {
case WB: case BB: return attacks_from(s);
case WR: case BR: return attacks_from(s);
case WQ: case BQ: return attacks_from(s);
- default: return NonSlidingAttacksBB[p][s];
+ default: return StepAttacksBB[p][s];
}
}
@@ -513,7 +514,7 @@ Bitboard Position::attacks_from(Piece p, Square s, Bitboard occ) {
case WB: case BB: return bishop_attacks_bb(s, occ);
case WR: case BR: return rook_attacks_bb(s, occ);
case WQ: case BQ: return bishop_attacks_bb(s, occ) | rook_attacks_bb(s, occ);
- default: return NonSlidingAttacksBB[p][s];
+ default: return StepAttacksBB[p][s];
}
}
@@ -586,8 +587,8 @@ bool Position::pl_move_is_legal(Move m, Bitboard pinned) const {
Bitboard b = occupied_squares();
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(from) == make_piece(us, PAWN));
+ assert(piece_on(capsq) == make_piece(them, PAWN));
assert(piece_on(to) == PIECE_NONE);
clear_bit(&b, from);
@@ -602,7 +603,7 @@ bool Position::pl_move_is_legal(Move m, Bitboard pinned) const {
Square from = move_from(m);
assert(color_of_piece_on(from) == us);
- assert(piece_on(king_square(us)) == piece_of_color_and_type(us, KING));
+ assert(piece_on(king_square(us)) == make_piece(us, KING));
// If the moving piece is a king, check whether the destination
// square is attacked by the opponent.
@@ -656,7 +657,7 @@ bool Position::move_is_check(Move m, const CheckInfo& ci) const {
assert(move_is_ok(m));
assert(ci.dcCandidates == discovered_check_candidates(side_to_move()));
assert(color_of_piece_on(move_from(m)) == side_to_move());
- assert(piece_on(ci.ksq) == piece_of_color_and_type(opposite_color(side_to_move()), KING));
+ assert(piece_on(ci.ksq) == make_piece(opposite_color(side_to_move()), KING));
Square from = move_from(m);
Square to = move_to(m);
@@ -832,7 +833,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
assert(color_of_piece_on(from) == us);
assert(color_of_piece_on(to) == them || square_is_empty(to));
- assert(!(ep || pm) || piece == piece_of_color_and_type(us, PAWN));
+ assert(!(ep || pm) || piece == make_piece(us, PAWN));
assert(!pm || relative_rank(us, to) == RANK_8);
if (capture)
@@ -905,7 +906,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
// Insert promoted piece instead of pawn
clear_bit(&(byTypeBB[PAWN]), to);
set_bit(&(byTypeBB[promotion]), to);
- board[to] = piece_of_color_and_type(us, promotion);
+ board[to] = make_piece(us, promotion);
// Update piece counts
pieceCount[us][promotion]++;
@@ -999,7 +1000,7 @@ 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) == PIECE_NONE);
- assert(piece_on(capsq) == piece_of_color_and_type(them, PAWN));
+ assert(piece_on(capsq) == make_piece(them, PAWN));
board[capsq] = PIECE_NONE;
}
@@ -1063,8 +1064,8 @@ void Position::do_castle_move(Move m) {
Square rfrom = move_to(m); // HACK: See comment at beginning of function
Square kto, rto;
- assert(piece_on(kfrom) == piece_of_color_and_type(us, KING));
- assert(piece_on(rfrom) == piece_of_color_and_type(us, ROOK));
+ assert(piece_on(kfrom) == make_piece(us, KING));
+ assert(piece_on(rfrom) == make_piece(us, ROOK));
// Find destination squares for king and rook
if (rfrom > kfrom) // O-O
@@ -1093,8 +1094,8 @@ void Position::do_castle_move(Move m) {
set_bit(&(byTypeBB[0]), rto); // HACK: byTypeBB[0] == occupied squares
// Update board array
- Piece king = piece_of_color_and_type(us, KING);
- Piece rook = piece_of_color_and_type(us, ROOK);
+ Piece king = make_piece(us, KING);
+ Piece rook = make_piece(us, ROOK);
board[kfrom] = board[rfrom] = PIECE_NONE;
board[kto] = king;
board[rto] = rook;
@@ -1170,7 +1171,7 @@ void Position::undo_move(Move m) {
assert(!pm || relative_rank(us, to) == RANK_8);
assert(!ep || to == st->previous->epSquare);
assert(!ep || relative_rank(us, to) == RANK_6);
- assert(!ep || piece_on(to) == piece_of_color_and_type(us, PAWN));
+ assert(!ep || piece_on(to) == make_piece(us, PAWN));
if (pm) // promotion ?
{
@@ -1178,7 +1179,7 @@ void Position::undo_move(Move m) {
pt = PAWN;
assert(promotion >= KNIGHT && promotion <= QUEEN);
- assert(piece_on(to) == piece_of_color_and_type(us, promotion));
+ assert(piece_on(to) == make_piece(us, promotion));
// Replace promoted piece with a pawn
clear_bit(&(byTypeBB[promotion]), to);
@@ -1203,7 +1204,7 @@ void Position::undo_move(Move m) {
do_move_bb(&(byTypeBB[pt]), move_bb);
do_move_bb(&(byTypeBB[0]), move_bb); // HACK: byTypeBB[0] == occupied squares
- board[from] = piece_of_color_and_type(us, pt);
+ board[from] = make_piece(us, pt);
board[to] = PIECE_NONE;
// Update piece list
@@ -1225,7 +1226,7 @@ void Position::undo_move(Move m) {
set_bit(&(byTypeBB[st->capturedType]), capsq);
set_bit(&(byTypeBB[0]), capsq);
- board[capsq] = piece_of_color_and_type(them, st->capturedType);
+ board[capsq] = make_piece(them, st->capturedType);
// Update piece count
pieceCount[them][st->capturedType]++;
@@ -1272,8 +1273,8 @@ void Position::undo_castle_move(Move m) {
rto = relative_square(us, SQ_D1);
}
- assert(piece_on(kto) == piece_of_color_and_type(us, KING));
- assert(piece_on(rto) == piece_of_color_and_type(us, ROOK));
+ assert(piece_on(kto) == make_piece(us, KING));
+ assert(piece_on(rto) == make_piece(us, ROOK));
// Remove pieces from destination squares:
clear_bit(&(byColorBB[us]), kto);
@@ -1293,8 +1294,8 @@ void Position::undo_castle_move(Move m) {
// Update board
board[rto] = board[kto] = PIECE_NONE;
- board[rfrom] = piece_of_color_and_type(us, ROOK);
- board[kfrom] = piece_of_color_and_type(us, KING);
+ board[rfrom] = make_piece(us, ROOK);
+ board[kfrom] = make_piece(us, KING);
// Update piece lists
pieceList[us][KING][index[kto]] = kfrom;
@@ -1711,7 +1712,7 @@ bool Position::has_mate_threat() {
// 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++)
+ for (cur = mlist; !mateFound && cur != last; cur++)
{
Move move = cur->move;
if ( !pl_move_is_legal(move, pinned)
@@ -1719,10 +1720,7 @@ bool Position::has_mate_threat() {
continue;
do_move(move, st2, ci, true);
-
- if (is_mate())
- mateFound = true;
-
+ mateFound = is_mate();
undo_move(move);
}
@@ -1974,25 +1972,26 @@ 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++)
{
- if (piece_on(piece_list(c, pt, i)) != piece_of_color_and_type(c, pt))
+ if (piece_on(piece_list(c, pt, i)) != make_piece(c, pt))
return false;
if (index[piece_list(c, pt, i)] != i)
return false;
}
- }
if (failedStep) (*failedStep)++;
- if (debugCastleSquares) {
- for (Color c = WHITE; c <= BLACK; c++) {
- if (can_castle_kingside(c) && piece_on(initial_kr_square(c)) != piece_of_color_and_type(c, ROOK))
+ 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)) != piece_of_color_and_type(c, ROOK))
+
+ if (can_castle_queenside(c) && piece_on(initial_qr_square(c)) != make_piece(c, ROOK))
return false;
}
if (castleRightsMask[initial_kr_square(WHITE)] != (ALL_CASTLES ^ WHITE_OO))