Coding Adventure: Chess AI
Video Statistics and Information
Channel: Sebastian Lague
Views: 766,450
Rating: 4.9768362 out of 5
Keywords: Artificial intelligence, alpha beta pruning, minimax, chess
Id: U4ogK0MIzqk
Channel Id: undefined
Length: 29min 21sec (1761 seconds)
Published: Fri Feb 12 2021
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.
What's a double pawn push?
Sebastian is a treasure!
His ideas give so much inspiration to programmers at all levels! He always captures so clearly the spirit of programming "just because". Its so refreshing -especially when feeling burned out at a programming-job.
Let the Coding Adventures never end!
Wow, this video is absolutely incredible. I've programmed a few basic chess engines and I wish this had existed when I started!
My take-aways:
- A common flaw with my engines has always been end-game strategy, and I was blown away by how effective 'nudging' his king towards the enemy king was to improve accuracy. My approach has been to increase search ply towards the end-game since there are less pieces on the board and thus less 'cost-per-ply', so to speak, but that approach would not have produced the same outcomes demonstrated in the video. Similarly, encouraging the AI to route the enemy king towards the edges is also a great technique that I (now in hindsight) can't believe I never thought of.
- At around 8 minutes, he talks about his first optimization being one to address valid move. generation, in particular weeding out moves that leave your king in check. He solved for this by keeping track of squares that the opponent's pieces attack and of course avoiding those with the king. I recall him mentioning this, but this is not sufficient as you can expose new lines of attack with moves that don't involve your king. I might be (probably am) missing something, but ultimately you still need to check at the end of each move whether your king is in check. One technique that I found more performant than iterating through each opposing piece and checking to see whether it can capture your king is to flip the script: check to see if your king can capture an opposing piece were it the same piece. So basically: search diagonals from your king for an opposing bishop/queen, search horizontally/vertically for rook/queen, and do the same for knights and pawns. This might be sub-optimal in the late game when your opponent has less pieces, but you can always implement both search functions and conditionally perform one based on the # of pieces each side has.
-------
I also loved the funny ui-bugs in the beginning. Some of my favorite bugs that I've encountered with my engines:
1) Initially, if it was able to find a forced-checkmate against it, the function to return its best move would essentially declare that there are no good moves since all moves result in an evaluation of -Infinity. This resulted in it simply not making any moves, or how I liked to imagine, it figuratively flipping the board and refusing to play any more.
2) Before I counted checkmates as Infinity, I just assigned the material value of a king as a number greater than all other pieces combined -- the idea being that the AI would prioritize 'capturing' it over all other directives. What happened then is that if there is a mate-in-one, but the AI could capture a piece without losing the forced-mate (within its search depth), it would go ahead and do that (since once it captures your king, the game is over and no more pieces to capture to boost. the evaluation). This often would repeat itself on the next move, and so on, until it captures all of your pieces, or capturing a piece somehow gives the player wiggle room to avoid checkmate on the following turn(s). As you can probably imagine, the result of this is a very rude chess AI that BMs its opponents by prolonging winning positions.
So yeah, an AI that refuses to play losing positions, and prolongs winnings positions -- I should have just claimed that it was by design to play like a bitter sibling.
Anyway, I highly recommend to anyone interested in chess and programming to give this a shot -- it's a very rewarding challenge and I can't describe the feeling of pride you get when playing against your AI and it makes a strong move. It's also very easy to tackle iteratively as it basically boils down to a handful of functions:
1) Generate valid move list, given a board
2) Evaluate the strength of a board, given a board and color
3) Find the best move, given a board and color
Each function can be written in so many different ways, and can be written as pure functions making it easy to experiment with significantly different approaches without rewriting other parts of your codebase.
-----
Finally, in case anyone is interested, here are some repos:
JS: https://github.com/robtaussig/ChaosChess/tree/master/src/engine
Rust: https://github.com/robtaussig/rust-chess
The Rust version I wrote as a means to learn Rust. I honestly have no idea if the code is good, but it compiles lol.
High quality content. I consider myself familiar with most of the ideas presented here and still consistently was learning new things throughout the video.
yay Sebastian lague
[removed]
I love how the AI that makes random moves opened up with the Sicilian!
Don't forget to register for TCEC, the chess engine tournament. It's in divisions, so you can see how far you can get. A Visual Basic engin got promoted a few times (but to be fair, it was compiled VB).
Oh shit! Another video from Sebastian! I love this guy!