A Tiny Chess Engine

A Delightfully Strong Nemises
read in ~2 minutes | Jul 29th 2023

I’m planning on participating in SebLague’s Chess-Challenge (C#)

I’ve been writing a chess engine for this challenge for the past few days, and I’m pretty happy with what I’ve been able to do.

Video Proof

What I’m doing:

I’ve written a recursive min-max algorithm including alpha-beta pruning and board value caching.

I search all possible moves, ignoring moves that opponents/I will never take. Anytime I calculate a board value, I store that value for future use.

I’m using Sebastian Lague’s Board C# object to grab bitboards of the current position for each type of piece and color. I use this information to quickly evaluate the board state and get a “value” which we can look for with our min-max search.

I also have some code that mitigates losses from time-outs. Ideally, we wouldn’t run out of time, but if we start to run low, I start to make my chess bot make very quick judgements only about 2 layers deep.

There was a bug that I had with the caching where 3-fold repititions became super common because I was using cached values of board states. When I returned: to a previous position in the search, it would show the old non-draw state instead of the new one, causing my algorithm to think that it could move there without a draw, which would cause a sad sudden drop in advantage, costing some games.

I should probably mention that there are very fast ways for counting 256 bit longs, which I am using. It’s from a stack overflow post I’ve lost track of, can’t seem to find it, but it mentioned hamming weights and fast ways to calculate them. More Info About Hamming Weights. This greatly reduces the number of steps in some of the most performance critical sections of my code.

If you want to take a look at my code: MyBot.cs

Have a great rest of your day, thanks for being curious.


🪧 Enjoy Reading This?

Here are some more you might like to read next:

Developing a PC + VR game with Godot

A long Journey Jun 1st 2023


Programming A Chess AI

A Fun Self Challenge Nov 21st 2022


Baking Configs

I can't stop changing my configs (help me) Apr 23rd 2024



💬 Looking for comments?

I don't have comments on this site as they can be time consuming to manage. I'd rather concentrate on programming cool and new things.

Since you can't leave a comment, feel free to ✉️ contact me instead.