mirror of
https://github.com/HChaZZY/Stockfish.git
synced 2025-12-20 09:06:45 +08:00
Rewrite flip() to use FEN string manipulation
Instead of dealing directly with internal parameters just "flip" the FEN string and set the position from that. No functional change.
This commit is contained in:
@@ -17,12 +17,12 @@
|
||||
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 (Square s = SQ_A1; s <= SQ_H8; s++)
|
||||
if (!pos.is_empty(s))
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
Piece p = Piece(pos.piece_on(s) ^ 8);
|
||||
put_piece(~s, color_of(p), type_of(p));
|
||||
std::getline(ss, token, i < 7 ? '/' : ' ');
|
||||
std::transform(token.begin(), token.end(), token.begin(), toggle_case);
|
||||
f.insert(0, token + (i ? "/" : " "));
|
||||
}
|
||||
|
||||
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; // Side to move
|
||||
f += (token == "w" ? "b " : "w ");
|
||||
|
||||
if (pos.st->epSquare != SQ_NONE)
|
||||
st->epSquare = ~pos.st->epSquare;
|
||||
ss >> token; // Castling flags
|
||||
std::transform(token.begin(), token.end(), token.begin(), toggle_case);
|
||||
f += token + " ";
|
||||
|
||||
st->checkersBB = attackers_to(king_square(sideToMove)) & pieces(~sideToMove);
|
||||
ss >> token; // En-passant square
|
||||
f += (token == "-" ? token : token.replace(1, 1, token[1] == '3' ? "6" : "3"));
|
||||
|
||||
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);
|
||||
std::getline(ss, token); // Full and half moves
|
||||
f += token;
|
||||
|
||||
set(f, is_chess960(), this_thread());
|
||||
|
||||
assert(pos_is_ok());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user