Changes

Jump to: navigation, search

Color Flipping

9,254 bytes added, 18:24, 10 May 2018
Created page with "'''Home * Chess * Position * Color Flipping''' '''Color Flipping''' a chess position refers to the [[Vertical Flipping|vertical flipping]..."
'''[[Main Page|Home]] * [[Chess]] * [[Chess Position|Position]] * Color Flipping'''

'''Color Flipping''' a chess position refers to the [[Vertical Flipping|vertical flipping]] or mirroring of all [[Pieces|pieces]] along the horizontal axis between the 4th and 5th [[Ranks|rank]] also swapping the [[Color|color]] of the flipped pieces from White to Black and vice versa, the [[Side to move|side to move]], the [[Castling rights|castling rights]] and the rank of a possible [[En passant|en passant]] target square from six to three or vice versa accordantly. The resulting position is mirror equivalent, pawn and pieces have the same [[Pawn Spans|span length]] or home rank relation. A white pawn on the light square c2 becomes a black pawn on the dark square c7. Color flipping a vertical symmetrical or mirror position (same castling rights, no en passant possible) results in the same position as if the side to move performed a [[Null Move|null move]]:

=Sample=
{| class="wikitable"
|-
! Giuoco Piano
! Giuoco Piano Symmetrical
|-
| <fentt border="double" style="font-size:24pt">r1bqk1nr/pppp1ppp/2n5/2b1p3/2B1P3/5N2/PPPP1PPP/RNBQK2R</fentt>
| <fentt border="double" style="font-size:24pt">r1bqk2r/pppp1ppp/2n2n2/2b1p3/2B1P3/2N2N2/PPPP1PPP/R1BQK2R</fentt>
|-
| r1bqk1nr/pppp1ppp/2n5/2b1p3/2B1P3/5N2/PPPP1PPP/RNBQK2R w KQkq -
| r1bqk2r/pppp1ppp/2n2n2/2b1p3/2B1P3/2N2N2/PPPP1PPP/R1BQK2R w KQkq -
|-
| <fentt border="double" style="font-size:24pt">rnbqk2r/pppp1ppp/5n2/2b1p3/2B1P3/2N5/PPPP1PPP/R1BQK1NR</fentt>
| <fentt border="double" style="font-size:24pt">r1bqk2r/pppp1ppp/2n2n2/2b1p3/2B1P3/2N2N2/PPPP1PPP/R1BQK2R</fentt>
|-
| rnbqk2r/pppp1ppp/5n2/2b1p3/2B1P3/2N5/PPPP1PPP/R1BQK1NR b KQkq -
| r1bqk2r/pppp1ppp/2n2n2/2b1p3/2B1P3/2N2N2/PPPP1PPP/R1BQK2R b KQkq -
|}
<span id="BookTransposition"></span>
=Book Transpositions=
During the [[Opening|opening]], in some lines, White may intentionally lose a [[Tempo|tempo]], to [[Transposition|transpose]] into a color flipped position of an opening White likes to play the "Black" side, which might be considered in chess programs [[Opening Book|book]] access code, see for instance [[WMCCC 1988#Pandix-Y!|WMCCC 1988, Pandix-Y!88]].

=Flipping an 8x8 Board=
An [[8x8 Board]] with a [[Squares|rank-file mapping]], needs to perform an [[General Setwise Operations#ExclusiveOr|exclusive or]] with 56 (A8 in [[Square Mapping Considerations#LittleEndianRankFileMapping|LERF]]) to vertically flip square coordinates. Assuming piece color is least significant bit and empty square is zero, a pure 8x8 Board may be color flipped that way in [[C]]:
<pre>
int board[64], sq, s, f;

for (sq = 0; sq < 32; ++sq) {
s = board[sq];
f = board[sq^56];
board[sq] = f ^ (f != 0);
board[sq^56] = s ^ (s != 0);
}
</pre>
<span id="Debugging"></span>
=Looking for Bugs=
A side to move relative, bug free static [[Evaluation|evaluation]] should have the same [[Score|score]] for both positions. [[Search]] is a different issue, since [[Move Ordering|move ordering]] might be slightly different for instance in bitboard programs due to different order in [[Bitboard Serialization|bitboard serialization]] if one doesn't use a [[Bitboard Serialization#BlackorWhite|symmetrical bitscan]] by side to move. Similar to the [https://en.wikipedia.org/wiki/Butterfly_effect butterfly effect] in [https://en.wikipedia.org/wiki/Chaos_theory chaos theory] a minor initial difference might amplify to enormous effects during the search. However, if the search behavior of a [[Tactics|tactical]] [[Test-Positions|test position]] of a most deterministic chess engine (e.g. no [[Parallel Search|parallel search]]), varies by magnitudes in solution [[Depth|depth]] and time between original and color flipped position, one may take a closer look performing some [[Logging|logging]] and/or [[Debugging|debugging]].
<span id="Monochrome"></span>
=Monochrome=
An idea to unify and simplify [[Move Generation|move generation]] and to get rid of order dependencies, is to perform a color flip of the position and its internal [[Board Representation|board representation]] directly after [[Make Move|making a move]], to make the engine white to move only, that is only generating white moves, or similar, to keep a normal and flipped board to [[Incremental Updates|update them incrementally]].
<span id="MicroChess"></span>
==MicroChess==
[[Peter Jennings|Jennings']] [[6502#MicroChess|approach]] with [[MicroChess]] was similar <ref>[[Peter Jennings]] ('''1976'''). ''[http://www.computerhistory.org/chess/full_record.php?iid=doc-431614f6d8478 MicroChess, a Chess playing program for the 6502 Microcomputer]''. [http://archive.computerhistory.org/projects/chess/related_materials/text/4-1.MicroChess_%20Manual_for_6502.Micro-Ware/MicroChessManual.PETER_JENNINGS.062303071.sm.pdf pdf], Courtesy of [[Peter Jennings]], [[The Computer History Museum]]</ref>, but instead of vertical flipping (xor 56) he reversed or rotated the board by 180 degrees (xor 63), like players changing places and looking the board alternately from White's or Black's point of view, with some considerations of the additional horizontal mirroring, concerning [[Castling|castling]] and [[En passant|en passant]].

==Quad-Bitboards==
[[Gerd Isenberg]] proposed a make move - color flipping approach using [[Quad-Bitboards]] <ref>[http://www.talkchess.com/forum/viewtopic.php?t=17589 color-flipped perft] by [[Gerd Isenberg]], November 04, 2007</ref> . The black [[Zobrist Hashing|Zobrist keys]] were the byte-swapped white Zobrist keys from the vertically flipped squares (xor 56). In symmetrical positions this gains some additional hits while probing the [[Transposition Table|transposition table]]. But symmetrical mirror positions, or symmetrical subsets have the property, their [[General Setwise Operations#ExclusiveOr|xored]] hashkey has only 32 distinct bits since hashkey equals [[Flipping Mirroring and Rotating#FlipVertically|flipVertical]](hashkey), for instance:
<pre>
01020304|50607080 // white pieces
^ 80706050|04030201 // black pieces of a mirror position
= 81726354|54637281
</pre>
==Six-Two Bitboards==
The common [[Bitboard Board-Definition#SixTwo|six piece kind and two color bitboard]] board representation takes eight byte swaps to color flip the whole board.

==Keeping two Boards==
Rather than flipping the board after each make, [[Reinhard Scharnagl]] proposed monochrome move generation by keeping a normal and a color flipped board, both [[Incremental Updates|incremental updated]] <ref>[http://www.open-chess.org/viewtopic.php?f=39&t=1039 Computerschach mit monochromen Methoden ?] by [[Reinhard Scharnagl]], [[Computer Chess Forums|OpenChess - Deutsches Forum]], January 27, 2011 (German)</ref> . He further suggested asymmetrical [[Pieces#PieceCoding|piece coding]] with less piece codes (only one or two) for the pieces of the side not to move. His approach has also the advantage to keep independent Zobrist keys for all squares and pieces.

=See also=
* [[Color]]
* [[Diagonal Mirroring]]
* [[Flipping Mirroring and Rotating|Flipping, Mirroring and Rotating]] of [[Bitboards]]
* [[Horizontal Mirroring]]
* [[Side to move]]
* [[Vertical Flipping]]

=Publications=
* [[Fernand Gobet]] ('''1993'''). ''[http://people.brunel.ac.uk/%7Ehsstffg/papers/ModelChessMem/Chess%20Memory.html A Computer Model of Chess Memory].'' Proceedings of the 15th Annual Meeting of the Cognitive Science Society, pp. 463-468.

=Forum Posts=
* [https://www.stmintz.com/ccc/index.php?id=199005 Conversion to dual positions for EPD testsuites?] by [[Leen Ammeraal]], [[CCC]], November 26, 2001
* [http://www.talkchess.com/forum/viewtopic.php?t=13923 fen to fen functions] by [[Uri Blass]], [[CCC]], May 21, 2007
* [http://www.talkchess.com/forum/viewtopic.php?t=14168 Comparison data of Crafty vs Rybka that I promised 2 days ago] by Les, [[CCC]], June 01, 2007
* [http://www.talkchess.com/forum/viewtopic.php?t=17589 color-flipped perft] by [[Gerd Isenberg]], November 04, 2007
* [http://www.talkchess.com/forum/viewtopic.php?t=30423 Board without color flags] by [[Fritz Reul]], [[CCC]], October 31, 2009
* [http://www.talkchess.com/forum/viewtopic.php?t=48843 Position flipping] by [[Steven Edwards]], [[CCC]], August 05, 2013
* [http://www.talkchess.com/forum/viewtopic.php?t=54465 Black/White symmetry in move generation] by Jeffery A Esposito, [[CCC]], November 25, 2014 » [[Move Generation]]
* [http://www.talkchess.com/forum/viewtopic.php?t=54704 Symmetric move generation using bitboards] by [[Lasse Hansen]], [[CCC]], December 20, 2014
* [http://www.talkchess.com/forum/viewtopic.php?p=663148 reversed-color transpositions] by [[John Philip Fishburn|John Fishburn]], [[CCC]], March 11, 2016
* [http://www.talkchess.com/forum/viewtopic.php?t=64003 FEN - Flipper for Windows] by [[Matthias Gemuh]], [[CCC]], May 17, 2017 » [[Forsyth-Edwards Notation|FEN]], [[Extended Position Description|EPD]]

=External Links=
* [http://www.jimloy.com/chess/mirror1.htm A "Mirror" Chess Problem] from [http://www.jimloy.com/chess/chess.htm Jim Loy's Chess Pages]
* [http://www.chessvariants.org/other.dir/mirror.html Playing Chess Against Its Mirror Image]

=References=
<references />

'''[[Chess Position|Up one Level]]'''

Navigation menu