Encoding Moves

Home * Chess * Moves * Encoding Moves



Encoding Moves inside a chess program refers to both game records or game notation, and search related generation, make and unmake move to incremental update the board. During generation, moves are stored inside move lists, and best moves or refutation moves failing high inside the search are stored inside the transposition table, Killer slots, PV- or refutation table, and moves, or certain aspects of a move, such as origin square and the target square are used to index butterfly boards for history- or countermove heuristic.

=Space-Time Tradeoff= Move encoding in game records and game databases is usually designed to minimize size, while in search efficiency in generating is the main concern, considering board representation and other data structures like attack and defend maps. In general, move encoding either comprehend full information, that is contains involved pieces and square coordinates, or more common, omits the redundant piece information and relies on a adequate board representation to lookup the pieces by square. In Alpha-Beta like search, an important consideration is lazy move generation, to delay acquisition of certain information until it is really needed, which might be never in case of Cut-nodes.

=Information Required=

From-To Based
The information required to uniquely describe a move is the initial square, also called from-, origin- or departure square, and the target square, also called to- or destination square, and in case of promotions the promoted piece code. While this from-to information is also sufficient for castling in standard chess, due to the otherwise impossible double king step, it might not in Chess960. Therefore and also for efficiency reasons, castles are tagged as "special" moves. Such a move encoding conveniently fits inside a 16-bit word, 6 bits for from-to square each to index a butterfly board, still leaves a nibble for flags for move kind and promoted piece code, for instance this arbitrary flags:

Extended Move Structure
The information which piece performs the move and which piece is captured (if any) is implicit given by the from-to squares, with the requirement to lookup the current board before making the move, but in case of captures not after making or before unmaking the move. Some programs use a 32-bit double word as extended move structure at generation time as well for making/unmaking moves, with the upper bits used for a move score scaled by various move ordering techniques for instance dedicated piece-square tables and/or history heuristic, and perhaps two three bit codes for the moving and captured piece (if any) which also implies a kind of MVV-LVA coding. Also the extended move may apply composite indices for incremental update of Zobrist- or BCH keys.

Having the piece codes inside the move structure safes the board lookups during make, and makes storing captured pieces on a ply stack for unmake needless. Of course for space considerations to store moves inside transposition table, Killer slots, PV- or refutation table, the compact 16-bit move structure is still adequate for coordinate and move kind comparison with the lower 16 bits of the extended move structure.

C++ Sample
Rather than using bitfield move structures or classes in C and C++, most programmers rely on scalar integers, such as short and int for 16- or 32-bit words, but implement the composition and extraction while writing and reading various structure elements by explicit shifts and masks, likely encapsulated thought an interface with most likely inlined functions (or macros) to hide the internal representation. Further extended move structures might either embed or inherit this most compact base structure or class, which might already rely the native 32-bit integer type to avoid x86 16-bit optimization issues.

class CMove { CMove(unsigned int from, unsigned int to, unsigned int flags) { m_Move = ((flags & 0xf)<<12) | ((from & 0x3f)<<6) | (to & 0x3f); }  void operator=(CMove a} {m_Move = a.m_Move;}

unsigned int getTo const {return m_Move & 0x3f;} unsigned int getFrom const {return (m_Move >> 6) & 0x3f;} unsigned int getFlags const {return (m_Move >> 12) & 0x0f;}

void setTo(unsigned int to) {m_Move &= ~0x3f; m_Move |= to & 0x3f;} void setFrom(unsigned int from) {m_Move &= ~0xfc0; m_Move |= (from & 0x3f) << 6;} ...

bool isCapture const {return (m_Move & CAPTURE_FLAG) != 0;} ...

unsigned int getButterflyIndex const {return m_Move & 0x0fff;} bool operator==(CMove a) const {return (m_Move & 0xffff) == (a.m_Move & 0xffff);} bool operator!=(CMove a) const {return (m_Move & 0xffff) != (a.m_Move & 0xffff);}

unsigned short asShort const {return (unsigned short) m_Move;}

protected: unsigned int m_Move; // or short or template type };

=Various Encodings and Decorations=

Algebraic Notation
Based on Philipp Stamma's short algebraic chess notation, a move can be described by the moving piece code and destination square. In case of disambiguating moves if two (or more) identical pieces can move to the same square, the file of departure, or if files are identical as well, the rank or both file and rank are given. A capture move, denoted by the symbol 'x' (takes) does not explicitly specify the captured piece and requires a look to the board as well.

Chess programs usually use algebraic notations concerning the user interface and Portable Game Notation - for appropriate conversion they have to deal with disambiguating source squares, that is need to be aware of all other moves of this piece-kind to the destination square to determine whether the from square needs additional file and/or rank information despite the moving piece.

Direction-Target
Another move encoding alternative motivated by direction wise target square serialization in bitboards is not to use the from-square but target square and direction. While the information is non-ambiguous it needs some effort with ray-lookup and bitscan to determine the from-square.

Check Flag
It might be interesting to determine whether a move gives check in advance during generation time, to possibly score them higher for move ordering, i. e. to don't reduce or even extend them, and also to safe in check detection after make move. However, as mentioned, lazy move generation is required, to delay acquisition of information until it is really needed, which might be never in case of Cut-nodes. Additionally an early determined checking move, even if failing high, usually implies a huge sub-tree due to check extensions, while an early non checking moves likely makes a cheaper cutoff for most futile Cut-nodes. Therefor, determining checking moves in advance is not that recommended for most board-representations.

Move Index
If the generated moves inside a move list are deterministic, one may encode moves as relative list index. Since the maximum number of moves per positions seems 218, one byte is enough to index the move. This encoding was used in early game databases.

Move Enumeration
Based on Influence Quantity of Pieces one may enumerate all moves, to specify a unique determined move number with respect to moving piece, from- and to squares, captured piece (if any) and promoted piece inside a 16-bit range.

Piece Moves
Reversible and Captures, Influence Quantity of Pieces times six target combinations for empty and five possible capture targets.

All Moves
Sheet of all moves, considering Castling (but no null move):

=See also=
 * Influence Quantity of Pieces
 * Move Generation
 * Move Ordering

=Forum Posts=

2000 ...

 * Subject: Maximum Number of Legal Moves by Andrew Shapira, CCC, May 08, 2005
 * Two doubts by Fermin Serrano, CCC, March 10, 2008

2010 ...

 * max amount of moves from a position? by Srdja Matovic, CCC, June 10, 2011 » Chess Maxima
 * Contest: Find Position with the most moves by Charles Roberson, CCC, December 09, 2011
 * Move encoding by Russell Reagan, CCC, April 21, 2014
 * Killer and move encoding by Fabio Gobbato, CCC, August 14, 2014 » Killer Heuristic

2020 ...

 * Generating all the moves on a board by Marc-Philippe Huget, CCC, May 07, 2020

=External Links=
 * Move Representation - Computer Architecture and Languages Laboratory, University of Maribor
 * Code from Wikipedia

=References=

Up one Level