Author: Russell Reagan
Date: 14:20:50 02/11/04
Go up one level in this thread
On February 11, 2004 at 15:45:22, Anthony Cozzie wrote:
>IMHO, it is better to have a more compact piece representation. Zappa uses [P=1
>.. K=6] << 1 + color, which works pretty well. This way is_pawn() and
>is_white() are also simple masks, but indexing into a table is not really
>possible with your approach (some 20 times larger -> not cache friendly). You
>will find that there are a lot of table index operations in a chess program.
I agree. I would replace these kinds of functions:
typedef unsigned char PIECE;
inline bool is_black(PIECE piece)
{
return (piece & 1 == 1);
}
inline bool is_pawn(PIECE piece)
{
return (piece & W_PAWN != 0);
}
With something like this:
// 32-bit variable faster on 32-bit hardawre
typedef unsigned int COLOR;
typedef unsigned int PIECE;
// A piece has the form:
// +-------+------+------+------+
// | bit0 | bit1 | bit2 | bit3 |
// +-------+--------------------+
// | color | piece |
// +-------+--------------------+
const COLOR COLOR_MASK = 0x1;
const PIECE PIECE_MASK = 0xE;
inline COLOR color_of (PIECE piece)
{
return piece & COLOR_MASK;
}
inline PIECE type_of (PIECE piece)
{
return piece & PIECE_MASK;
}
Then if you want to know if a piece is a pawn, you can do:
if (type_of(piece) == PAWN)
...;
if (color_of(piece) == WHITE)
...;
Another thing to consider is this. What if you are not interested in whether a
piece on a square is white or black, but rather the opposite color? Usually if
you are doing something like generating captures, this is more useful. With your
approach you might have to do this:
if (side_to_move == WHITE)
if (is_black(piece))
generate_capture();
else
if (is_white(piece))
generate_capture();
With the other way I proposed, all you do is:
if (color_of(piece) == enemy_color_of(side_to_move))
generate_capture();
One other way I like is to have the board be an array of pointers to piece
structs like this:
struct Piece
{
int color; // white, black
int type; // pawn, knight, etc.
int square; // a1, b1, c1, etc.
int status; // alive, dead, etc.
};
Piece * board[MAX_SQUARES];
If you set things up right, you can do a lot very efficiently.
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.