Regex Chess: A 2-ply minimax chess engine in 84,688 regular expressions

nicholas.carlini.com

126 points by surprisetalk 5 days ago


zelphirkalt - an hour ago

It would be different, if somehow all those 84688 regexes were coded by hand. Then it would be a piece of art.

It would be different, if the number of regexes was maybe below 300, and it still plays acceptably. The sheer number of regexes kind of defeats the purpose.

At that code size, a much better engine can be written, or other kind of code for an engine be generated. Regexes themselves are not really something we should strive to use more either. Maybe its intentional badness kind of makes it art?

Kaliboy - 8 hours ago

This is amazing. I'm at loss for words.

During my CS years I remember being fascinated by NFA's, as opposed to boring single universe DFA's.

For some reason I internalized that I would never see something like an NFA implemented beyond text books.

Then came Carlini.

LukaD - an hour ago

This is delightfully insane! I don't think I would say it doesn't play _entirely_ terrible though ;) It's playing really bad, but it could be worse and it's already super impressive that it can even generate legal moves.

strenholme - 6 hours ago

For people who are interested, here is the solution. In standard PGN, the solution is:

1. e4 e5 2. Nf3 Nf6 3. Nxe5 Nxe4 4. Qe2 Nxd2 5. Nc6+ Ne4 6. Nxd8 Kxd8 7. Qxe4 a6 8. Bg5+ Be7 9. Qxe7#

In the Stockfish notation this engine uses, White’s moves are:

1. e2e4 2. g1f3 3. f3e5 4. d1e2 5. e5c6 6. c6d8 7. e2e4 8. c1g5 9. e4e7

Here is a Lichess analysis of this game:

https://lichess.org/WnMF3LpX

(In terms of Regexes, Javascript has a very rich Turing complete Regex library; it’s an open question whether Lua 5.1’s regexes are Turing complete, but they are good enough for the text processing I do)

evilsnoopi3 - 8 hours ago

The technical write up is worth perusing but I played a game before reading and accidentally found a winning strategy immediately. I'm not sure if this is a result of the 2-ply nature of the engine or if the mentioned deficiencies account for this but the computer did not act to prevent checkmate in 1 (without any intervening check); the game I played was (in algebraic notation): 1. e4 e5 2. kf3 kf6 3. kxe5 kxe4 4. d4 kxf2 5. Kxf2 a5 6. Qf3 b5?? 7. Qxf7 1-0

VladVladikoff - 8 hours ago

This is like a fever dream.

userbinator - 7 hours ago

Upon reading the title, this is one of those "I know that's possible, but I'd never bother to implement it" things, although this particular implementation isn't exactly what I had in mind.

dtj1123 - 4 hours ago

Brilliant. The Chinese room thought experiment as a chess engine.

- 7 hours ago
[deleted]
explodes - 8 hours ago

2025

asplake - 6 hours ago

And now you have 84,689 problems

neuroelectron - 5 hours ago

Previously posted: https://news.ycombinator.com/item?id=42619652

carlsborg - 3 hours ago

"Memory plus search is all you need"

devanshp - 6 hours ago

This is absurd. I did not realize you could do nearly this much computation in regex.

casey2 - 3 hours ago

Alternate title:

Compiling Python to a Branch-Free SIMD Virtual Machine via Extended Regular Expression String Rewriting