How My Random Move Chess Bot (technically) Beat Stockfish

Kevin Ripper
5 min readApr 7, 2021

If you just want to see the game, it’s here:

1. f4 Nf6 2. Nh3 d5 3. c3 Bxh3 4. Kf2 Bg4 5. Ke1 Bf5 6. e4 Nxe4 7. d4 e6 8. Qc2 c6 9. Bd3 Bd6 10. Be2 a6 11. Be3 a5 12. Bc1 a4 13. Bc4 dxc4 14. Qxa4 Rxa4 15. g4 Bxg4 16. h4 O-O 17. Kf1 f5 18. Na3 Bxa3 19. Bd2 c5 20. bxa3 e5 21. fxe5 f4 22. Re1 f3 23. Rc1 f2 24. Kg2 cxd4 25. Rhf1 d3 26. Ra1 Qxh4 27. Rfe1 fxe1=Q 28. Bg5 Qxa1 29. Be3 Rxa3 30. Ba7 d2 31. Be3 Rf2+ 32. Bxf2 Nxf2 33. e6 d1=Q 34. e7 b6 35. e8=Q# *

And an animated gif from chess.com:

My bot opened with 1.f4 and won. Impressive.

If you are a high level player of chess, or have ever played more than a few games, you might think, “That was the worst game of chess I have ever seen”. They both played terrible, and my Random Move Bot (White) won by a complete fluke against Stockfish (Black). So that begets a few questions.

1. Why did Stockfish play so terrible?

Stockfish is generally the gold standard of chess engines at the moment. Leela had its moments against Stockfish a couple times in recent Top Chess Engine Championships, but Stockfish has won the past three of those. It’s a pretty phenomenal engine. Here’s a video of Stockfish vs Leela showing a mastery of chess humans will likely never play at:

So,

2. How was my Random Move Bot able to swindle such a powerful engine?

It wasn’t really able to beat full power Stockfish. In fact it got absolutely smashed by full power Stockfish.

Stockfish (Black) in a… not very close game against Random Move Bot (White)

So in order to beat Stockfish, my Random Move Bot had to be given several advantages.

To give some extra context, the bot is written in python, and I am using the Python-Chess Module for move validation, board creation, PGN writing, and some other background work. Here’s what the code looks like to have Stockfish choose a move with very few limitations:

computer_move = engine.play(board, chess.engine.Limit(time=10))

board.push(computer_move.move)

What the computer_move variable is doing is calling the engine.play class from Python Chess, and setting the limitations for Stockfish. The only limitation I included was forcing Stockfish to make a move after 10 seconds of thinking. Now, that isn’t very much time, but it’s enough to trounce a Random Move Bot, as well as the vast majority of humans who have plenty of time to think. So what needs to change in order to allow my Random Move Bot to win… sometimes?

Limit(time=.001, nodes=1, depth=1)

This means that Stockfish can look down one node, for one ply, for .001 seconds. A “node” is essentially how many game states Stockfish is looking at or evaluating. For example, If you’re playing chess with a friend and you imagine 3 or 4 potential responses to your move, you’re evaluating 3 or 4 nodes. a “ply” is a single turn from one player. You’ll see that the notation for the first game included is 1. f4 Nf6. f4 is therefore a ply, and Nf6 is also a ply. Those together constitute a single “move”. Strange, but chess is full of strange rules and terminology (Google en passant if you haven’t heard of it. You’ll see a flurry of accusations of “glitches” as well as an interesting quirk of chess). So coming back to you playing against your friend, if you also look 4 to 5 plies deep into those 3 or 4 nodes, you’d have nodes=3 or 4, depth =4 or 5. The way Stockfish is coded above, it’s only allowed to look at one possible response for one possible move.

The result of this? Stockfish is almost entirely incapable of checkmating. So Random Move Bot wins a lot right?

Not really.

Out of 1000 games simulated this way, my Random Move Bot won once. The 999 remaining games were all draws. In almost all those games Stockfish is able to win a bunch of material off of my Random Move Bot, but is incapable of looking for checkmate. So after it takes all the pieces it can see from Random Move Bot, it just arbitrarily moves its king around, unsure of what chess even is.

So is it even a victory for my Random Move Bot to win .1% of the games it played?

Yes. Absolutely.

3. Ok. But why? Why would you do this?

Great question! Most of us know the old oft repeated theorem, stick enough (immortal and incapable of being distracted) monkeys in a room with typewriters, and give them enough time, and they will eventually create the complete works of Shakespeare. But has anyone ever actually put monkeys in a room with typewriters? No, or at least I hope not. But I wanted to explore a possibility of a random perfect game.

Theoretically, if you don’t think too hard about it, it’s possible for a random move bot to play only the best possible moves. Stockfish, currently, only plays the best moves according to Stockfish. But imagine moves that Stockfish may be currently incapable of evaluating for one reason or another. But, through sheer luck, maybe Random Move Bot can find those moves.

If you google the possibility of this, you’ll probably be pretty discouraged. The number of possible games of chess that there are is a big number. Really big. The wikipedia page on the Shannon number seems to think the number of “sensible” games is around 10⁴⁰. Unfortunately, Random Move Bot does not care about “logic”, “reason”, or “sense”. It is beyond those human conceptions. It only cares about one thing, making a move, and it does not care at all what move that is.

So how long would it take for the Random Move Bot to beat Stockfish once, when Stockfish only has a time limitation of .01 second? I don’t know. Assuming a near infinite amount of computers with a near infinite amount of resources playing a near infinite amount of games every second, it’d probably still never ACTUALLY happen.

So how about if it runs on a mid range laptop, plays a game around one per 5 seconds, and has to be reset every few thousand games? I’d say it’s got a decent shot.

--

--

Kevin Ripper
0 Followers

Machine Learning Student. Currently encouraging bad chess bots to fight. They/Them