Can you beat 1024 bytes of JavaScript?
March 5, 2021 4:31 PM   Subscribe

The Kilobyte’s Gambit 1k chess game: I adapted an impressive 1.25KB chess engine to remove display code and get it down to 1024 bytes, then created a separate interface using pixel art of The Queen’s Gambit. It won’t win any tournaments, but if you’re a chess novice brace yourself for a challenge. [via mefi projects]
posted by not_the_water (51 comments total) 27 users marked this as a favorite
 
Betteridge's law of headlines strikes again.
posted by signal at 4:42 PM on March 5, 2021 [24 favorites]


Can you adjust the graphics a bit? I had a hard time differentiating some of the pieces.
posted by mikeand1 at 4:50 PM on March 5, 2021 [9 favorites]


Well, my iPad just beat me at chess, but it didn’t fare anywhere near as well in kickboxing.
posted by azpenguin at 5:28 PM on March 5, 2021 [16 favorites]


It took me a dozen tries. That's really impressive for something so tiny.

I love, love, love the CGA graphics. That terrible palette is burned into my brain so hard.
posted by phooky at 5:54 PM on March 5, 2021 [6 favorites]


I lost in like 5 moves.

Now I just need 2014 bytes of JavaScript to dig me a grave.
posted by GuyZero at 5:58 PM on March 5, 2021 [2 favorites]


The author of the JS chess engine has done many other tiny code projects, including a chess engine in 392 bytes of x86 assembly code.
posted by RobotVoodooPower at 6:07 PM on March 5, 2021 [1 favorite]


I don't know if it's 2014 bytes of JavaScript, but you can always use this.
posted by phooky at 6:08 PM on March 5, 2021 [1 favorite]


I see you've got the burnt croissant bug covered on the opening page.
posted by clawsoon at 6:23 PM on March 5, 2021


I beat it using the London System*! Woo! Watching all of those viral chess videos finally paid off!

*described as "the perfect opening for the lazy student". I.e. me.
posted by clawsoon at 6:38 PM on March 5, 2021 [3 favorites]


I had a hard time differentiating some of the pieces.

Bishops look like pawns. Got me on that one.
posted by ovvl at 6:39 PM on March 5, 2021 [1 favorite]


This is amazing and also super frustrating. I just can't beat it and I can beat low-rated humans.

I haven't done any programming since BASIC but I would love to be able to figure out how that short but of code works.
posted by bondcliff at 7:46 PM on March 5, 2021 [1 favorite]


Like bondcliff, I am someone with very rudimentary programming understanding, and would love to understand how this works. The transformation of so little code into something that even just knows the rules of chess is incredible to me. The fact that in can actually play well enough to beat some humans seems mysterious enough to just be magic.
posted by ManInSuit at 7:51 PM on March 5, 2021 [2 favorites]


Not too difficult to beat. It plays a weak but reasonable game. It will catch any blunders you make. But getting it done in the size is a really cool programming project.
posted by Xoc at 7:56 PM on March 5, 2021 [1 favorite]


If I'm following the correct links, it looks like it takes a 170-page ebook to explain that kilobyte of code. "For each source line there are multiple reasons for some coding decisions..."

Makes me think of the clever things that some bacteria (and especially viruses) do to get multiple proteins out of limited DNA. Every which way you read it, it does something different and useful.
posted by clawsoon at 8:13 PM on March 5, 2021 [2 favorites]


Either I do not understand the rules of chess, or it is totally blocking me from making legit moves. Ragequit in 10.
posted by Meatbomb at 8:15 PM on March 5, 2021


Meatbomb, you're not trying to drag the pieces, are you? I tried that for the first little while before I figured out to click on start and end squares for my moves. Didn't run into any legal moves that it wouldn't let me do. Even castling worked. It'd be interesting to see your board position and the move(s) you were trying and failing to make.
posted by clawsoon at 8:29 PM on March 5, 2021


I guess I beat it. It wasn't pretty but I did beat it.
posted by Urtylug at 8:35 PM on March 5, 2021 [1 favorite]


clawsoon, I was attempting to move my knight for the second time, and it seemed like a totally acceptable move. I also tried moving a bishop, for the first time. Again I am not a huge expert but they seemed like totally normal and legit moves.
posted by Meatbomb at 8:39 PM on March 5, 2021


Meatbomb, I'm assuming you ruled out any checks or pins as a reason you weren't allowed to move?
posted by clawsoon at 8:52 PM on March 5, 2021 [1 favorite]


Very cool!

I tried to play the Queen's Gambit against it, but sadly after 1.d4 it played 1...h5.

Based on my sample size of one game, it seems to really love making lots of pawn moves in the opening. It didn't move a piece until move 10! It's pretty terrible positionally, but good at making and defending against one- or two-move threats. So to beat it, you can just play any kind of normal opening setup and develop all your pieces quickly while it faffs around pushing random pawns, and you'll have an overwhelming positional advantage by the middlegame, at which point you should be able to convert straightforwardly as long as you are careful not to outright hang any pieces.
posted by Syllepsis at 9:35 PM on March 5, 2021 [10 favorites]


Can you beat 1024 bytes of JavaScript?

I can, reliably, now that I have its measure. It took a while to work it out, though. Key seems to be to make h5 a poor option for it early on; it really likes that move.
posted by flabdablet at 10:03 PM on March 5, 2021 [4 favorites]


My favorite nimzo opening did just fine; I'm guessing closed games with lots of dependencies aren't a forte.
posted by kaibutsu at 11:25 PM on March 5, 2021


I see you've got the burnt croissant bug covered on the opening page.

It's been fun seeing the game do well & the nice comments about it... but also frustrating that there are hundreds of people saying "The pawns cheat!", "Some pieces freeze!", "It won't castle!", etc because they don't know the rules.
(There may well be subtle bugs in the engine, but so far none of the grumbles have stood up to scrutiny)
posted by malevolent at 11:26 PM on March 5, 2021 [2 favorites]


yes it is a pawnpusher, but I blundered a piece after it had made like 7 pawn moves in the opening, and it was efficient enough with finishing me
posted by thelonius at 11:35 PM on March 5, 2021 [1 favorite]


I'm an extremely weak player, but I was able to win after breaking the position open with a sacrifice.
posted by nosewings at 12:50 AM on March 6, 2021


So as someone who used to play the Atari 2600 chess game, can someone explain whether the chess engine here is even more compact than that one?
posted by johngoren at 1:23 AM on March 6, 2021 [1 favorite]


No, because a chess engine expressed in 1k of Javascript requires way more than an Atari 2600's total memory capacity for the Javascript interpreter before it does anything useful. The Atari 2600 chess game includes all the graphics and I/O coding as well, and runs directly on the box's extremely limited hardware.
posted by flabdablet at 1:36 AM on March 6, 2021 [2 favorites]


This engine is tiny (its author has smaller ones, right down to 326 bytes of machine code), and plays a lot better than those early Atari/ZX81/etc efforts, but can make extravagant use of memory & CPU (and since it was originally coded in 2010, browsers & devices have improved enough to let it now look an extra move ahead)
posted by malevolent at 2:16 AM on March 6, 2021 [2 favorites]


malevolent: but also frustrating that there are hundreds of people saying "The pawns cheat!", "Some pieces freeze!", "It won't castle!", etc because they don't know the rules.

This sounds like an opportunity: A chess engine that plays according to what the average person thinks the rules of chess are.

(Or even an article about what the average person thinks the rules of chess are would be interesting.)
posted by clawsoon at 4:20 AM on March 6, 2021


This is a triumph of minimalist design. I have played the original in past years (Toledo Chess). What strikes me is how secure this coding is versus other programs. Apparently, security researchers use blat as an e-mail client for security, as it is small enough to read and understand all of the code. Your other project bootOS...did you create it with security in mind? Beyond tracing the microchips yourself, it seems like this would be the safest possible way to develop a system free of surveillance.
posted by metamonday at 6:42 AM on March 6, 2021


Atari 2600 Video Chess is 4K. It'd be interesting to see it disassembled.

Oscar managed to produce a 1K chess game for Atari 2600 though it omits some things like castling and move validation. The AI engine takes up 429 of those 1024 bytes.

RAM usage isn't a big factor, as these engines just walk up and down a search tree and do/undo moves in the same memory space.

x86 still seems to be the winner for code density.
posted by RobotVoodooPower at 7:14 AM on March 6, 2021 [1 favorite]


No
posted by chasles at 7:30 AM on March 6, 2021 [1 favorite]


> Can you beat 1024 bytes of JavaScript?

What a stupid question. Of course not.

The first chess program I ever played was one that had been laboriously typed into a KIM-1, which had 4 KB of RAM and for graphics only a six-digit LED display, like you might expect on a calculator that somebody cut corners designing. Playing against it required a real-world chess board. You would move your piece on the board, enter the move using 16 character keypad (hexadecimal, you know), and then wait for the computer to display its move, after which you would update the board manually. It fucking shellacked me every time*, that's how good I am at chess.

As far as I'm concerned, the past forty years of progress in chess automation has been of no value what so fucking ever, aside from making my unbroken string of defeats more stylishly presented, convenient and portable.

*(Excepting when we discovered it had no room in its tiny little brainpan for error checking, so it would struggle gamely along even while white queen flew to A8 on the first move and progressively captured all the black pieces.)
posted by ardgedee at 7:32 AM on March 6, 2021 [5 favorites]


Impressive, if it is what it seems.

We need to put this chap to work on the global heating problem. He knows how to get things done.
posted by Twang at 7:42 AM on March 6, 2021


They really ought to improve the graphics, though. I played 3 games and now my eyes are hurting. (Oddly enough, the pain first started after I had closed the browser tab.)
posted by WalkingAround at 10:41 AM on March 6, 2021


Or even an article about what the average person thinks the rules of chess are would be interesting.

The misconception that I have seen most often (other than not knowing about en passant pawn captures, I guess) is believing that you can't promote to a piece that hasn't been captured yet. Why do people think this? They are taught it when they are taught how the pieces move and how to play, I think. I've heard that it is pretty much the official local rules in some places. A rustic custom, like saying "Check to the Queen!" when you attack the Queen.

Another thing that new players often come up with: why can't my King take a piece that is guarded only by a piece pinned to its own King? The pinned piece isn't "really" protecting that square, since it cannot move to it. So my King should be able to take that piece, since it isn't really in check there, since he cannot take my King. If you look in the rules of chess, the FIDE rules, it explicitly says that none of this matters, is why. The pinned piece still protects the piece that you'd prefer to take.
posted by thelonius at 11:32 AM on March 6, 2021


I beat it on the second try. I think it really is just a matter of using a reasonable opening, since it seems obsessed with pushing pawns (1. ..h5?). If you control the center at the get-go and play conservatively, it'll eventually run out of lines and you can trade pieces to open the board. I won by trading queens, then queening a pawn, which it didn't really try to prevent. I think if the program included even a small opening book, it'd be much harder.
posted by swift at 11:32 AM on March 6, 2021 [1 favorite]


(1. ...h5?).
It should at least move its Rook up to ...h6 if it is going to do this: how is it going to menace the enemy from h8?
posted by thelonius at 11:35 AM on March 6, 2021


An interesting historical non-standard rule that persisted in parts of Europe (mostly Italy I think) was "free castling", where you had more options with where the King and Rook go when you castle. The King could go to h1, for example, or the Rook to e1. I think that sometimes you only had one or two options, but in other locales, you could use every possibility. I have heard that castiling itself evolved from the "King's Jump", a rule that permitted the King to move two squares once per game. So you'd have to checkmate people twice, or mate them so thoroughly that there were no squares they could King's Jump to. This actually sounds fun.
posted by thelonius at 11:46 AM on March 6, 2021


No.
posted by donpardo at 11:52 AM on March 6, 2021


I am amused that the code contains "1e4". It's obviously just short form for 10,000, saving 2 bytes, and not the favourite opening move of chess coaches who want you to learn your tactics, but still nice to see it.
posted by clawsoon at 12:32 PM on March 6, 2021


swift: I think if the program included even a small opening book, it'd be much harder.

Clearly someone needs to port it to WebAssembly so that an opening book can be fit into its 1024 bytes.
posted by clawsoon at 12:41 PM on March 6, 2021


I finally found a definitive rating for the C version: 1224, which is about where the 1966 PDP-6 MacHack landed.
posted by RobotVoodooPower at 12:47 PM on March 6, 2021 [2 favorites]


... [mis]believing that you can't promote to a piece that hasn't been captured yet. Why do people think this?
If you're playing on a physical board, promotion to an un-captured piece requires hardware which promotion to a previously-captured piece does not. Of course you could just do the swap in your minds, but picture your friend (who's a little murky on the rules anyway) getting upset because he forgot that particular pawn can move backwards now for some reason.

A house rule, where you can't promote a pawn unless there's a captured piece available to replace it, sounds like a good way to avoid bad feelings among young players.
posted by fantabulous timewaster at 2:00 PM on March 6, 2021 [1 favorite]


> Of course you could just do the swap in your minds, but picture your friend (who's a little murky on the rules anyway) getting upset because he forgot that particular pawn can move backwards now for some reason.

> A house rule, where you can't promote a pawn unless there's a captured piece available to replace it, sounds like a good way to avoid bad feelings among young players.
I'm pretty sure USCF regulations say you're supposed to use a Teenage Mutant Ninja Turtles action figure to represent the promoted queen.
posted by Syllepsis at 2:41 PM on March 6, 2021 [2 favorites]


A house rule, where you can't promote a pawn unless there's a captured piece available to replace it, sounds like a good way to avoid bad feelings among young players.


...or ones at the pub. You are probably right.
posted by thelonius at 5:59 PM on March 6, 2021


I, a rusty and mediocre player at best, played maybe ten games and finally scored a draw and then a win. It’s a fun toy, and the cga graphics are endearing!
posted by rikschell at 8:34 PM on March 6, 2021 [2 favorites]


I think I finally beat it after about fifteen games. It fell for a discovered check and I took her queen.

This is amazing and also I hate it and can never play it again.
posted by bondcliff at 2:20 PM on March 7, 2021 [1 favorite]


I opened with e4 and it responded with h5??? And I destroyed it. There isn’t a clock so take your time to find good moves. Since it only looks 4 moves deep and does a simple min / max calculation you can trick it with tactics and traps pretty easily.
This is actually a neat little program to help sharpen your game and avoid and setup simple traps and play the sharper lines in your openings.
posted by interogative mood at 2:54 PM on March 7, 2021 [1 favorite]


A guy I used to play would train sometimes by playing Chessmaster on PC, blindfold mode (pieces are hidden), with it limited to a few ply.
posted by thelonius at 3:30 PM on March 7, 2021


I opened with e4 and it responded with h5???

I tried 1. g4 just to see if it would respond 1....h5
It did not, it played 1...d5
posted by thelonius at 9:46 AM on March 9, 2021


« Older Sorry, I Missed Your Call   |   But where does Eddie fit in? Newer »


This thread has been archived and is closed to new comments