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. Cant tell you how much i love your vids. Its the comedy – pure original

  2. You have such a lovely voice man

  3. I think a good way to see if a bot is good is if its eval is similar to other bots and if its ideas are similar to other stronger bots

  4. How long did the coding to this video take?

  5. Thanks for the video, once again learned something new

  6. Nice video! I really have begun to wonder now how dificult it would be to make a self learning chess bot that maybe could learn from games of stockfish and the way it analyses them. If i ever get to program (its very cool gut looks extremely hard) maybe i will try this idea. Also, if you are looking for ideas for q future coding adventure, matbe you'd like trying to make some grass, since it looks so difficult. Really entertaining video, keep it up!

  7. Coefs must smoothly changes during game. Not split to debut, middle,endspiel

  8. Load all coefs to NN and take better solution

  9. Make multithread and this increases depth and level

  10. would be interesting to see a chess engine with few to none tunable parameters, like arbitrary numbers that "just work", how bout a fully or almost fully dynamic evaluation function? Then texel tuning or neural networks wouldn't have taken over top engines and turned evaluation functions into black boxes with uninterpretable magic behind them.

  11. We'd all love to see a third video!! And beating your version one that badly wow!!! Amazing improvements on your end

  12. please can you make a tutorial on "how do you program a game, on which language, and what to install"
    (just subscribed)

  13. Blame on you, Sebastian! I went down the rabbit hole of a chess engine from scratch of my own. I made bit boards , alphabeta search, nn evaluation, Montecarlo tree search and many other micro elements retrieved here and there as ideas. A wrote the code from zero 3 times, changing programming language as well as approaches. After two months I am dreaming of bishops moving only on diagonal bits (don’t know, in the dream a “diagonal bit” has totally sense). In fact it has been a huge journey and I am happy I had the stamina to overcome the several difficulties and the final result is definitely mediocre but totally mine and this is enough. You started the interest in the topic I had since I was a child as a personal programming challenge. Very nice job pal. Thanks a lot

  14. You can give the font letter using in vscode?

  15. I think the tom7 video just used one 64bit number for the whole board. Then he wrote a function Int64 -> Int64

  16. 32:40+
    How is a lookup from such gigantic table quicker than just doing a few iterations 4 times per rook? Is modern memory really THIS fast?
    Edit. Okay I'm 6 minutes forward and I understand nothing except that it does now some math instead of lookup.

  17. Thank you for recommending that 30 chess bots suckerpinch video! It's really great for people who enjoy similar content and one of the most under-viewed videos on youtube. These two chess bot videos of yours have been the only things to scratch that same lovely itch for me, so thank you even more for making such amazing videos like these.

  18. Really cool video 😀
    How much chess do you play yourself? Do you know your personal rating (i.e. lichess etc.)?

  19. I'm just here for the cats. Okay, I'm kidding. I would be fascinated to see an attempt at a chess bot using neural networks, like in your recent video on them. Such a bot would probably play quite poorly, but would be a lot of fun.

  20. Your videos on chess engine inspired me to write my own, and now it is accepted on CCRL, its elo is currently 2300, but I hope I can learn more from you to improve mine

  21. He is way beyond my coding skills and I love how i do not understand

  22. Chess computers would be very very fast as a ternary computer instead of as a binary computer. Hell, everything would be very fast, but chess computers would perhaps benefit the most. However, outside of Samsung, most people seem too scared of a third number to actually do things with it.
    The circuitry for a magic bitboard for the horizontal row of a rook would be able to be done as 10121101 -> 1012 1101 -> 2101 1101 -> 2100 1100 -> 00121100 COMPLETE. That's 3 operations and a simple turing machine with no need for a loop check. The final operation before everything is put together is just seeing the 0 and repeating the 0 until the end of the sequence/ A downside, though, is that a chess board wouldn't be perfectly fitting within trytes but I guess you could use the extra trit to just put a piece-specific sign operator which could be used with further thought for some optimization at the cost of a slightly higher file size.

  23. I've watched this video a few times and read around it and I still don't understand magic bitboards 🙁

  24. It pains me to my very core that I can only give this video 1 thumbs up!

  25. Great video, felt like 20 minutes – not like an hour! 😀

  26. I would love to know how you take notes, you went through a lot of articles and videos about chess and chess engine and still managed to remember all the things you needed to fix with your engine "our Engine" It's really fascinating how you where able to fix minor issues to make it stand up against Stockfish 15 that took a team of engineers and programmers years to develop to make it the strongest engine in the world, thank you for the animation and the information, we all love data and numbers to understand things, and your graphs mixed with numbers.. I get closer to the screen every time you were comparing to the previous bot or version1

  27. Your original chess video inspired me to make my own chess application in javascript back in february

  28. This is outstanding. I don't really know how to play chess…well…I mean I know the rules and how the pieces move, but how to actually play it decently WELL, not so much. Coding it this way, how you calculate moves, and store the info efficiently is an incredibly good lesson in not just how to play chess from a nerd's perspective but also solid coding approaches and practices. Massive kudos!

  29. A easy stat you should do is a Student's t-test. You should run each test several times (maybe split the test games into batches), then compute the mean's and standard deviation and also do a t-test. That should give you a p-value indicating whether each version has an improve performance. Something to consider is whether the chess engine has any stochasticity for a given start – will the engine always converge on the same solution? I would guess not since you are running for a set amount of time, which will result in different levels of performance depending on current CPU, cache misses, etc..

  30. One thing that I would probably do is instead of limiting the algorithm’s search by time, you limit it by actions, accesses or evaluations, this would prevent outside influences from making the program look bad, like for example the operating system is downloading something, causing the algorithm to run slower

  31. That's the problem with creating bots, no matter if you win or lose against it you feel disappointed

  32. I have a problem in my code (btw i am using python) I have a saved .txt file that has 7118 lines. in every line are: startsquare, blockerbitboard and legalmovebitboard. problem is; if i try reading the right value, it takes far too long! (acually 0.09s but thats too slow to make a bot). what should I do? should i use a list? should i somehow precalculate the line where the given data is stored?

  33. Can we please get a video on chess engine using reinforcement learning

  34. You could save a lot of cache lines by splitting the rook lookups into rank moves + file moves, since the LUT is the same for every rank and file modulo a single shift op. One multiplication and shift to pack the bits along one file into a single byte, one shift to right-align the bits on a single rank, then two lookups or'd together after the correct shifts. I believe that with certain SSE intrinsics it becomes even faster and you could use one table for both ranks and files.

  35. Hello Guys in the Comments and Maybe Sebastian,
    I have a Problem in my concept of the Dictionary for the Blockers:
    The Problem is that I am currently creating the Dictionary EVERY TIME when starting the program, this takes about 14 seconds everytime. I could copy the Dictionary into a .txt but that would take some time reading too. What should I do?
    I am using Python btw.
    Here's a Google Drive Folder with my progress: https://drive.google.com/drive/folders/13vJV71ziqdtjQEp_Nhr0aJVRpZhLtmyB?usp=drive_link

  36. These videos are so god damn good, please do part 3

  37. Okay. Cool and impressing. Explain NetHack then.

  38. Have you considered trying to implement Monte Carlo Tree Search into your chess bot? It should up the strength by a bit if you do it after min-max.

  39. Just played it on Lichess. Now I'm adopted by your bot.

  40. watching this as 'research' for my dissertation. I've been using problem reductions to solve a Rubik's cube, by reducing the number of sides still needed to finish a solve. I've represented each cube as a series of six integers (bitboards!), and my inspiration was this video. Thank you very much!

    I have it working using my own string representation of the states, not quite there for recognising the set of 4 moves, or the set of two. I'll get there!

Leave a Reply

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