Coding Adventure: Chess AI

Sebastian Lague
My attempt at creating a little chess playing program!
Think you can beat it? Give it a go over here:

If you’d like to support the creation of more videos (and get early access to new content), I’d greatly appreciate the support here:

Project Source:

Learning Resources:

If you’re interested in a video about some weird and interesting chess strategies, I highly recommend taking a look at this video by suckerpinch (and all other videos on that channel too!)

00:00 The Board and Pieces
03:20 Generating Moves
06:54 A Random Adversary
07:52 Optimization and Testing
12:11 Search and Evaluation
17:43 Easy Endgames
20:00 The Transposition Table
23:03 Openings
25:09 Game One
26:06 Game Two
27:01 Game Three
28:05 Game Four

Devoted Mind by Wild Colors
Intuition by Lincoln Davis
Wonderland by Shimmer
Selfless by Eleven Tales
Floating Point by Roie Shpigler
Nobility by Wicked Cinema
A Quiet Place by Jordan White
Air by Assaf Ayalon
Heart Wide Open by Sounds Like Sander
Thoughts by Anbr
Deep Blue Sea by Sivan Talmor
Flight of the Inner Bird by Sivan Talmor
Kings and Queens by Wicked Cinema


  1. Just posting random things on my channel says:

    can I ask what you used for coding, I'm new, can you recommend what to use?

  2. i dont know what your saying magic man but i like the results

  3. 11:54 I think it’s time to start working on something a little more challenging.
    cats jumps in keyboard oh no! Not that challenging, down genius coder kitty!! Down!

  4. As a beginner looking him code like this , I am jealous, excited, depressed and probably a lot of feelings…
    Just how did you start your coding journey and how much experience do you have ?

  5. Loved the Danya shout out. Then immediately gets smother mated lol

  6. 3:22 are we all gonna ignore the fact that he started conting on 0?

  7. solution to the puzzle at 2:28 1. Nf7+ Kg8 2. Qe8+ Qf8 3. Qxf8#

  8. I've long felt like Chess should have some chance to it. Pawn vs Pawn – slightly favors the attacker. Pawn vs Rook – not looking good for the pawn – either attacking or defending, but who knows… the battle isn't always to the strong.

  9. That cat is a grandmaster, checkmating the king without even making a move, brilliant.

  10. coding is the one science I find worse than math, programming just absorbs everything of the programmer leaving only an empty shell,,
    ..which is partly the reason it's intriguing

  11. why use 2 bits to represent the color of the pieces?

  12. A simplier and faster way to deal with checks and pins: For every move someone makes, let the computer calculate if their king is on an from the enemy attacked square after the move. If it is, the move is illegal. This would spare you from tracking pinned pieces and therefore would speed up your code a tiny bit.

    Edit: That's what I thought at first but after a bit more thinking: When you want to move and take a piece, you show all legal moves of that piece by painting the squares red. So instead of you making the move and then the computer saying "No", you actually calculate every move for every piece beforehand to show the player the legal ones. By tracking pinned pieces you can take a shortcut and say "You don't need to calculate every possible move for that piece, it's pinned, it can't move right now" and save resources, therefore time. This shortcut is probably what caused the bug at 11:15.
    This was probably too complicated to explain to a general audience so you left that out of the video. I'm still really proud I was able to figure that out, I'm only at my first semester of my informatics study.
    Also hats off to you for going the extra mile of making more code to make your version more chess beginner friendly.

  13. Most amazing thing to me was how the AI suddenly became really good at simple endgames as soon as you added that endgame tweak.

  14. 7:01 somehow, the first 2 moves of the random engine were somewhat good. it's just that the 3rd one wasn't. but then, if you get unlucky enough, it could have defended the mate, and then played all the best moves and win/draw.

  15. there is nothing funnier then being able to castle with an enemy knight

  16. Why did you set colour using 2 bits, when one bit would suffice. Guess I;ll keep watching, maybe there's a reason…..

  17. Lowercase k for BLACK king and uppercase K for WHITE – that's racist!

  18. what if world start to exist by the rules rather and we are like a figures, im thinking:)

  19. 27:50 yes the explanation made sense and it's fascinating, I still can't believe so much optimization comes from the extra pruning

  20. You had me at: “I’m rapidly losing faith in my ability to code anything”.

  21. finally, we see the developer of this game,

    please buff the kings they are totally useless and can't do shit without other pieces!
    and also there is should equality among colours, totally unfair that white always start.
    should be 5050 chance

  22. That binary encoding of pieces has just solved an issue I was stuck on for months with my masters project.


  23. I became curious: what are the implications of using CC BY-SA graphics in a YouTube video? 🤔

  24. God damn, this guy just appeared in my recommendation page at 12am
    Ended up watching 15 minutes of coding I don't even understand but they sounded cool

  25. How would you code the DrawSquare method?

  26. How did you design the chess GUI, because I'm having some real difficulty starting from scratch and trying to build a chess application from UWP, WPF or Java Swing. Thanks.

  27. Interesting is: playing random, computer choosed Scilian Defense 😂😂😂😂

  28. @11:50 "I think its time to start working on a more challenging opponent." A WILD CAT APPEARS!

  29. I don't even know what programming language you are using, but still I somehow understand.

  30. a lot of your personalty came threw in this video

  31. Hey everyone, hope you enjoy the video! Quick note – I noticed some slightly distracting compression issues after uploading, where squares of the chess board would sometimes blur together and flicker a bit. The only solution I could find was upscaling to 4k, so if you have the bandwidth I'd recommend watching in 1440 or 2160p.

    By the way if you'd like to play against the AI, you can find downloads here:
    And source code for the project is over here:

Leave a Reply

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