Monday, December 06, 2004

Punctuation

: is clearly superior to the - . At least some people seem to think so. I never was much a : guy, or even a ; So perhaps the - is where its at? Who knows. Mysteries of the universe I suppose.

I know I said it last time, but Flash really does suck the fat one, in the most unflattering of ways. I went back to refine my checkers, and found that a little function gets called twice, when it only needed to be called once at the top of my stack. This might not seem like much, but when it was being called a second time, it was when move "n+1" was being evaluated, where n is the current search depth. This meant that this particular function was getting called alot. Taking it out, and a few minor adjustments dropped my AI think time by 50%. W00t! Well, almost a woot, its still slow as molasses.

Then I got the idea - actually I read the idea - that bitboards would be the way to go - and hey, Flash actually supports bit operations like OR,XOR, left shift, etc. Being that I don't normally program AI I hadn't used this, but I immediately saw just how clever this solution is (which its cleverness has been around for a long time, imagine my surprise). You see, we could represent the board as a bunch of 0s and 1s. A 0 would be an empty square, while a 1 would have a checker in it. So the opening board of checkers would look like:

10101010
01010101
10101010
00000000
00000000
10101010
01010101
10101010

And then you would also have a bitboard representing ONLY the red checkers, ONLY the red kings, ONLY black checkers, and ONLY black kings. And then maybe 2 more, one for all the red pieces and all the black pieces (just to make things a bit faster)

So, instead of searching through an array of pieces and positions to find valid moves, you can just use the bit operations to see what you can do.

For example, a black checker (bottom) would have a move bit board of say

00000000
00000000
00000000
00000000
00000101
000000#0
00000000
00000000

The checker would be located at # (which would still be a 0, but I put the # in there to illustrate)
You can then compare that bitboard to the one holding all the pieces and quickly see if its occupied.

The real value of this (while still helpful in checkers) would be something like chess where a rook would move something like (again the # for illustration on location)


00000010
00000010
00000010
00000010
11 11 1 1#1
00000010
00000010
00000010

Anyway, there's plenty on the subject on Google's "chess bitboard"

So here's my a great idea, that could probabyl help speed things up marvelously, but can flash do it? No! Of course not! Why? Because it sucks the big fat one remember? Bitwise operations can only return 32bit intergers which means you can toss the chess/checkers board right out the window since each square is a bit, and there are 8 rows x 8 cols of bits (that's 64 for you non math majors ;) )


4 Comments:

At 7:59 AM, Anonymous Anonymous said...

A checkers board may be 8x8=64 squares but all the action happens on the black squares, leaving us exactly 32 bits of interestingness. The white squares will always be 0 so why not just leave them out. You do need to have unsigned integers for this, I know java blows ass because it only has signed integers. And no 64 bit thingies at all.

I also found a nice move generator, for example to generate all the moves in the direction North-East you can do this:

a = (board & 0f0f0f0f) << 4
b = (board & 00e0e0e0) << 3
moves = (a | b) & ~board

I reckon this is faster than a table lookup because there's no loop here.

You'll probably have to draw it out like I did to see it. You will have to to get the other directions. For jumps it should be possible to do the same trick, only with other masks and bigger shift values (twice as big?).

 
At 8:02 AM, Anonymous Anonymous said...

When I wrote ofofofof I clearly meant 0f0f0f0f (that is the number 0 instead of the letter o).

 
At 8:03 AM, Anonymous Anonymous said...

When I wrote "When I wrote ofofofof I clearly meant 0f0f0f0f (that is the number 0 instead of the letter o)" I was clearly under the impression that the number 0 and the letter o would be distinguishable from each other, font-wise. It appears they're not.

 
At 1:27 AM, Blogger DD3123 said...

no sorry, sadly to say, the 32 bit extended into flash means that (in flash at least) the last bit is reserved for the positive / negative sign. Which really means I only have 31 bits to play with. Thanks for the effor tho

 

Post a Comment

<< Home