6 ms·
Show HN: Sameshi – a ~1200 Elo chess engine that fits within 2KB
I made a chess engine today, and made it fit within 2KB.
I used a variant of MinMax called Negamax, with alpha beta pruning. For the board representation I have used a 120-cell "mailbox". I managed to squeeze in checkmate/stalemate in there, after trimming out some edge cases.
I am a great fan of demoscene (computer art subculture) since middle school, and hence it was a ritual i had to perform.
For estimating the Elo, I measured 240 automated games against Stockfish Elo levels (1320 to 1600) under fixed depth-5 and some constrained rules, using equal color distribution.
Then converted pooled win/draw/loss scores to Elo through some standard logistic formula with binomial 95% confidence interval.
- chvid 8mo agoCool that you could keep it under 2k but it would nice to have a readable version of the source code. Do you work with it like this or do you have some sort of script you apply to get it down to a single line, single letter variable names?
- noutella 8mo agoWhat you’re describing is the typical output / function of a minifier
- alansaber 8mo agoThe real fun would be reverse-engineering the minified code (there are loads of tools to do this for chrome extensions)
- TZubiri 8mo agonot lossless
- mackeye 8mo agoa readable version seems to have just been added! https://github.com/datavorous/sameshi/tree/master/readable https://github.com/datavorous/sameshi/tree/master/readable
- GeertB 8mo agoHow did you handle games where Stockfish would castle or promote?
- datavorous_ 8mo agoi forced stockfish to play only non castling, non en passant, non promotion moves by filtering legal moves and passing only those as root_moves also removed castling/EP rights from FEN
- comboy 8mo agoI'd call that cheating but the size and capability is impressive nonetheless.
- lekevicius 8mo agoDo you think it would be possible to achieve 1:1 ELO:bytes? Even smaller, but can be less smart.
- datavorous_ 8mo agomaybe for very low ratings it's plausible? 1 elo per byte might happen in a tiny range but at a useful strength it would break fast, that's what i think
- iterance 8mo agoWhat's the snallest possible program that accepts a chess board state and prints any legal move? True randomness may only have a couple hundred ELO, but then, that's pretty big for golf
- dmurray 8mo agoThe program that resigns every time unfortunately does a lot worse than random. But it depends on the population it's pitted against - it should at least pick up a few points against copies of itself.
- contravariant 8mo agoDon't resign, just offer a remise after moving a pawn. Only resign if no pawns are left. I'd claim it would work on human opponents, but I think it would get banned from chess tournaments.
- dmurray 8mo agoPerhaps playing 1. e4 2. Bc4 3. Qh5 4. Qf7 (and resigning or offering a draw if some move isn't legal) would minmax this further The problem isn't really well defined. Elo rating is assumed to be determinable independent of what opponents you face, so scoring 50% against opponents rated 1800 gives you the same information as scoring 26% against opponents rated 2000. In practice that's obviously not completely true, and for degenerate examples like the ones we are discussing it completely falls apart.
- TZubiri 8mo agoCodex or Claude Code?
- datavorous_ 8mo agonone. scribbling long enough on a piece of paper is more enjoyable than prompting.
- semi-extrinsic 8mo agoa thousand times this.
- lyu07282 8mo agoIsn't it bad enough they beat us at chess, do you have to make it even worse? ;p
- genie3io 8mo ago[dead]
- haute_cuisine 8mo agoThis is amazing! Thanks for sharing. What would be the elo gain for 4KB engine? P.S. I assume 1200 elo in chess com scale (not lichess / fide elo) and bullet chess variant?
- grumpopotamus 8mo agoThere is a TCEC category for 4k engines. The top ones are ~3000 Elo.
- sigmoid10 8mo agoIt's wild to think that 4096 bytes are sufficient to play chess on a level beyond anything humans ever achieved. Makes you think what other difficult tasks are out there that take even highly gifted humans years or decades to master, but a superior algorithm would more or less fit into one of those big QR code formats. These things always make me think back to Westworld season 2, where the finale revealed that human minds are much simpler than they themselves believe and fit completely into an algorithm that could be printed in an average book.
- kevmo314 8mo agoThe core search algorithm is very simple though. 4KB engines may not run that fast if they do exhaustive search, but they’ll be quite accurate. According to TCEC the time control is 30 mins + 3 sec, that’s a lot of compute!
- sigmoid10 8mo agoIf you look at the current winner [1], it does a lot more than just brute force tree search. The space state for chess is simply too big to cover without good heuristics. Deep Blue may have been a pure brute force approach to beat Kasparov after Deep Thought failed using the same core algorithm, but modern chess engines search far deeper on the tree with far fewer nodes than Deep Blue ever could thanks to better heuristics. [1] https://github.com/MinusKelvin/ice4 https://github.com/MinusKelvin/ice4
- falsaberN1 8mo agoOh my god the source is so tiny! It's really hard to parse because of it being minified but I love it to bits.
- burstw0w 8mo agoGood job! I love how you obfuscated your code, really in a spirit of FOSS!
- datavorous_ 8mo agoOh well, the file initially looked like https://github.com/datavorous/sameshi/blob/7ab4e47144f96becdb5ad62c3f15e43f230ba381/tinychess.c https://github.com/datavorous/sameshi/blob/7ab4e47144f96becd... It is hideous now!
- burstw0w 8mo agoIt's not about being hideous, it's about being useless. Your code is useless to anyone that wants to contribute, or maybe make something better by improving on the idea.
- y-curious 8mo agoCoworker: “hey if you have a second, I have a one-liner PR open” The PR:
- bstsb 8mo agoit’s minification, not obfuscation. the whole point of the engine is its small footprint
- recursive 8mo agoYou might have misunderstood the purpose of this.
- newzino 8mo ago[flagged]
- tromp 8mo agohttps://www.chessprogramming.org/Toledo https://www.chessprogramming.org/Toledo is a family a moderately strong tiny chess programs.
- jll29 8mo agoCool project. You could also use the front-end of GNU chess to save some lines, and implement only a back-end. Bug report: a b c d e f g h 8 r n b q k b n r 8 7 . . p p p p p p 7 6 . p . . . . . . 6 5 p . . . . . . . 5 4 P . . P P . . . 4 3 . . . . . . . . 3 2 . P P . . P P P 2 1 R N B Q K B N R 1 a b c d e f g h move: b2b3 ai: b6b4 The pawn is not permitted to move two fields after it has already beeen moved once before: b6b4 isn't a valid move after b7b6. (First moving two fields, and then one would have been okay, in contrast.)
- datavorous_ 8mo agoThanks for pointing it out! I will try to patch it. Appreciate you taking the time to test it.
- dfc 8mo agoHow many games did you have to throw away because stockfish wanted to castle? Or did you force stockfish to not castle? Castling seems like such a frequent move it is hard to draw any conclusions about the strength of an engine that does not support it.
- datavorous_ 8mo agozero games were thrown away for castling, because i forced stockfish not to castle (and not to play en passant/promotion) by filtering legal moves and only giving those filtered moves via root_moves so every game stayed in the same no castling variant and you're right, this rating is for that constrained variant, not full chess.
- jsmith99 8mo agoWouldn't stockfish's position evaluation be incorrect in that case? (If it evaluated the position based on a formula that assumed normal rules)
- YawningAngel 8mo agoI'm not quite clear on the how of it, but Stockfish works pretty well outside the normal bounds of chess. There are toy chess variants on chess.com with "dragons" (knight + bishop) and stockfish can use those very effectively
- deleted 8mo ago[deleted]
- tzs 8mo agoWouldn't that just stop it from considering castling, en passant, and promotion on the first move of the position you are analyzing? It's still going to consider those in the tree search, and the static evaluation neural net was trained on positions where they are allowed. For castling you should be able to fix this by specifying that castling rights have been lost in the positions you give it. For the others I think you would need to filter not just when giving it the list of allowed first moves. You'd also have to make it not consider en passant and promotion in the search, by modifying its move generation. The static evaluation would still be off a bit, but that probably would not have much effect most of the time. From what I've read it is feasible to train a new neural net on a decent home computer in maybe around a week, but that's probably overkill for your use of figuring out how strong your engine is at no castling/no promotion/no en passant chess.
- oh_my_goodness 8mo agoIf you ever spent much time at a chess club, you've seen why 2kB is a really disturbing number.
- jqr- 8mo agoI have not. Can you please tell me why?
- oh_my_goodness 8mo agoNot really. You have to see it for yourself. (Partial answer, 2kB is a very small fraction of what we'd like to think counts as human.)
- AlexCoventry 8mo agoHumans don't have much capacity for systematic tree search. It's sort of amazing that humans can do as well as they can, given that limitation.
- CyberDildonics 8mo agoI don't think what you're saying has any connection to chess or chess clubs. 2kB is a very small fraction of what we'd like to think counts as human This doesn't seem to mean anything. Why would 2KB have any relation to "counting as human". It's the data of about 10 comments.
- oh_my_goodness 8mo agoTFA describes a 2kB program can play a human game against humans, and sometimes win.
- CyberDildonics 8mo agoWe know that, that's what the article is about. The things you said are vague and contain no information. What does 2KB have to do with "what we think counts as human" ?
- sireat 8mo agoThis is very cool and having stalemate is nice, however how much space would it take to implement the full ruleset? As you write: not implemented: castling, en passant, promotion, repetition, 50-move rule - those are all required to call the game being played modern chess. I could see an argument for skipping repetition and 50-move rule for tiny engines, but you do need castling, en pessant and promotion for pretty much any serious play. https://en.wikipedia.org/wiki/Video_Chess https://en.wikipedia.org/wiki/Video_Chess fit in 4k and supported fuller ruleset in 1980 did it not? So I would ask what is the smallest fully UCI (https://www.chessprogramming.org/UCI https://www.chessprogramming.org/UCI) compliant engine available currently? This would be a fun goal to beat - make something tiny that supports full ruleset. PS my first chess computer in early 1980s was this: https://www.ismenio.com/chess_fidelity_cc3.html https://www.ismenio.com/chess_fidelity_cc3.html - it also supported castling, en pessant, not sure about 50 move rule.
- dmurray 8mo agoToledoChess [0] has a few implementations of this in different languages. Some highlights: 2KB of JavaScript with castling, en passant, promotion, search and even a GUI 326 bytes of assembly, without the special rules I don't think the author has a UCI-compliant one, but it should be easier than the GUI. There are forks of the JS one that might do it. [0] https://nanochess.org/chess6.html https://nanochess.org/chess6.html
- l674 8mo agoIf anyone is curious, the most common tool I've seen for ELO estimation among engine developers is cutechess [1], which uses SPRT [2]. Or ordo [3], haven't used this myself though [1] https://cutechess.com/ https://cutechess.com/ [2] https://www.chessprogramming.org/Sequential_Probability_Ratio_Test https://www.chessprogramming.org/Sequential_Probability_Rati... [3] https://github.com/michiguel/Ordo https://github.com/michiguel/Ordo
- raphaelmolly8 8mo ago[dead]
- deleted 8mo ago[deleted]
- thomasmg 8mo agoCool! I just recently implemented a chess engine in ~400 (readable) lines, with all rules, first in Java and then ported to my own programming language "Bau" [1]. This is including a terminal UI. I'll measure the ELO, but I was never able to beat it :-) The castling moves are specially tricky to implement I think. I enjoyed the challenge as well. [1] https://github.com/thomasmueller/bau-lang/blob/main/src/test/resources/org/bau/converter/chess.bau https://github.com/thomasmueller/bau-lang/blob/main/src/test...
- ycombinatrix 8mo agoHow come there's no unsigned numeric types in Bau?
- thomasmg 8mo agoI tried to describe this in [1]: "Unsigned integer are intentionally not supported to simplify learning and using the language, to avoid surprising behavior and edge cases, and to reduce security issues and error-handling pitfalls. When needed, unsigned behavior is available through explicit operations. This design does not affect performance or memory usage." I understand this may not sound very convincing yet... it is hard to describe... basically, unsigned types sound simple, but they are not. [1] https://thomasmueller.github.io/bau-lang/features.html https://thomasmueller.github.io/bau-lang/features.html
- ycombinatrix 8mo agoHmm, I don't quite follow. For usages like counters or version numbers, it seems like allowing negatives makes things more complicated rather than less. Like, what if you're getting a u32 over the wire? I feel like using an i32 to represent that data makes it more error-prone. Numbers will unexpectedly appear to be negative?
- dxxvi 8mo agoI wonder how big 1300, 1400, ..., 2200 Elo chess engines are.
- kachapopopow 8mo agoneed to start measuring these things in the size of compiled functions so we can stop looking at oneliners (maybe wasm since it has an easy to read text representation)
- deleted 8mo ago[deleted]
- galkk 8mo agoThis is not chess, but something that allows to move chess pieces. > Not implemented: castling, en passant, promotion, repetition, 50-move rule.
- Mr_Minderbinder 8mo agoI have been into computer chess for many years and I was fully expecting those concessionary statements. I have seen enough programs in this lucrative genre where a lot of attention can be gained by fraudulently claiming you implemented chess in a seemingly impossibly small size. When confronted, the charlatans will often claim senselessly that those omissions were in fact superfluous. This is a behaviour I have unfortunately also observed in other areas of computing. If anyone reading this is interested in small and efficient chess programs that are still reasonably strong, there was a x86 assembly port of Stockfish called asmFish from a couple of years ago (the Win64 release binary was about 130KiB). Also see OliThink (~1000 LOC) and Xiphos which has some of the simplest C code for an engine of its strength that I have seen. I have not investigated the supposedly 4K sized engines that participated in TCEC too closely but from what I have seen so far it would seem that there are a few asterisks to be attached to those claims.
- wittlesus 8mo ago[dead]
- yuppiepuppie 8mo agoVery cool! I’ve added this to the HN Arcade https://hnarcade.com/games/games/sameshi https://hnarcade.com/games/games/sameshi
- Aachen 8mo agoNice initiative! Was hoping the "play" button/link would go to a playable version though
- drev 8mo agoSo small it fits on a gameboy https://github.com/odrevet/sameshi-gbdk https://github.com/odrevet/sameshi-gbdk