I wrote my first chess engine, which you can find on my GitHub.

I will be writing an updated version soon, but as a placeholder, I will note that writing a chess engine teaches you a treasure trove of information, especially if you are aiming to write a fast one. I will give a small preview of things I have learned and plan to learn:

  • I better learned to program in C++, specifically about minimizing my memory and performance footprints.
  • Bit manipulation is often introduced as an ancient technique with little use for modern programs. To maximize performance for a chess move generator, make using of bitwise representations of chessboards is crucial, often dropping memory usage by up to 1 or 2 orders of magnitude. The bitwise operations that are actually employed to achieve these speedups are also nontrivial and end up very interesting.
    • One approach to calculating moves is called the “magic bitboards” approach and further introduces you to generated certain bitwise values by making use of non-deterministically generated perfect hashes.
  • On the note of dropping memory usage, it should also teach you about cache and the fact that programs which don’t use much memory can fit in cache, and therefore execute much faster.
  • I generally learned about some language features which although are useful, may cause slowdowns in extremely high performance code.
  • Disciplined programming is difficult to learn, so writing a chess engine serves as a good exercise because of the massive amount of edge cases which you may encounter.
  • Modern chess engines tend to use basic neural networks to evaluate positions, so it serves as a small entry into the field of AI as well.
  • Searching for the best move to be played at a given point in time is typically done using the minimax algorithm with alpha beta pruning based on the results of the neural network’s evaluation of searched positions.