Color

From Chessprogramming wiki
Jump to: navigation, search

Home * Chess * Color

In Chess, Color refers to the color of the two piece sets, the color each player is assigned to, the side to move, and the color of a square. The pieces are divided, into white and black sets, but their colors should not taken literally. The players are referred to as "White" and "Black", they control their associated pieces. The colors of the sixty-four squares alternate and are referred to "light squares" and "dark squares", sometimes also referred to "White" and "Black" squares.

Color Definition

Since there are only two colors, one bit is sufficient to encode them. This is how one may define colors in C++:

enum enumColor {
   ecWhite = 0,
   ecBlack = 1
};

Toggle Color

Since the players alternately move, one need to toggle the side to move color after each move made inside the Chess Position object. This can be done by subtracting color from one (ecBlack), ...

inline enumColor toggleColor(enumColor color) {
   return ecBlack - color;
}

... or a little bit cheaper, by xoring the color as left operand inside a register or memory cell with an immediate constant (ecBlack).

inline enumColor toggleColor(enumColor color) {
   return color ^ ecBlack;
}

See also

External Links

References

Up one Level