2022/08/07

tic-tac-toe bitboards

While checking out the source code for the Game of Life implementation in cosmopolitan libc I was introduced to the concept of bitboards. I was aware of using bit-level logic for performance and memory reasons, but I did not know that there was a specific name for applying this to games. The cosmopolitan source calls out Bitboard Methods for Games by Cameron Browne, which is a great resource. It is clear to me how this approach can be used to implement Conway's Game of Life and other grid based games. The obvious way to improve such systems is to parallelize the computation across the board. I've gone the GPU accelerated route before but it never occurred to me that a similar result can be accomplished using bit-wise operations.

As an exercise in understanding, I wanted to implement something not explicitly called out in the above paper. I went with tic-tac-toe, since it is simple enough to do in short amount of time. I implemented something in C, and I am happy with how it went. I am not thrilled with the board printing logic, it feels like there should be an easier way to map an int to a string of output without using a mask for each bit. I'm sure that I could do away with the usage of pointers and replace some of the conditionals, but I don't know how much is left to be gained there. While not groundbreaking, this was interesting and I would like to look at applying this to more complex problems starting with cellular automata.

When I first started programming, I remember implementing tic-tac-toe in Java. I used 2D arrays (tic-tac-toe is probably a great introduction to this idea, but is far from optimal) and had plenty of loops to check win conditions. The input handling was janky and the output was a mess. I spent a lot of time writing logic to make the class general, which was definitely a waste of time. This seems to be a common issue, especially in Object Oriented code, but that is a topic for another day. While it is nothing fancy, I know myself from Tenth grade would think that this is cool.