Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

A Tiny Chess Engine

A Delightfully Strong Nemises

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.

Gif Missing :(

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 repetitions 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.