Coding Adventure: Making a Better Chess Bot

Trying to improve an old chess bot by experimenting with various interesting techniques.
You can play (or watch) the bot on lichess:
This is a sequel to:

If you’d like to support my work (and get early access to new videos and projects) you can become a patron of the channel over here:

Source code:

A really fun video about various algorithms for playing chess by @tom7:

Music and other credits:

Chapters:
00:00 Intro
00:38 Battle of the Bots
03:18 Maybe Don’t Throw Away the Best Move?
07:13 Transposition Troubles
10:55 Search Extensions
14:01 Refactoring and Recapping
15:51 Tweaking Kings and Pawns
19:35 Bitboards!
23:54 Passed Pawns (and more)
28:32 Magic Bitboards (minus the magic)
34:40 The Magical Part of Magic Bitboards
39:00 Testing and Optimizing Move Generation
41:50 Killers, Reductions, and Repetitions
45:56 Creating a Lichess Bot
49:30 Let’s Play!
54:54 Existential Crisis
55:02 The Bot’s First Game Online
56:12 Can Our Bot Beat Stockfish? (No)
56:59 Rating Speculation
59:28 Outro

405 Comments

  1. Even though I am no coding expert try to match it against stockfish, Idk if it is an AI that learns from its mistakes but you could try I guess. Try like to play against it like 100 games and Idk if thats possible but maybe there is like a tactics book so even though its an engine if theres a tactic he could see it instantly. Also you could try instead of stockfish leela chess zero Because they play different I guess.

  2. Hi Sebastian,

    I must say, your dedication and hard work really shone through this one. It was a fantastic video, and I really appreciate the effort you put into it.

    I'm reaching out to you because I have an exciting chess coding project in mind, and I believe you could be the one to bring it to life. I understand that after making this video you might want to take a break from chess projects, but I believe this new project would be a perfect fit for your coding expertise and the awesome thing is that as far as I am aware this is a completely novel idea.

    If you're interested in exploring this possibility further, I'd love to discuss the project in more detail. Please let me know your thoughts, and if you're open to the idea, I'm eager to collaborate with you on another exciting chess coding adventure.
    Thank you for considering my proposal, Sebastian. I genuinely appreciate your time and effort. Keep up the incredible work!

  3. create a new AI that does not know any chess openings it learns like alpha 0

  4. Yea lichess ratings are a bit inflated. An average player is definitely not 1500 ELO in real life. The bot is certainly destroying above average players tactically, but I assume it has problems in closed strategic positions, like all engines do.

  5. It’s pretty impressive that this bot is reaching IM to GM levels of play already with these basic optimizations.

  6. It's so nice to see someone using the latest C# features to write some performant code!

  7. 14:28 Petting my own bug hunter while I watch this video. We make a great team.

  8. This series inspired me to write my own chess engine. We need part 3 Sebastin!

  9. dude, i just had the worst/best idea ever, you should combine this project with the raytracing project and make a raytraced board with a handmade chess AI.

  10. Its really interesting how early improvements converted draws and losses to wins, but then you had a run of 4 improvements that only converted losses, and the best loss -> win conversion came from your last two improvements. i dunno what to take away from this, but it's still interesting!

  11. This is absolutely amazing! I haven't actually watched your first video (i'll make sure to do that now), but this video is so interesting as a chess player and programmer myself – I actually tried making a program similar to that recently but gave up on the idea. Perhaps I'll continue now with the new motivation I've obtained from this video and what I've learned. Thank you!

  12. i'm sure you've heard by now how amazing your work is. what i want to point out though, is how remarkable your patience is. Sticking through gigantic projects like these and keeping on through all the bugs and edge cases is an amazing feat!

  13. Did you see CPGGrey’s video on hexagon chess? Could be a fun challenge.

  14. Tip on searching for more magical magic numbers – look for sparsely populated random numbers, i.e. numbers with low bit counts. You can generate these easily by ANDing 2 or 3 numbers together, e.g.:

    uint64 random_sparse() {
    // Random sparsely populated 64 bit number
    return random_uint64() & random_uint64() & random_uint64();
    }

    With a little more searching you should be able to get your rook table down to about 705kB and your bishop table down to about 38.5kB.

    Also, don't bother searching for 8 bit bishop keys – you will never find lower than 9 bits for the 4 center squares (the margin of this post is too small to contain the marvelous mathematical proof of this!)

  15. 23:47 What do you mean? This is the far more entertaining video. Love it. Programming and chess.

  16. Didn't expect tom7's video to be mentioned here.

  17. You're the best youtuber ever! Your content is so peaceful, pleasing and intelligent! Thank you so much

  18. wdym apologies for chess part 3? i want that!

  19. This is the most engrossed I’ve been in an hour long video in my many years on this platform.👍

  20. Quick quis Sebastian
    Where did u learn c++ and blender and unreal engine
    Just curious

  21. 2:20 reminds me of the TCEC, why don't you allow unequal positions but make both engines play as black/white? If you have a better-attacking engine it'll be able to make use of small advantages on either side, but if you improve the defense capabilities that might not show up until it manages to draw a worse position.

  22. Did you put your engine on CCRL? Wondering what the rating would be.

  23. 26:54 instead of "calculating" all the time passedPawnMask could easily have been a contant array of 64 position… (and there are a lot of pawns in the board to be recalculated each move…)

  24. Everyone is talking about your great coding but how about your seriously good evaluation and understanding of the position starting around 50 mins! No one is talking about how accurately and quickly you were evaluating those positions.

  25. Makes more sense than en passant lol no it doesnt at least not unless this is pacman

  26. Interestingly, the bot’s rating has gone up around 400 points since this video (at least in Blitz) and now sits at nearly 2700

  27. litteraly the best instructive video i ve seen in a while….Thanks

  28. the ascii art blocks in the thumbnail are not 8 characters.

  29. id love to see all the ai versions scores against a weaker mode of stockfish to show how much they improved over generations compared to an exernal bot

  30. 18:30 you can treat wins:everything else as a binomial distribution, standard deviation is sqrt(N*(prob. of winning)*(prob. of not winning)); if you're more than two or three standard deviations off with the new version vs the old, you can say there's probably a change

  31. Hey Sebastian.. huge fan of your work.. can you please tell me how to start building your projects. Like the software requirements and the language you use. I am so inspired to code it on my own, it would be of great help if you could share some tools you use

  32. Awesome video. Very entertaining and informative at the same time. I thought 20 minutes have passed and then the whole video ended. Really enjoyed the explanation on every addition. Keep it up!!

  33. Chess programming is very interesting because there are so many different ways to make the engine. I like that two of the best engines, Stockfish and Leela Zero, are using different hardware for training and playing and different software as well.

  34. One thing I also appreciate about your videos is how smooth all the transitions are, from selecting text to highlighting… I can imagine it takes a long time to edit and put together the videos, thank you for that! 🙂 would you please share what tools you use for producing the video?

  35. How about tuning your heuristics tables with some optimization algorithm instead of guessing? 😁😉
    Or how about going really deep into hardware optimization? Maybe use some really cracked C# optimization features, it's fun 🤪

  36. sometimes moving the king can be unsafe even with few pieces left because it is easier to get mated. 23:00 is there the option to promote to rook/bishop/knight? (GothamChess has a good video about underpromotion) 29:45 would it be easier to check first if any pieces are on the file/rank the rook is looking to move to and if nothing is there (excluding enemies on the edge) you can move to any of the spaces but if a piece is there, then go one by one from the rook to see where the piece is?

  37. You lost me at the magic bitboards part. Why did you need to include all 4 directions making such a huge key and lookup table instead of handling them one by one and concatenating the possible moves?

  38. "I feel like this bullet rating might go down a bit with time" – 2 week's later and the bot's currently sitting at 2827. Yikes.

  39. You should release your geographical adventure game on steam!

  40. I wonder how you did to create this triangular background that moves at the start of your latest videos, could I have more information?

Leave a Reply

Your email address will not be published. Required fields are marked *