Coding Adventure: Chess AI

Sebastian Lague

388 Tsd. aufrufe15

    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:
    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

    Am Vor 20 Tage


    1. Sebastian Lague

      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:

      1. AnonTen

        have you been inspired by suckerpinch?

      2. Het Smiecht

        I believe it is Unity

      3. Douglas Parkinson

        im not joking, next time you find yourself with hours of free time, please record an audiobook

      4. Vinyl

        If you want to try out nnue evaluation (fast CPU NNs abusing SIMD) and need help you're always welcome on the Stockfish discord.

      5. Dionysis Nikolopoulos

        Man this was the mosta interesting youtube video I have seen in months. Please consider making a part 2!!!

    2. Edits Like Clockwork

      What programming language is this in?

    3. Jacob Sunny

      Epic. I want to make one

    4. DampoeCF

      Really awesome!

    5. Iiro Peltonen

      Really interesting stuff.

    6. hexerous.


    7. DanielGame


    8. Connor Dias

      couldn't the colour be 1 binary digit

    9. Ethan Maruyama

      This video deserves so much attention. I can really tell how much work you've put into this. Well done.

    10. cynicalsaint

      5:23 Not going to lie I was super interested in seeing how you handled some of the weirder moves like castling and en passant, and super sad that you just skipped over them.

    11. EricJon

      The queen vs pawn endgame was beautiful to see the computer perform. I’ve learned that and always screw it up somewhere but with your programming the computer found the best moves so fast. Great job. Now if only I could download this program into my brain. Part 2?

    12. Prashant Vishwakarma

      3:08 This is literally how I used to win games when I was 15 years old.

    13. Ernesto Delgato

      can someone please tell me what language this is...looks like c++ but i cant differentiate between c and c++

    14. PHOENIX

      its playing completely random moves: it then plays the scillian: me:-_-

    15. Oliver Krüger

      Hey make it play on lichess and find out what rating it gets. 900?

    16. Fab DuG

      Excellent video ! Thanks !

    17. Ahmad Sami

      can u tell me what ide u are using / what application please?

    18. Narro Chess

      OMG it's a great video, but I hope sit down and enjoy your coffee, don't update Stockfish anymore ... my head might explode haha #just kidding

    19. t

      Hey i’m just wondering what environment he uses and if it is c sharp or not thanks 🙏

    20. Elaphantykid

      skipping thop the end of this I actually thought you made lichess or something

    21. EmberDrops

      Mr. Lague: Developing astonishing code over several months, condensing the progress into a 30min video Me: 27:22

    22. Jeremy Ansel

      Amazing video! I wish I had the time to try this out myself. Two of my favorite things together :)

    23. Rising Sun

      Beautiful video. I play it in the background when I do coding.

    24. FTWGaming

      Now have it fight itself.

    25. Faisal A

      I sleep watching videos like this am I normal ?

    26. EmeraldRod

      This is pretty interesting!

    27. Amceg

      I really don't know how to program and this could be all made up, but It is an excelente chess video to understand the game and excelent logic keep up these videos!

    28. snowboardhusky

      You should make a full game Frome start to finish on your Chanel and try to make money off it

    29. Pixel

      What editor ur using pls?

    30. F


    31. Arslan N

      11:12 u found my game

    32. Buster Dafydd

      I looking at your code with a space before your open bracket why?

    33. Zuzana Suchá

      2:29 nf7+ kg8 qe8+ qf8 qxf8# ?

      1. Sebastian Lague


    34. Ivan the Benighted

      That was so fascinating, I loved the video.

      1. Sebastian Lague

        Happy to hear it!

    35. Tech Gamer

      Nice cat.. I mean job

    36. Sedrowow Chaos

      How do you make this with the coding? I meant by that the animation of what you write? Is this a specific program or an plugin for an program? And what is its name?

    37. Johan Röjas Söderman

      Now play it against Hikaru xD

    38. Zafer Sernikli

      Plays definitely better than me, I wonder what would the ELO rating of the bot would be.

    39. eraggo

      25:47 :D

    40. FrostZ

      After just going on DEfasts just to learn something new! I come across this! And after watching the video........ SUBSCRIBED, THIS CHANNEL IS GOLD!

    41. Xcyiterr

      5:39 cat takes e5, checkmate

    42. Noah Joseph

      what did you use to program this?

    43. Vincent Guerin

      I'm a senior-level programmer, and I lasted about 4 minutes before saying "forget this.." lol!

    44. Ian Timmis

      Where did you purchase that chess board?

    45. Luke Nukem

      [01:11] [01:33] - Hi I don't understand why you use an or | operator when the values equal the same as adding them together. This [] documentation offers zero explanation. I have tested and concluded that the integral value is chief, and you'll result in the same binary value by adding the const values. - 9 : 8 + 1 10 : 8 + 2 11 : 8 + 3 12 : 8 + 4 13 : 8 + 5 14 : 8 + 6 17 : 16 + 1 18 : 16 + 2 19 : 16 + 3 20 : 16 + 4 21 : 16 + 5 22 : 16 + 6 - Even with the C# doc, setting your value to (8 | 1) is odd compared to (8 + 1) and has the same result. The doc offers no explanation on how the alleged OR works in that 'addition' case. Why be confusing?

      1. Luke Nukem

        Instead of the result asking OR, it's still just ADDING.

    46. willl0014

      What language was that c#???

    47. Brad Rogers

      To make head spin when you code it less convert it to pixel bit mapping without high language

    48. Clown Clown

      What language is this?

    49. Justin Kassube

      So... Drawsquare()?

    50. Brad Rogers

      Now take the code and mix it to your planets to lower code that processes data

    51. Antonio Augusto Barbaro _ BRAZIL

      7:16 best game ever

    52. Pat B

      I'll never understand any of the coding, but man was this an amazing and funny video. How could someone be this good at both programming and designing videos?

    53. kipmeneer 5

      The cat surenderd

    54. Nishan Shrestha

      Make it better

    55. Lorin Semela

      Programming noob here. How is this an AI? He programs its evaluation of the position and how it finds the ‚best‘ move. Where does the algorithm learn? Thanks for your explanations!

    56. Andrew Lapides

      Hey Sebastian, watched this with interest. Coded my app in 2000, when Java was young. The most difficult rule was enpassant. Check your code for this: Black King on a4, White Rook on h4, black pawn on d4, white pawn on e2. White plays e2-e4. Black takes enpassaunt de. Which puts his King in check, hence illegal. Validate if your code allows this move.

      1. Andrew Lapides

        oh you have it at 11:40, so ignore my comment

    57. Oscar Franco OFK

      Nice Nice Nice . thx programing is creazy and cool.

    58. Leo Mikec

      What language is this

    59. Pacific Bird

      Sebastian's microphone is an Electrovoice RE-20 by the way. One of the easiest mics to identify by looks.

    60. PiecePaper

      im not a big fan of for for loops. i understand the principle why you chose to do so and are still reasonable because they are small functions. but later you will have a for for if if if nestet statement inside.

    61. jon ilo

      Next Coding Adventure : Tesseract?

    62. Pavpv V

      Hi there, really enjoyed the video. I'm really just a beginner when it comes to coding, so what programming language are you using?

      1. Sebastian Lague

        Hey, happy you liked it. I’m using C#

    63. Remi

      The CPU fan in my brain is working overtime trying understand this

    64. Brylle Ivan Bernardo

      What is your coding platform?

    65. Bryce Huey

      I’m over here coding python and he’s just like, Oh let’s make an AI for chess, which most humans can’t play

    66. Jakub Gania

      How you know that what you have to write wtf

    67. Paul Oketch

      Get the programing instructions , visible , pls.

    68. Thespourieye

      Man, I spent 30 minutes watching a dude with a cool accent coding a chess AI. And I fucking loved it. Amazing !

    69. Solodam

      11:00 That king + knight castle made me laugh hard

    70. Somnath Roy

      1 minute into the video and the reason behind your choice made me like and subscribe to your channel. (Sebastian Coding Adventures)

    71. Gaunt

      It's actually surprising to me that there are not more well produced chess engine videos, always seemed like a very interesting topic to me.

      1. Eetu Salli

        What sebastian has in terms of skill in programming, writing and presenting your thoughts outloud, and producing videos is a rare combination though. His videos are really, really well produced and written. Actually I know of no other programming youtuber with as high quality content.

    72. John Braddock

      Danya would be proud!

    73. FormalSpecialist

      Very good video, very insightful! Though, not sure what the clips with the cat were doing in this video other than being very distracting and taking away the professionalism of this video.

    74. CBMax

      2:28 knight to f7, king to g8, queen to e8, queen to f8, queen takes f8

    75. Paddy Spencer

      What ide is that

    76. First

      Do you have an estimate of its rating?

    77. Nate Pasion

      25:27 Ah yes, the prophet.

    78. tree music

      holy shit it's playing perfect theoretical endgames

    79. Saif Ul Islam

      10 minutes in, and I'm starting to fall in love again with programming and chess. This is beautiful.


      Can you please put a source in the description to the chess piece assets? I will really help

    81. Hopeless Dorito


    82. Ineedhelp101

      Yup, agreed 👍

    83. Lourenço Bispo

      3:08 when the king finally show his powers

    84. shado

      played against a computer. LOST. Me: Guess what, I'll make my own computer

    85. Mark B

      Great video. Would have been cool to compare it to different stockfish levels to see what rating it would be. Also I would like the idea, of an AI, which would make specific mistakes, which would prone specific skills of an player. Like having a bad king safety, leaving a piece unprotected, invite to an fork, ......

    86. Linton FOR

      someone's watched the queens gambit...

    87. vincent soriano


    88. Aryan Parekh

      Please upload part 4 for your neural network series :(

    89. Shreyas Bhat

      thanks for the great video ! I'm really in awe of your modesty and how well you explained difficult concepts while practicing good coding habits :D

    90. Karunamay Murmu

      Now I can hack the

    91. Friiqi

      deep! :D

    92. Isentaic

      I just found your channel and your videos are awesome. Looking forward for the next one!

    93. Neelabhro Ghosh

      ha! abandon c# return to C++, and you shall see far into the future

    94. Julio Zenelaj

      Coding Adventures suggestion: Underwater light ripples effect.

    95. Nybbl er

      Hey Sebastian, I want to make a game and I can do the code but I've always struggled with art. I can always see what I want to create in my mind but really struggle to put it into images, models, textures. Do you have any advice for starting with 3D modeling? Thank you, your videos are awesome and helpful.


      15:32 sebatian: promoting a pawn would be a good idea mE An InTeLlEcTuAl: 😏😏😜

    97. Vander Karl

      I was disappointed to see that making another Chess AI vid lost the poll. Looking forward to more of this!

    98. Dan E

      For the king check recognition, why not use "reverse raytracing" technique. The king can only ever be attacked from knight squares or any piece in 8 directions, and one could check if any of your pieces attempted to make an illegal move and putting the king in check.

    99. Penguin Boizz

      Hmm... How about tic tac toe AI?

    100. Jean Veron

      Is it in C ?