Tuesday, March 21, 2006

The Rook

Finally, enough motivation struck me to start working on my school project again tonight. This was, of course, after playing a 5-table, 50-person, $5, multi-table at party. How did I do? What's my magic number? Would you believe it? 3. I came in third. At the end, my AQo was taken out by A10o. So it goes.

I was expecting the Rook to be a fairly complex piece to generate the bitboards for. Mainly because it can move in 2 directions. Horizontally and Vertically. It turns out to be quite simple. The key lies in having 2 pre-built bitboards ready to go. These are a bitboard that represents the A-File, and a bitboard that represents the first rank. So what we have is the following:

ulong bbAFile = 0x0101010101010101;

which maps out to:
10000000
10000000
10000000
10000000
10000000
10000000
10000000
10000000

ulong bbFirstRank = 0x00000000000000FF;

which maps out to:
00000000
00000000
00000000
00000000
00000000
00000000
00000000
11111111

Let's start with the file. If we are on the A file, we use the bitboard above. If we are on the B file, we would simply shift the A file bitboard right 1. For the rank, if we are on the 1st rank we would use the bitboard above (in my code, the 1st rank is on top, so it's a little different). If you're on the second rank, you would simply shift right 8. To get the move for a given square, you would then XOR the two together. You need to XOR so that the square that the rook is on is not counted as a legal move. An XOR works like this:

0 0 = 0
0 1 = 1
1 0 = 1
1 1 = 0

So the result for the square A1 would look like:
10000000
10000000
10000000
10000000
10000000
10000000
10000000
01111111

I still need to work on the legal move generation for the rooks, but the pre-generated bitboards were definitely a lot of fun. Why pre-generate them? Because I keep them in an array of 64, so if I know that a Rook is on the 10th square (B2), I can simply look at bbLegalRookMoves[11] and have the bitboard of all the legal moves without having to generate it at move generation time. It speeds up the processing considerably... in theory. In C#, we'll see. since it uses managed code it's array processing may not be nearly as fast as in C.

Time to get back to work.

Ed.

No comments: