The gears of war

About

This is version of the board game Pentago. It is notable to me because it is the first, (and hopefully the last) time I try to write a bitboard implementation in javascript using javascript’s float type, which is insane!

Floats in javascript give you 53 bits of precision - however, you can only do bitwise operations on 32 bits at a time. This meant writing helper functions for all the common bitwise operations, for example this xor function:

function xor(x, y) {
    return ((x/0x100000000) ^ (y/0x100000000)) * 0x100000000 + ((x^y)>>>0);    
}

While this ended up working, it’s rarely a good sign when you’re constantly amazed that your own implementation actually works, and is a complete nightmare to debug. Since then I’ve had some success in switching over to using javascript typed arrays for my javascript bitboard needs.

#More Madness But wait - there’s more… Usually it’s convenient to layout the bitboard so that it’s convenient to map a [row, column] position to bit position. However, to make it easier to rotate the quad boards, it is arranged thusly:

/* (Comment taken from the Board class) */
Bit positions:
0 | 1 | 2   ||  9 |10 |11           Quads:
7 | 8 | 3   ||  16|17 |12           Q0 | Q1
6 | 5 | 4   ||  15|14 |13           --------
_________________________           Q2 | Q3
-------------------------
18|19 |20   ||  27|28 |29
25|26 |21   ||  34|35 |30
24|23 |22   ||  33|32 |31

This allowed for quads to be rotated using a bitshift operation.