Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Show HN: Constrained Chess, Play Stockfish with custom natural-language rules (constrainedchess.vercel.app)
2 points by vigrant 3 months ago | hide | past | favorite | 8 comments
Built this mostly with AI assistance (vibe-coded). Stockfish 16 runs in the browser via WASM. Constraints are parsed in two layers: a hand-written rule parser handles common patterns (no captures, piece restrictions, no castling, etc.), and anything it can't match falls through to Claude Haiku, which generates a JavaScript matches(uciMove, chess) function on the fly. That function runs client-side and filters the list of legal UCI moves before being passed to Stockfish's searchmoves parameter, so Stockfish's skill level, depth, and randomness all still apply correctly within the allowed set.

Constraints can target the engine, the player, or both. Shareable via URL (FEN + constraints + ELO encoded as query params).

Pre-loaded challenge if you want to skip setup:

https://constrainedchess.vercel.app/?skill=5&c=player%7Ccan%...



> That function runs client-side and filters the list of legal UCI moves before being passed to Stockfish's searchmoves parameter, so Stockfish's skill level, depth, and randomness all still apply correctly within the allowed set.

Hmmm... I can immediately think of a reason this won't work.

If I create a constraint that says "Bishops are only capable of moving 4 squares maximum", the set of valid moves you are passing into Stockfish only affects the initial move.

But as soon as it starts to build the minimax-equivalent search tree, it's no longer enforcing that constraint.

To make this work, you’d have to get Stockfish to continuously enforce the “bishop move” rule at every ply during its search.

Caveat - unless this is something you are enforcing in searchmoves? I don't have a lot of experience with in-depth usage of Stockfish.

Alternatively, you could look into Fairy-Stockfish, which allows you to define rule variants.

https://fairy-stockfish.github.io


The constraint is enforced at every move. Stockfish can never actually play against it. The only limitation is that its internal search tree imagines unconstrained future moves, which makes its evaluation slightly off and the engine effectively weaker. For the use case (handicapping for fun) that's an acceptable tradeoff

https://constrainedchess.vercel.app/?fen=2b1kb2%2F8%2F8%2F8%...


> The only limitation is that its internal search tree imagines unconstrained future moves.

Yes this is what I'm saying. Evaluation can be way more than just "slightly off" though and can make the evaluation MUCH weaker because it might go down "ply-routes" that aren't just suboptimal - they simply wouldn't work at all.

For example thinking it had a "move set" that lead to a checkmate but the final ply involves a bishop that is five squares away per my rule constraint mentioned earlier.

It's fine for fun but the statement that "Stockfish's skill level applies correctly" is not a true one at any moderate level of play. (above 1200 ELO).

That's why I suggested looking into Fairy which supports runtime rule variations.


So did some research....

Fairy would help with search tree accuracy but it's not expressive enough to cover everything, you can't encode "no captures for 5 moves" in an INI config. I'd still need the JS filtering layer for dynamic constraints, so you would end up in the same place with those cases.


Ah yeah that'll get complicated quickly. :( It'd be nice if stockfish or its variants exposed the "ply searcher" with some kind of a callback so you could quickly kill a ply based on your custom constraints.

When I was doing some research into rolling my own chess engine about 5 years ago I stumbled across this:

https://github.com/josefjadrny/js-chess-engine

But you'd have to modify its minimax search and now we're getting pretty deep down the rabbit hole. Either way, good luck!


Yup, you are right. I'll take a look at Fairy. Looks like there is a WASM version!


Nice, had no idea they'd ported it over to WASM. I've used the original Fairy to play Chinese chess in the past though and it worked great!


I agree, but Stockfish is playing at the 1.5×Magnus level. It's so good than in most cases it will win anyway.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: