]> git.sesse.net Git - stockfish/blobdiff - src/position.cpp
Fix parametrized direction in pawns generation
[stockfish] / src / position.cpp
index e526885e50252daa058cab86232b5affc8597f82..f25ee276b9ef70787d221f9a0454a4388c396223 100644 (file)
@@ -42,7 +42,7 @@ Key Position::zobCastle[16];
 Key Position::zobSideToMove;
 Key Position::zobExclusion;
 
-Score Position::PieceSquareTable[16][64];
+Score Position::pieceSquareTable[16][64];
 
 // Material values arrays, indexed by Piece
 const Value PieceValueMidgame[17] = {
@@ -129,7 +129,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 isChess960) {
+void Position::from_fen(const string& fenStr, bool isChess960) {
 /*
    A FEN string defines a particular position using only the ASCII character set.
 
@@ -161,13 +161,13 @@ void Position::from_fen(const string& fen, bool isChess960) {
   char col, row, token;
   size_t p;
   Square sq = SQ_A8;
-  std::istringstream ss(fen);
+  std::istringstream fen(fenStr);
 
   clear();
-  ss >> std::noskipws;
+  fen >> std::noskipws;
 
   // 1. Piece placement
-  while ((ss >> token) && !isspace(token))
+  while ((fen >> token) && !isspace(token))
   {
       if (token == '/')
           sq -= Square(16); // Jump back of 2 rows
@@ -183,17 +183,17 @@ void Position::from_fen(const string& fen, bool isChess960) {
   }
 
   // 2. Active color
-  ss >> token;
+  fen >> token;
   sideToMove = (token == 'w' ? WHITE : BLACK);
-  ss >> token;
+  fen >> token;
 
   // 3. Castling availability
-  while ((ss >> token) && !isspace(token))
+  while ((fen >> token) && !isspace(token))
       set_castling_rights(token);
 
   // 4. En passant square. Ignore if no pawn capture is possible
-  if (   ((ss >> col) && (col >= 'a' && col <= 'h'))
-      && ((ss >> row) && (row == '3' || row == '6')))
+  if (   ((fen >> col) && (col >= 'a' && col <= 'h'))
+      && ((fen >> row) && (row == '3' || row == '6')))
   {
       st->epSquare = make_square(File(col - 'a'), Rank(row - '1'));
       Color them = opposite_color(sideToMove);
@@ -203,7 +203,7 @@ void Position::from_fen(const string& fen, bool isChess960) {
   }
 
   // 5-6. Halfmove clock and fullmove number
-  ss >> std::skipws >> st->rule50 >> fullMoves;
+  fen >> std::skipws >> st->rule50 >> fullMoves;
 
   // Various initialisations
   chess960 = isChess960;
@@ -270,13 +270,13 @@ void Position::set_castling_rights(char token) {
 
 const string Position::to_fen() const {
 
-  string fen;
+  std::ostringstream fen;
   Square sq;
-  char emptyCnt;
+  int emptyCnt;
 
-  for (Rank rank = RANK_8; rank >= RANK_1; rank--, fen += '/')
+  for (Rank rank = RANK_8; rank >= RANK_1; rank--)
   {
-      emptyCnt = '0';
+      emptyCnt = 0;
 
       for (File file = FILE_A; file <= FILE_H; file++)
       {
@@ -284,40 +284,46 @@ const string Position::to_fen() const {
 
           if (!square_is_empty(sq))
           {
-              if (emptyCnt != '0')
+              if (emptyCnt)
               {
-                  fen += emptyCnt;
-                  emptyCnt = '0';
+                  fen << emptyCnt;
+                  emptyCnt = 0;
               }
-              fen += PieceToChar[piece_on(sq)];
-          } else
+              fen << PieceToChar[piece_on(sq)];
+          }
+          else
               emptyCnt++;
       }
 
-      if (emptyCnt != '0')
-          fen += emptyCnt;
+      if (emptyCnt)
+          fen << emptyCnt;
+
+      if (rank > RANK_1)
+          fen << '/';
   }
 
-  fen += (sideToMove == WHITE ? " w " : " b ");
+  fen << (sideToMove == WHITE ? " w " : " b ");
 
   if (st->castleRights != CASTLES_NONE)
   {
       if (can_castle(WHITE_OO))
-          fen += chess960 ? char(toupper(file_to_char(square_file(castle_rook_square(WHITE_OO))))) : 'K';
+          fen << (chess960 ? char(toupper(file_to_char(square_file(castle_rook_square(WHITE_OO))))) : 'K');
 
       if (can_castle(WHITE_OOO))
-          fen += chess960 ? char(toupper(file_to_char(square_file(castle_rook_square(WHITE_OOO))))) : 'Q';
+          fen << (chess960 ? char(toupper(file_to_char(square_file(castle_rook_square(WHITE_OOO))))) : 'Q');
 
       if (can_castle(BLACK_OO))
-          fen += chess960 ? file_to_char(square_file(castle_rook_square(BLACK_OO))) : 'k';
+          fen << (chess960 ? file_to_char(square_file(castle_rook_square(BLACK_OO))) : 'k');
 
       if (can_castle(BLACK_OOO))
-          fen += chess960 ? file_to_char(square_file(castle_rook_square(BLACK_OOO))) : 'q';
+          fen << (chess960 ? file_to_char(square_file(castle_rook_square(BLACK_OOO))) : 'q');
   } else
-      fen += '-';
+      fen << '-';
 
-  fen += (ep_square() == SQ_NONE ? " -" : " " + square_to_string(ep_square()));
-  return fen;
+  fen << (ep_square() == SQ_NONE ? " -" : " " + square_to_string(ep_square()))
+      << " " << st->rule50 << " " << fullMoves;
+
+  return fen.str();
 }
 
 
@@ -331,7 +337,7 @@ void Position::print(Move move) const {
   if (move)
   {
       Position p(*this, thread());
-      string dd = (piece_color(piece_on(move_from(move))) == BLACK ? ".." : "");
+      string dd = (sideToMove == BLACK ? ".." : "");
       cout << "\nMove is: " << dd << move_to_san(p, move);
   }
 
@@ -507,14 +513,14 @@ bool Position::pl_move_is_legal(Move m, Bitboard pinned) const {
   assert(piece_color(piece_on(from)) == us);
   assert(piece_on(king_square(us)) == make_piece(us, KING));
 
-  // En passant captures are a tricky special case. Because they are
-  // rather uncommon, we do it simply by testing whether the king is attacked
-  // after the move is made
+  // En passant captures are a tricky special case. Because they are rather
+  // uncommon, we do it simply by testing whether the king is attacked after
+  // the move is made.
   if (move_is_ep(m))
   {
       Color them = opposite_color(us);
       Square to = move_to(m);
-      Square capsq = make_square(square_file(to), square_rank(from));
+      Square capsq = to + pawn_push(them);
       Square ksq = king_square(us);
       Bitboard b = occupied_squares();
 
@@ -545,20 +551,14 @@ bool Position::pl_move_is_legal(Move m, Bitboard pinned) const {
 }
 
 
-/// Position::move_is_pl_slow() takes a move and tests whether the move
-/// is pseudo legal. This version is not very fast and should be used
-/// only in non time-critical paths.
-
-bool Position::move_is_pl_slow(const Move m) const {
+/// Position::move_is_legal() takes a move and tests whether the move
+/// is legal. This version is not very fast and should be used only
+/// in non time-critical paths.
 
-  MoveStack mlist[MAX_MOVES];
-  MoveStack *cur, *last;
+bool Position::move_is_legal(const Move m) const {
 
-  last = in_check() ? generate<MV_EVASION>(*this, mlist)
-                    : generate<MV_NON_EVASION>(*this, mlist);
-
-  for (cur = mlist; cur != last; cur++)
-      if (cur->move == m)
+  for (MoveList<MV_LEGAL> ml(*this); !ml.end(); ++ml)
+      if (ml.move() == m)
           return true;
 
   return false;
@@ -580,7 +580,7 @@ bool Position::move_is_pl(const Move m) const {
 
   // Use a slower but simpler function for uncommon cases
   if (move_is_special(m))
-      return move_is_pl_slow(m);
+      return move_is_legal(m);
 
   // Is not a promotion, so promotion piece must be empty
   if (promotion_piece_type(m) - 2 != PIECE_TYPE_NONE)
@@ -905,9 +905,9 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
 
   // Move the piece
   Bitboard move_bb = make_move_bb(from, to);
-  do_move_bb(&(byColorBB[us]), move_bb);
-  do_move_bb(&(byTypeBB[pt]), move_bb);
-  do_move_bb(&(byTypeBB[0]), move_bb); // HACK: byTypeBB[0] == occupied squares
+  do_move_bb(&byColorBB[us], move_bb);
+  do_move_bb(&byTypeBB[pt], move_bb);
+  do_move_bb(&byTypeBB[0], move_bb); // HACK: byTypeBB[0] == occupied squares
 
   board[to] = board[from];
   board[from] = PIECE_NONE;
@@ -930,7 +930,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
       // Set en passant square, only if moved pawn can be captured
       if ((to ^ from) == 16)
       {
-          if (attacks_from<PAWN>(from + (us == WHITE ? DELTA_N : DELTA_S), us) & pieces(PAWN, them))
+          if (attacks_from<PAWN>(from + pawn_push(us), us) & pieces(PAWN, them))
           {
               st->epSquare = Square((int(from) + int(to)) / 2);
               key ^= zobEp[st->epSquare];
@@ -944,8 +944,8 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
           assert(promotion >= KNIGHT && promotion <= QUEEN);
 
           // Insert promoted piece instead of pawn
-          clear_bit(&(byTypeBB[PAWN]), to);
-          set_bit(&(byTypeBB[promotion]), to);
+          clear_bit(&byTypeBB[PAWN], to);
+          set_bit(&byTypeBB[promotion], to);
           board[to] = make_piece(us, promotion);
 
           // Update piece counts
@@ -1039,7 +1039,7 @@ void Position::do_capture_move(Key& key, PieceType capture, Color them, Square t
     {
         if (ep) // en passant ?
         {
-            capsq = (them == BLACK)? (to - DELTA_N) : (to - DELTA_S);
+            capsq = to + pawn_push(them);
 
             assert(to == st->epSquare);
             assert(relative_rank(opposite_color(them), to) == RANK_6);
@@ -1054,9 +1054,9 @@ void Position::do_capture_move(Key& key, PieceType capture, Color them, Square t
         st->npMaterial[them] -= PieceValueMidgame[capture];
 
     // Remove captured piece
-    clear_bit(&(byColorBB[them]), capsq);
-    clear_bit(&(byTypeBB[capture]), capsq);
-    clear_bit(&(byTypeBB[0]), capsq);
+    clear_bit(&byColorBB[them], capsq);
+    clear_bit(&byTypeBB[capture], capsq);
+    clear_bit(&byTypeBB[0], capsq);
 
     // Update hash key
     key ^= zobrist[them][capture][capsq];
@@ -1100,12 +1100,9 @@ void Position::do_castle_move(Move m) {
   Color us = side_to_move();
   Color them = opposite_color(us);
 
-  // Reset capture field
-  st->capturedType = PIECE_TYPE_NONE;
-
   // Find source squares for king and rook
   Square kfrom = move_from(m);
-  Square rfrom = move_to(m);  // HACK: See comment at beginning of function
+  Square rfrom = move_to(m);
   Square kto, rto;
 
   assert(piece_on(kfrom) == make_piece(us, KING));
@@ -1116,28 +1113,30 @@ void Position::do_castle_move(Move m) {
   {
       kto = relative_square(us, SQ_G1);
       rto = relative_square(us, SQ_F1);
-  } else { // O-O-O
+  }
+  else // O-O-O
+  {
       kto = relative_square(us, SQ_C1);
       rto = relative_square(us, SQ_D1);
   }
 
-  // Remove pieces from source squares:
-  clear_bit(&(byColorBB[us]), kfrom);
-  clear_bit(&(byTypeBB[KING]), kfrom);
-  clear_bit(&(byTypeBB[0]), kfrom); // HACK: byTypeBB[0] == occupied squares
-  clear_bit(&(byColorBB[us]), rfrom);
-  clear_bit(&(byTypeBB[ROOK]), rfrom);
-  clear_bit(&(byTypeBB[0]), rfrom); // HACK: byTypeBB[0] == occupied squares
-
-  // Put pieces on destination squares:
-  set_bit(&(byColorBB[us]), kto);
-  set_bit(&(byTypeBB[KING]), kto);
-  set_bit(&(byTypeBB[0]), kto); // HACK: byTypeBB[0] == occupied squares
-  set_bit(&(byColorBB[us]), rto);
-  set_bit(&(byTypeBB[ROOK]), rto);
-  set_bit(&(byTypeBB[0]), rto); // HACK: byTypeBB[0] == occupied squares
-
-  // Update board array
+  // Remove pieces from source squares
+  clear_bit(&byColorBB[us], kfrom);
+  clear_bit(&byTypeBB[KING], kfrom);
+  clear_bit(&byTypeBB[0], kfrom);
+  clear_bit(&byColorBB[us], rfrom);
+  clear_bit(&byTypeBB[ROOK], rfrom);
+  clear_bit(&byTypeBB[0], rfrom);
+
+  // Put pieces on destination squares
+  set_bit(&byColorBB[us], kto);
+  set_bit(&byTypeBB[KING], kto);
+  set_bit(&byTypeBB[0], kto);
+  set_bit(&byColorBB[us], rto);
+  set_bit(&byTypeBB[ROOK], rto);
+  set_bit(&byTypeBB[0], rto);
+
+  // Update board
   Piece king = make_piece(us, KING);
   Piece rook = make_piece(us, ROOK);
   board[kfrom] = board[rfrom] = PIECE_NONE;
@@ -1147,10 +1146,13 @@ void Position::do_castle_move(Move m) {
   // Update piece lists
   pieceList[us][KING][index[kfrom]] = kto;
   pieceList[us][ROOK][index[rfrom]] = rto;
-  int tmp = index[rfrom]; // In Chess960 could be rto == kfrom
+  int tmp = index[rfrom]; // In Chess960 could be kto == rfrom
   index[kto] = index[kfrom];
   index[rto] = tmp;
 
+  // Reset capture field
+  st->capturedType = PIECE_TYPE_NONE;
+
   // Update incremental scores
   st->value += pst_delta(king, kfrom, kto);
   st->value += pst_delta(rook, rfrom, rto);
@@ -1226,8 +1228,8 @@ void Position::undo_move(Move m) {
       assert(piece_on(to) == make_piece(us, promotion));
 
       // Replace promoted piece with a pawn
-      clear_bit(&(byTypeBB[promotion]), to);
-      set_bit(&(byTypeBB[PAWN]), to);
+      clear_bit(&byTypeBB[promotion], to);
+      set_bit(&byTypeBB[PAWN], to);
 
       // Update piece counts
       pieceCount[us][promotion]--;
@@ -1244,9 +1246,9 @@ void Position::undo_move(Move m) {
 
   // Put the piece back at the source square
   Bitboard move_bb = make_move_bb(to, from);
-  do_move_bb(&(byColorBB[us]), move_bb);
-  do_move_bb(&(byTypeBB[pt]), move_bb);
-  do_move_bb(&(byTypeBB[0]), move_bb); // HACK: byTypeBB[0] == occupied squares
+  do_move_bb(&byColorBB[us], move_bb);
+  do_move_bb(&byTypeBB[pt], move_bb);
+  do_move_bb(&byTypeBB[0], move_bb); // HACK: byTypeBB[0] == occupied squares
 
   board[from] = make_piece(us, pt);
   board[to] = PIECE_NONE;
@@ -1260,15 +1262,15 @@ void Position::undo_move(Move m) {
       Square capsq = to;
 
       if (ep)
-          capsq = (us == WHITE)? (to - DELTA_N) : (to - DELTA_S);
+          capsq = to - pawn_push(us);
 
       assert(st->capturedType != KING);
       assert(!ep || square_is_empty(capsq));
 
       // Restore the captured piece
-      set_bit(&(byColorBB[them]), capsq);
-      set_bit(&(byTypeBB[st->capturedType]), capsq);
-      set_bit(&(byTypeBB[0]), capsq);
+      set_bit(&byColorBB[them], capsq);
+      set_bit(&byTypeBB[st->capturedType], capsq);
+      set_bit(&byTypeBB[0], capsq);
 
       board[capsq] = make_piece(them, st->capturedType);
 
@@ -1298,13 +1300,13 @@ void Position::undo_castle_move(Move m) {
   assert(move_is_castle(m));
 
   // When we have arrived here, some work has already been done by
-  // Position::undo_move.  In particular, the side to move has been switched,
+  // Position::undo_move. In particular, the side to move has been switched,
   // so the code below is correct.
   Color us = side_to_move();
 
   // Find source squares for king and rook
   Square kfrom = move_from(m);
-  Square rfrom = move_to(m);  // HACK: See comment at beginning of function
+  Square rfrom = move_to(m);
   Square kto, rto;
 
   // Find destination squares for king and rook
@@ -1312,7 +1314,9 @@ void Position::undo_castle_move(Move m) {
   {
       kto = relative_square(us, SQ_G1);
       rto = relative_square(us, SQ_F1);
-  } else { // O-O-O
+  }
+  else // O-O-O
+  {
       kto = relative_square(us, SQ_C1);
       rto = relative_square(us, SQ_D1);
   }
@@ -1320,26 +1324,28 @@ void Position::undo_castle_move(Move m) {
   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);
-  clear_bit(&(byTypeBB[KING]), kto);
-  clear_bit(&(byTypeBB[0]), kto); // HACK: byTypeBB[0] == occupied squares
-  clear_bit(&(byColorBB[us]), rto);
-  clear_bit(&(byTypeBB[ROOK]), rto);
-  clear_bit(&(byTypeBB[0]), rto); // HACK: byTypeBB[0] == occupied squares
-
-  // Put pieces on source squares:
-  set_bit(&(byColorBB[us]), kfrom);
-  set_bit(&(byTypeBB[KING]), kfrom);
-  set_bit(&(byTypeBB[0]), kfrom); // HACK: byTypeBB[0] == occupied squares
-  set_bit(&(byColorBB[us]), rfrom);
-  set_bit(&(byTypeBB[ROOK]), rfrom);
-  set_bit(&(byTypeBB[0]), rfrom); // HACK: byTypeBB[0] == occupied squares
+  // Remove pieces from destination squares
+  clear_bit(&byColorBB[us], kto);
+  clear_bit(&byTypeBB[KING], kto);
+  clear_bit(&byTypeBB[0], kto);
+  clear_bit(&byColorBB[us], rto);
+  clear_bit(&byTypeBB[ROOK], rto);
+  clear_bit(&byTypeBB[0], rto);
+
+  // Put pieces on source squares
+  set_bit(&byColorBB[us], kfrom);
+  set_bit(&byTypeBB[KING], kfrom);
+  set_bit(&byTypeBB[0], kfrom);
+  set_bit(&byColorBB[us], rfrom);
+  set_bit(&byTypeBB[ROOK], rfrom);
+  set_bit(&byTypeBB[0], rfrom);
 
   // Update board
-  board[rto] = board[kto] = PIECE_NONE;
-  board[rfrom] = make_piece(us, ROOK);
-  board[kfrom] = make_piece(us, KING);
+  Piece king = make_piece(us, KING);
+  Piece rook = make_piece(us, ROOK);
+  board[kto] = board[rto] = PIECE_NONE;
+  board[kfrom] = king;
+  board[rfrom] = rook;
 
   // Update piece lists
   pieceList[us][KING][index[kto]] = kfrom;
@@ -1461,7 +1467,7 @@ int Position::see(Move m) const {
   // Handle en passant moves
   if (st->epSquare == to && piece_type(piece_on(from)) == PAWN)
   {
-      Square capQq = (side_to_move() == WHITE ? to - DELTA_N : to - DELTA_S);
+      Square capQq = to - pawn_push(side_to_move());
 
       assert(capturedType == PIECE_TYPE_NONE);
       assert(piece_type(piece_on(capQq)) == PAWN);
@@ -1718,8 +1724,7 @@ template bool Position::is_draw<true>() const;
 
 bool Position::is_mate() const {
 
-  MoveStack moves[MAX_MOVES];
-  return in_check() && generate<MV_LEGAL>(*this, moves) == moves;
+  return in_check() && !MoveList<MV_LEGAL>(*this).size();
 }
 
 
@@ -1750,11 +1755,11 @@ void Position::init() {
 
   for (Square s = SQ_A1; s <= SQ_H8; s++)
       for (Piece p = WP; p <= WK; p++)
-          PieceSquareTable[p][s] = make_score(MgPST[p][s], EgPST[p][s]);
+          pieceSquareTable[p][s] = make_score(MgPST[p][s], EgPST[p][s]);
 
   for (Square s = SQ_A1; s <= SQ_H8; s++)
       for (Piece p = BP; p <= BK; p++)
-          PieceSquareTable[p][s] = -PieceSquareTable[p-8][flip_square(s)];
+          pieceSquareTable[p][s] = -pieceSquareTable[p-8][flip_square(s)];
 }