A position flip is the action by which a position is replaced with a copy of itself modified such that each man is replaced with the corresponding man of the opposite color and then moved to the opposite side (front/back) of the board (file is same, but rank is flipped).
Other items must also be flipped: these include the color on the move, the castling status, and the en passant target square.
I notice that in Crafty, which has a flip command, there doesn't seem to be a flip of the en passant target. Am I missing something here?
Symbolic goes a bit further with its position flip. Not only are the board and the FEN scalars flipped, but also the entire move history and the saved state history. This means that it's possible to retract played moves in the flipped position all the way back to its flipped start. Also: flip(flip(P)) ≡ P
A position flip is a handy user command. It can also be used during opening book generation when (position, move) pair data are all stored using a White-to-move convention; this allows retrieval of "reversed" opening data.
Position flipping
Moderators: hgm, Rebel, chrisw
-
- Posts: 2684
- Joined: Sat Jun 14, 2008 9:17 pm
Re: Position flipping
This is another thing I am about to drop from Stockfish. The only reason while is still there is because it is from original Tord's code....but I found no use for it even once in the last years...sje wrote:A position flip
-
- Posts: 6401
- Joined: Thu Mar 09, 2006 8:30 pm
- Location: Chicago, Illinois, USA
Re: Position flipping
It is nice for debugging purposes. In debug code, it is good to flip the position, evaluate it, and make sure the score is the same but negative. I mirror it left-right if no castle is available. This process is done every time evaluation is called (in DEBUG mode only, of course) Over the years, I caught bugs very early in this way.mcostalba wrote:This is another thing I am about to drop from Stockfish. The only reason while is still there is because it is from original Tord's code....but I found no use for it even once in the last years...sje wrote:A position flip
Miguel
-
- Posts: 4675
- Joined: Mon Mar 13, 2006 7:43 pm
Re: Position flipping
It can also be used during debugging for verifying that perft(P, N) = perft(flip(P), N).michiguel wrote:It is nice for debugging purposes. In debug code, it is good to flip the position, evaluate it, and make sure the score is the same but negative. I mirror it left-right if no castle is available. This process is done every time evaluation is called (in DEBUG mode only, of course) Over the years, I caught bugs very early in this way.
-
- Posts: 2684
- Joined: Sat Jun 14, 2008 9:17 pm
Re: Position flipping
This IMHO should be left to an external tool that reverses FEN strings (BTW do you know one?) more than the engine itself.sje wrote:It can also be used during debugging for verifying that perft(P, N) = perft(flip(P), N).michiguel wrote:It is nice for debugging purposes. In debug code, it is good to flip the position, evaluate it, and make sure the score is the same but negative. I mirror it left-right if no castle is available. This process is done every time evaluation is called (in DEBUG mode only, of course) Over the years, I caught bugs very early in this way.
-
- Posts: 6401
- Joined: Thu Mar 09, 2006 8:30 pm
- Location: Chicago, Illinois, USA
Re: Position flipping
In the case of the evaluation is not nearly as effective.mcostalba wrote:This IMHO should be left to an external tool that reverses FEN strings (BTW do you know one?) more than the engine itself.sje wrote:It can also be used during debugging for verifying that perft(P, N) = perft(flip(P), N).michiguel wrote:It is nice for debugging purposes. In debug code, it is good to flip the position, evaluate it, and make sure the score is the same but negative. I mirror it left-right if no castle is available. This process is done every time evaluation is called (in DEBUG mode only, of course) Over the years, I caught bugs very early in this way.
Miguel
-
- Posts: 2684
- Joined: Sat Jun 14, 2008 9:17 pm
Re: Position flipping
Anyhow it is easier to flip with string manipulation (at least in C++) starting from the FEN representation of a position:
Code: Select all
static char toggle_case(char c) {
return isupper(c) ? tolower(c) : toupper(c);
}
void Position::flip() {
string f, token;
std::stringstream ss(fen());
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 ? "/" : " "));
}
ss >> token; // Side to move
f += (token == "w" ? "b " : "w ");
ss >> token; // Castling flags
std::transform(token.begin(), token.end(), token.begin(), toggle_case);
f += token + " ";
ss >> token; // En-passant square
f += (token == "-" ? token : token.replace(1, 1, token[1] == '3' ? "6" : "3"));
std::getline(ss, token); // Full and half moves
f += token;
set(f, is_chess960(), this_thread());
assert(pos_is_ok());
}
-
- Posts: 4675
- Joined: Mon Mar 13, 2006 7:43 pm
Re: Position flipping
If there are no pawns on the board, you can also reflect the position about either main diagonal.michiguel wrote:In debug code, it is good to flip the position, evaluate it, and make sure the score is the same but negative. I mirror it left-right if no castle is available.
-
- Posts: 4675
- Joined: Mon Mar 13, 2006 7:43 pm
Re: Position flipping
That code doesn't flip any position history. Move retraction will not work, and neither will repetition detection.mcostalba wrote:Anyhow it is easier to flip with string manipulation (at least in C++) starting from the FEN representation of a position:
-
- Posts: 2684
- Joined: Sat Jun 14, 2008 9:17 pm
Re: Position flipping
This was your previous (few hours ago) definition of flip:sje wrote:That code doesn't flip any position history. Move retraction will not work, and neither will repetition detection.mcostalba wrote:Anyhow it is easier to flip with string manipulation (at least in C++) starting from the FEN representation of a position:
Code: Select all
A position flip is the action by which a position is replaced with a copy of itself modified such that each man is replaced with the corresponding man of the opposite color and then moved to the opposite side (front/back) of the board (file is same, but rank is flipped).
Other items must also be flipped: these include the color on the move, the castling status, and the en passant target square.
What I have written flips according to this definition.