
C program for chess board code#
Quad-Bitboards for dense "vertical" piece code nibbles._\ (_) (_) (_) (_) (_)Ĭhess symbols in Unicode, see also the Unicode Board. Boris Diplomat Chess Piece Font by David LindsayĪSCII art chess pieces, collected by Andreas Freise :ĪSCII art Staunton chess set by David Moeser.If you intend to implement stuff like Zillions of Games or different Fairy Chess variants, it might be a reasonable idea, to design derived piece classes with virtual functions and late binding during runtime. On the other hand, one should not exaggerate abstraction, to be aware of an wrapped integer type is fine, and that bytes are appropriate to store an array of piece codes, which may easily zero-extended to wider types if passed via registers. a type definition in C/C++ is recommend for better compile time checking, other programmer still prefer (unsigned) integer types for passing pieces (and moves, colors and square coordinates) around.
In general, it is recommend to hide implementation details, likely in C++ inside a wrapper class with inlined getter and setter, or in C a set of C-Macros and functions that "wrap" an scalar integer type. There are a lot of possibilities to encode pieces, however surrounding source code should not directly rely on the binary representation and range of that codes, e.g. One may even use one disjoint bit out of six per piece, yielding in exactly two bits set per regular piece code, for instance : Each white and black piece has a disjoint color bit set, and appropriate bits may indicate a sliding piece, orthogonal or diagonal stepping or sliding etc. To concatenate piece type and color the other way around is also quite common, color at the little end.Īn alternative coding approach for most efficient inner loops with cheap test-instructions in scanning board arrays in conjunction with move generation, specially captures, is to encode pieces by five and up to eight disjoint bits (one byte). That is why most programmers rely on positive ranges only and concatenate the three piece-type bits with one color-bit, for instance the color at the big end.Įpc_woff = ept_bpawn, // may be used as off the board blocker in mailboxĮpc_blacky = 8, // color code, may used as off the board blocker in mailboxĮpc_boff = ept_wpawn + epc_blacky, // may be used as off the board blocker in mailbox

But if we like to index (zero based) arrays in C, C++ or Java by piece codes an offset has to be considered - also the Two's Complement needs abs-functions or additional indirection to extract the pure piece code, rather than to mask (and eventually shift) three bits. One approach is to use negative values for black and positive for white (or color don't care) pieces. Often the order of enumeration is conform with the value of the pieces, for instance a C++ enumeration: This way piece-type completely specifies the mechanical move abilities, and three bits are still appropriate for a dense range to index tables or lookup-arrays, e.g. Harm Geert Muller proposed distinct white and black pawn codes inside the otherwise colorless range of piece-types, to take the different move-directions of pawns into account. Īny enumeration of piece-type codes is fine. Another piece-array contained the associated piece-types or zero if the piece is missing. Beside a bitboard board-definition using 12 piece bitboards and occupancy as union set, Lachex used a redundant 8x8 board array, containing those 1.32 piece-codes, but zero for empty squares.

Lachex for instance used following enumeration scheme: the a1-rook was labeled with 1, b1-knight with 2, a2-pawn with 9, the a8-rook with 17 and the h7-pawn with 32. Other programs distinguish not only piece-type and color, but enumerate all 32 pieces from their initial position, which label or code does not change during the course of a game (even after a possible promotion of a pawn) and might be one-to-one associated with the bit-position of a 32-bit piece set, and/or are used to index a piece list containing the current square the piece resides on.

C program for chess board plus#
Quite common is to use three bits to encode the piece-type plus one bit or Two's Complement (not recommend for languages with zero based array indices, like C, C++ or Java) for the color. Depending on the board representation, some programmers introduce an artificial blocking piece, which surrounds the embedded 8x8 boards inside a 10x12 board for cheaper off the board tests in offset move generation.įor cheaper extraction, most programmers prefer distinct coding of piece-types and the color of piece. Since only one piece may occupy one square at a time, one usually expands the range of piece codes with the Nil-Piece aka empty square, often encoded as zero. There are six types of pieces for each side, in total twelve different men.
