Refining the Piece Index Values

I’ve been thinking about the piece index values.  The original set had all of the pieces in a random list.  I think there is an advantage in having the following piece index values:

#define WHITE 0
#define BLACK 1

#define KNIGHT 1
#define BISHOP 2
#define ROOK 3
#define QUEEN 4
#define PAWN 5
#define KING 6

#define BLANK 0
#define WHITEROOK 3
#define WHITEQUEEN 4
#define WHITEPAWN 5
#define WHITEKING 6

#define BLACKROOK 10
#define BLACKQUEEN 11
#define BLACKPAWN 12
#define BLACKKING 13

With these values note the following:

You can detect the color easily with a simple (piece >>> 3).  This isn’t a big deal.

Since the king has the highest index of all piece, I can find any move in the global move table using the following code:

move = global_move_list[from][to][piece] + captured_piece

or for promoting a pawn:

move = global_move_list[from][to][piece] + captured_piece + (4 * promote_to)

That’s neat!

One of the weakness of bitboards is it is sometimes difficult to see which piece is occupying a particular square.  In a pure bitboard only structure you’d need to loop through all the piece bitboards until you find the relevant one.  This sounds cumbersome.  So I think I’ll also need to add a square[64] array to the board structure. This will simply hold the index of the piece occupying the square.

I think Maverick is going to fly!

  • Joao Marco

    Any special reason for pawn being 5 and not 1, or is just a matter of taste?
    #define PAWN 1
    #define KNIGHT 2
    #define BISHOP 3
    #define ROOK 4
    #define QUEEN 5
    #define KING 6
    seems more intuitive.

    • Steve Maughan

      I was thinking about pawn promotions. I’d like to be able to write such as the following move = global_move_list[from][to][piece] + promote_to. Since a pawn is irrelevant as a candidate for promote_to I thought it should have a higher index. In reality I doubt it makes any material difference to the speed. It’s early days for the engine so I’m just trying to think about how to make life easier as it gets more mature.

      • Thomas Petzke

        Hi Steve, if it is about making the life easier have a look at enums. It is very helpful in debugging if the debugger tells you the value of variable is WHITEPAWN instead of 5. Also already the compiler might detect whether you have used the wrong parameter order in a function call, it can’t do that if everything is just an integer.


        • Steve Maughan

          Thanks Thomas – I’ll take a look at enums. I use them all the time in Delphi but haven’t explored them in C.