Show HN: Sameshi – a ~1200 Elo chess engine that fits within 2KB (github.com)

164 points by datavorous_ 8 hours ago

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.

chaps a few seconds ago

Made one with 1KB at 1325 ELO:

  int N,M=136,S=128,I=8e3,X,Y,O,Q,b[129]={6,3,5,7,4,5,3,6};
  char*q="@A?PQ`PQ`_aP^boqPQQSOSUYWO[VXSV",*n=".?+nkbrq?*?NKBRQ";
  D(k,w,l,e,E,x,d){
  int j,t,p,u,r,y,i=0,m=d>1|w>e?w:e,v,h,z,F,G,H;N++;
  do{u=b[i];if(u&k){r=p=u&7;j=q[p+23]-80;
  while(r=p>2&r<0?-r:80-q[++j]){y=i;F=G=S;
  do{H=y+=r;if(y&M)break;if(p<3&y==E)H^=16;
  t=b[H];if(t&k|p<3&!(r&7)-!t)break;v=99*(q[t&7|16]-80);
  if(v<0)m=I;if(m>=l)return m;
  if(h=d-(y!=x)){v+=i%8-y%8;
  b[G]=b[H]=b[i]=0;b[y]=u&31;G&M||(b[F]=k+6,v+=30);
  v=-D(24-k,-l,-m,z=-e-v,F,y,h);
  if(x==9){if(v+I&&i==X&y==Y){Q=z;O=F;return l;}v=m;}
  b[G]=k+38;b[F]=b[y]=0;b[i]=u;b[H]=t;
  v>m&&(m=v,x&8&&(X=i,Y=y));
  }t+=p<5;p<3&&6*k+(y&112)==S
  ||(u&~24)==36&j==7&&G&M&&b[G=(i|7)-(r>>1&7)]&32
  &&!b[G^1]&!b[G^2]?F=y,t--:0;
  }while(!t);}}}while(i=i+9&~M);return m;}
  main(){int k=8,d;char c[9];
  for(O=S,X=8;X--;){b[X+112]=(b[X]+=48)-8;b[X+16]=18;b[X+96]=9;}
  for(;;){for(X=0;X<121;X++)printf("%c",X&8&&(X+=7)?10:n[b[X]&15]);
  gets(c);if(*c)X=*c-16*c[1]+799,Y=c[2]-16*c[3]+799;
  else for(d=N=2;N<1e6;)D(k,-I,I,Q,O,8,d++);
  if(D(k,-I,I,Q,O,9,2)+I)k^=24;}}

sireat 6 hours ago

This 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 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) 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 - it also supported castling, en pessant, not sure about 50 move rule.

dmurray 2 hours ago

ToledoChess [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

jll29 6 hours ago

Cool 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_ 6 hours ago

Thanks for pointing it out! I will try to patch it.

Appreciate you taking the time to test it.

thomasmg an hour ago

Cool! 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...

lekevicius 8 hours ago

Do you think it would be possible to achieve 1:1 ELO:bytes? Even smaller, but can be less smart.

esafak 6 hours ago

That's an awesome code golf challenge

datavorous_ 7 hours ago

maybe 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 6 hours ago

What'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 2 hours ago

l674 6 hours ago

If 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/

[2] https://www.chessprogramming.org/Sequential_Probability_Rati...

[3] https://github.com/michiguel/Ordo

dfc 6 hours ago

How 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_ 6 hours ago

zero 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 2 hours ago

Wouldn't stockfish's position evaluation be incorrect in that case? (If it evaluated the position based on a formula that assumed normal rules)

tromp 6 hours ago

https://www.chessprogramming.org/Toledo is a family a moderately strong tiny chess programs.

kachapopopow an hour ago

need 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)

dxxvi an hour ago

I wonder how big 1300, 1400, ..., 2200 Elo chess engines are.

chvid 8 hours ago

Cool 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 8 hours ago

What you’re describing is the typical output / function of a minifier

alansaber 8 hours ago

The real fun would be reverse-engineering the minified code (there are loads of tools to do this for chrome extensions)

TZubiri 7 hours ago

not lossless

GeertB 8 hours ago

How did you handle games where Stockfish would castle or promote?

datavorous_ 8 hours ago

i 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 5 hours ago

I'd call that cheating but the size and capability is impressive nonetheless.

oh_my_goodness 6 hours ago

If you ever spent much time at a chess club, you've seen why 2kB is a really disturbing number.

jqr- 6 hours ago

I have not. Can you please tell me why?

vardump 5 hours ago

He's just trying to trick HN readers to join chess clubs.

oh_my_goodness 5 hours ago

Not 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.)

CyberDildonics 3 minutes ago

AlexCoventry 4 hours ago

haute_cuisine 7 hours ago

This 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 7 hours ago

There is a TCEC category for 4k engines. The top ones are ~3000 Elo.

sigmoid10 7 hours ago

It'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.

vunderba 7 hours ago

gnramires 2 hours ago

kevmo314 7 hours ago

falsaberN1 7 hours ago

Oh 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 7 hours ago

Good job! I love how you obfuscated your code, really in a spirit of FOSS!

datavorous_ 7 hours ago

Oh well, the file initially looked like https://github.com/datavorous/sameshi/blob/7ab4e47144f96becd...

It is hideous now!

burstw0w 2 hours ago

It'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 7 hours ago

Coworker: “hey if you have a second, I have a one-liner PR open”

The PR:

newzino 6 hours ago

The mailbox board representation is a good call for size-constrained engines. Bitboards give faster move generation but the manipulation code (shifts, masks, magic numbers for sliding pieces) eats a lot of bytes. With mailbox you just need offset tables and a sentinel check for board edges. Curious what your evaluation function looks like though. At 2KB you can't fit piece-square tables (that's 384 values minimum for both colors), so are you doing material-only eval or did you squeeze in some positional heuristics?

The gap between your 1200 Elo in 2KB and the TCEC 4K engines at ~3000 Elo is interesting. That extra 2KB buys a lot when it goes to better evaluation and move ordering. Even a simple captures-first sort in alpha-beta pruning costs only a few bytes of code but can roughly double your effective search depth.

TZubiri 7 hours ago

Codex or Claude Code?

datavorous_ 7 hours ago

none.

scribbling long enough on a piece of paper is more enjoyable than prompting.

semi-extrinsic 6 hours ago

a thousand times this.

lyu07282 3 hours ago

Isn't it bad enough they beat us at chess, do you have to make it even worse? ;p