]> git.sesse.net Git - stockfish/commitdiff
Rewrite flip() to use FEN string manipulation
authorMarco Costalba <mcostalba@gmail.com>
Mon, 5 Aug 2013 09:06:23 +0000 (11:06 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Mon, 5 Aug 2013 10:58:14 +0000 (12:58 +0200)
Instead of dealing directly with internal parameters
just "flip" the FEN string and set the position from
that.

No functional change.

src/position.cpp

index 98ba5c6009c663e1304faa9e9325499bd86ff141..e1f07bfb4857292bfb7d479cd9f270d05668fd60 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"
@@ -1293,45 +1293,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 isupper(c) ? tolower(c) : toupper(c);
+}
+
 void Position::flip() {
 
-  const Position pos(*this);
+  string f, token;
+  std::stringstream ss(fen());
 
-  clear();
-
-  sideToMove = ~pos.side_to_move();
-  thisThread = pos.this_thread();
-  nodes = pos.nodes_searched();
-  chess960 = pos.is_chess960();
-  gamePly = pos.game_ply();
+  for (int i = 0; i < 8; i++)
+  {
+      std::getline(ss, token, i < 7 ? '/' : ' ');
+      std::transform(token.begin(), token.end(), token.begin(), toggle_case);
+      f.insert(0, token + (i ? "/" : " "));
+  }
 
-  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; // Side to move
+  f += (token == "w" ? "b " : "w ");
 
-  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));
+  ss >> token; // Castling flags
+  std::transform(token.begin(), token.end(), token.begin(), toggle_case);
+  f += token + " ";
 
-  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); // Full and half 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());
 }