]> git.sesse.net Git - stockfish/blobdiff - src/position.cpp
Revert "Fix random moves when time < 10ms"
[stockfish] / src / position.cpp
index 98ba5c6009c663e1304faa9e9325499bd86ff141..0b687a8d1496e797e22983b36ca7b0819fd92ea8 100644 (file)
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include <algorithm>
 #include <cassert>
 #include <cstring>
 #include <iomanip>
 #include <iostream>
 #include <sstream>
-#include <algorithm>
 
 #include "bitcount.h"
 #include "movegen.h"
@@ -1149,9 +1149,9 @@ void Position::clear() {
   startState.epSquare = SQ_NONE;
   st = &startState;
 
-  for (int i = 0; i < 8; i++)
+  for (int i = 0; i < PIECE_TYPE_NB; i++)
       for (int j = 0; j < 16; j++)
-          pieceList[0][i][j] = pieceList[1][i][j] = SQ_NONE;
+          pieceList[WHITE][i][j] = pieceList[BLACK][i][j] = SQ_NONE;
 }
 
 
@@ -1223,6 +1223,7 @@ Key Position::compute_material_key() const {
 /// game and the endgame. These functions are used to initialize the incremental
 /// scores when a new position is set up, and to verify that the scores are correctly
 /// updated by do_move and undo_move when the program is running in debug mode.
+
 Score Position::compute_psq_score() const {
 
   Score score = SCORE_ZERO;
@@ -1254,21 +1255,14 @@ Value Position::compute_non_pawn_material(Color c) const {
 }
 
 
-/// 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.
-bool Position::is_draw() const {
+/// Position::is_draw() tests whether the position is drawn by 50 moves rule
+/// or by repetition. It does not detect stalemates.
 
-  // Draw by material?
-  if (   !pieces(PAWN)
-      && (non_pawn_material(WHITE) + non_pawn_material(BLACK) <= BishopValueMg))
-      return true;
+bool Position::is_draw() const {
 
-  // Draw by the 50 moves rule?
   if (st->rule50 > 99 && (!checkers() || MoveList<LEGAL>(*this).size()))
       return true;
 
-  // Draw by repetition?
   int i = 4, e = std::min(st->rule50, st->pliesFromNull);
 
   if (i <= e)
@@ -1279,7 +1273,7 @@ bool Position::is_draw() const {
           stp = stp->previous->previous;
 
           if (stp->key == st->key)
-              return true;
+              return true; // Draw after first repetition
 
           i += 2;
 
@@ -1293,45 +1287,36 @@ bool Position::is_draw() const {
 /// Position::flip() flips position with the white and black sides reversed. This
 /// is only useful for debugging especially for finding evaluation symmetry bugs.
 
+static char toggle_case(char c) {
+  return char(islower(c) ? toupper(c) : tolower(c));
+}
+
 void Position::flip() {
 
-  const Position pos(*this);
+  string f, token;
+  std::stringstream ss(fen());
 
-  clear();
+  for (Rank rank = RANK_8; rank >= RANK_1; rank--) // Piece placement
+  {
+      std::getline(ss, token, rank > RANK_1 ? '/' : ' ');
+      f.insert(0, token + (f.empty() ? " " : "/"));
+  }
 
-  sideToMove = ~pos.side_to_move();
-  thisThread = pos.this_thread();
-  nodes = pos.nodes_searched();
-  chess960 = pos.is_chess960();
-  gamePly = pos.game_ply();
+  ss >> token; // Active color
+  f += (token == "w" ? "B " : "W "); // Will be lowercased later
 
-  for (Square s = SQ_A1; s <= SQ_H8; s++)
-      if (!pos.is_empty(s))
-      {
-          Piece p = Piece(pos.piece_on(s) ^ 8);
-          put_piece(~s, color_of(p), type_of(p));
-      }
+  ss >> token; // Castling availability
+  f += token + " ";
 
-  if (pos.can_castle(WHITE_OO))
-      set_castle_right(BLACK, ~pos.castle_rook_square(WHITE, KING_SIDE));
-  if (pos.can_castle(WHITE_OOO))
-      set_castle_right(BLACK, ~pos.castle_rook_square(WHITE, QUEEN_SIDE));
-  if (pos.can_castle(BLACK_OO))
-      set_castle_right(WHITE, ~pos.castle_rook_square(BLACK, KING_SIDE));
-  if (pos.can_castle(BLACK_OOO))
-      set_castle_right(WHITE, ~pos.castle_rook_square(BLACK, QUEEN_SIDE));
+  std::transform(f.begin(), f.end(), f.begin(), toggle_case);
 
-  if (pos.st->epSquare != SQ_NONE)
-      st->epSquare = ~pos.st->epSquare;
+  ss >> token; // En passant square
+  f += (token == "-" ? token : token.replace(1, 1, token[1] == '3' ? "6" : "3"));
 
-  st->checkersBB = attackers_to(king_square(sideToMove)) & pieces(~sideToMove);
+  std::getline(ss, token); // Half and full moves
+  f += token;
 
-  st->key = compute_key();
-  st->pawnKey = compute_pawn_key();
-  st->materialKey = compute_material_key();
-  st->psq = compute_psq_score();
-  st->npMaterial[WHITE] = compute_non_pawn_material(WHITE);
-  st->npMaterial[BLACK] = compute_non_pawn_material(BLACK);
+  set(f, is_chess960(), this_thread());
 
   assert(pos_is_ok());
 }