I Hacked Stockfish’s Code To Learn How It Thinks (Part 1)

What is FEN?

👉🏽This will take you from 1000 to 1500 📈
🎓 🎓 Reviews: Overwhelmingly Positive (32 reviews)

✅ My Courses! ✅

The chess set behind me:
The Pieces:
The Board:

👕 New Shirts! 👕

☑️ Play Chess Here:

☑️ Support Nelson Here:

Links are affiliate links and help support the Chess Vibes channel via a commission.


  1. HAHAHAHAHA i love the duck chess board in the background

  2. Benchmark is self explanatory. But the 'moves' part of FEN actually defines the position. For example, you can reach an opening position by inputting '*start pos FEN* moves *all moves*' and UCI would translate that as the opening position itself.

  3. StockFish’s evaluation function has been replaced with the efficiently updatable neural network (NNUE backwards), that roughly halved the number of moves/sec it could consider, but the better evaluation counters that. All those carefully tuned “bitboard” tables about how many points to give pieces on different squares depending on phase of the game are now irrelevant. But if you look at Leela-0 you’ll see that its doing 100k nodes/sec vs StockFish’s 5M nodes/sec. That’s the critical difference: LC0 looks at way fewer moves, but has a much better understanding of the board. But I’d argue it’s still not a human level of understanding, as clearly no human evaluates 100k moves/sec.

  4. I didn't think you'd actually go through the code

  5. Try searching for the comments. I mean they put 5 man position mate nd4 thing as comments. If searching comments are possible then why not

  6. I tried the same thing before.it's just weird😵‍💫

  7. 'push_back' is a method of std::vector (see vector<string> fens, list; on line 113) that appends the string to the end.

  8. These positions have absolutely no significance whatsoever. A benchmark is a piece of code that runs another piece of code, repeatedly and probably with different parameters, to test that code's performance. You just dug through their test-/benchmark cases and kept asking yourself what the significance of these positions is – there is none. You could maybe have learned something by looking at the actual benchmark code instead of the input data. But if you really want to learn how a piece of software works, you're usually better off looking at test code than benchmark code.

  9. now we see it

    stockfish is NOT a chess engine

    actually, it is an alien intelligence, sent to conquer this planet

  10. Github is bad for browsing. Apart from the file name you have no clue what it is all about. The text besides a file name is the description of the change the file is lastly changed. This change could be unsubstantial. Browsing is only useful for people thoroughly familiar with the code and knowby heart the purpose of each file.

  11. That file looks like it's for checking the engine can correctly generate all legal moves in weird but known positions, ones that commonly trip up devs when creating an engine
    eg. the position at 6:00 is for testing the combination of checks, double pawn moves and en passant
    White can play e4, then normally black should be able to take it en passant with fxe3, but then that would put black into check so it's illegal

  12. There is a bug in github search. fens variable used later in the same file to generate a list of commands to run benchmarks. I doubt that these positions are used inside of Stockfish for evaluating next steps, it's just random position that stockfish is using for measuring performance of the Stockfish.

  13. i didnt knew tuna can ride horses like that these positions were for sure a thing

  14. Looks like you could use some help from someone who knows C++ pretty well. 🙂

    You hit maybe the least interesting file when it comes to understanding what stockfish does. All it does is performance testing.

    Also GitHub search sucks since it sometimes misses things. In this case fens is used in a for loop at line 156. The loop does only a bit of processing to construct a new list of commands to be processed by the caller.

    Edit: material.cpp looks interesting, I suggest looking at it first.

  15. Because I'm a C++ junkie, I decided to look up Stockfish's code for the fun of it.

    What happens with "fens" is that it's just a temporary variable used inside the function "setup_bench()". This function returns a list of fen positions that gets used in a file called "uci.cpp". What this particular function seems to be doing is getting a list of fen positions that can be sourced from an external file, or use the default positions (hence, "Defaults") from Stockfish's own source code.

  16. @chessvibes these positions better be a puzzle video in the near future so I can pretend to know the solutions.

  17. Haha! "Why is it storing the starting position?" Where else to start from in a standard game??

  18. why dont we just learn the same stuff stockfish knows

  19. My name is Roberta and I'm with your sister right now driving to ashville!

  20. So a benchmark test is usually for testing how quickly the program can calculate something. I wouldn't expect those test cases to have much significance, but they could include edge cases that will exercise unusual code paths.

  21. Feels so weird to have you go over Stockfish's Github page. Never expected to see this day.

  22. Those benchmark positions are just random positions for running benchmarks to test the program, lol. There's nothing special about them. The reason it stored that "bad" move is just to test the UCI command position fen <fen_code> moves <moves>. It's just for testing.

  23. You literally spent the entire video discussing random positions meant for testing.

  24. Not sure I would call any of this hacking. Pretty much its intended use…

  25. Those endgames are quite interesting and definitely deserve a video. The two knights one is not too complicated, and all the key themes can be explained in 1 video. I would probably start with the longest checkmate which is over 100 moves I believe. It transposes into the position that you gave.

  26. Is the FEN positions things that the direct code of stockfish cant compute and figure out fully by itself but the positions were known and figured out by people already?

  27. Wait, is stockfish stealing our data on chess? 😡💀

  28. I want to point out that "syzygy" is a word and should be pronounced as one, rather than the individual letters. It's meaning is an alignment between three celestial objects.

  29. As others have said, I imagine that the positions are there for some sort of testing. They're not positions for which Stockfish has a pre-programmed answer, instead they can be used to see how well the program is working.

    You seemed confused commentating on two of the positions. At 15:50 you seemed surprised that the black king was being forced towards its pawn. If white can't win with a king and two knights against a bare king, then it can't win by having one knight block a pawn and the other knight and king dance around the black king in an unrelated part of the board. You need the black king close to the black pawn to provide some sort of blocking that the white knights can't do alone.

    Also you seemed puzzled that the three bishops at 17:27 was a draw. The point is that the bishops are all on black squares. If black keeps his king on white squares then white can never put him in check. As long as he keeps his king towards the middle of the board (so that the white king can't block off all the white squares) he can keep going forever.

  30. Nelson, you always have very interesting content. I use a modified stockfish 13 I made to help me analyze my own games. I modified it to have increased valuation on mobility/space and opponent king safety. It doesn't play better than the original, but it plays a more sacrificial, aggressive game that I'd argue is more anti-human. I'd rather learn lines which gain initiative and aren't as deep. Moves which only make sense 10 moves later don't really teach me in any meaningful way.
    I made another stockfish 13 which only plays hardcoded gambit openings and accounts for permutations, as well as shifting to a different compatible gambit if possible. This helped me train my openings a bit.
    I'm working on another one which has a ridiculous amount of hardcoded trap positions it will always accept(made in a way somewhat similar to that Defaults list you were looking at). Stockfish won't otherwise play dubious anti-human traps which are actually effective against human opponents around my level.

    I have an insanely tough crowd at my chess club who have all been playing 20+ years, so the only way I'm able to win is through taking them out of their comfortable lines with gambits, keeping initiative, and the occasional traps they still fall for. Plus it makes for some really fun games. I've only been playing for a few years so winning makes feel like a swindler sometimes, but you gotta adapt and overcome.

    Starting with a very simple chess engine like Sunfish is a good idea if you want to understand some of the foundational ideas without going too deep.

  31. This was really fascinating, quite interesting getting a better idea of how stockfish works.

  32. Tbh the game in the intro was pretty obvious to sac your knight

  33. Umm…but I thought Alpha Zero was the best engine in chess…

  34. well there is a bot that beat stockfish15 ask GothamChess or Levy

  35. he opens up a random text file that isnt even code for stockfish

  36. Looking you be amazed at everything while having no idea about programming is fun to watch, but your title is widely misleading.

  37. I found that "Bench is a number that is basically "number of total nodes searched that is needed to calculate this positions to given bench with given hash". Default parameters are 64 MB hash and 13 depth."

  38. new series – stockfish puzzles

Leave a Reply

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