# The Kilobyte’s Gambit

Can you beat 1024 bytes of JavaScript?

Next

• You play as white. Click on a piece, then click where to move.
• Supports castling, en passant & pawn promotion (to queen only).
• It won’t announce victory/defeat, only prevent any further moves.
• The entire ‘brain’ of the chess engine fits into 1024 bytes (only 3 times the length of this help text), including setting up the board & validating moves.

Adapted from code by Oscar Toledo G. and graphics by Pinot with their kind permission

(NB: if puzzled by a pawn move, please check for en passant before reporting a bug)

## How It Works

(NB: if puzzled by a pawn move, please check for en passant before reporting a bug)

Here’s all of the chess engine’s code, it’s a modified version of the 1.25K game on Oscar Toledo G.’s site, where you’ll find all his award-winning tiny chess programs and an ebook explaining how they work.

`for(B=y=u=b=0,x=10,z=15,I=[],l=[];l[B]=("ustvrtsuqqqqqqqq"+"yyyyyyyy}{|~z|{}@G@TSb~?A6J57IKJT576,+-48HLSUmgukgg OJNMLK IDHGFE").charCodeAt(B)-64,B++<120;I[B-1]=B%x?B/x%x<2|B%x<2?7:B/x&4?0:l[u++]:7);X=(c,h,e,S,s)=>{c^=8;for(var T,o,L,E,D,O=20,G,N=-1e8,n,g,d=S&&X(c,0)>1e4,C,R,A,K=78-h<<9,a=c?x:-x;++O<99;)if((o=I[T=O])&&(G=o&z^c)<7){A=G--&2?8:4;C=9-o&z?l[61+G]:49;do{R=I[T+=l[C]];g=D=G|T+a-e?0:e;if(!R&&(G||A<3||g)||(1+R&z^c)>9&&G|A>2){if(!(2-R&7))return K;for(E=n=G|I[T-a]-7?o&z:6^c;E;E=!E&&!d&&!(g=T,D=T<O?g-3:g+2,I[D]<z|I[D+O-T]|I[T+=T-O])){L=(R&&l[R&7|32]*2-h-G)+(G?0:n-o&z?110:(D&&14)+(A<2)+1);if(S>h||1<S&S==h&&L>2|d){I[T]=n,I[g]=I[D],I[O]=D?I[D]=0:0;L-=X(c,h+1,E=G|A>1?0:T,S,L-N);if(!(h||S-1|B-O|T-b|L<-1e4))return W(I,B=b,c,y=E);E=1-G|A<7|D|!S|R|o<z||X(c,0)>1e4;I[O]=o;I[T]=R;I[D]=I[g];D?I[g]=G?0:9^c:0}if(L>N||!h&L==N&&Math.random()<.5)if(N=L,S>1)if(h?s-L<0:(B=O,b=T,0))return N}}}while(!R&G>2||(T=O,G|A>2|z<o&!R&&++C*--A))}return-K+768<N|d&&N};Y=(V)=>{X(8,0,y,V);X(8,0,y,1)};Z=(U)=>{b=U;I[b]&8?W(I,B=b):X(0,0,y,1)}`

Included above are setting up the board and pieces, checking your moves are legal, and deciding how to respond. It doesn’t flag up checkmate/stalemate though, you’ll have to work that out for yourself.

Looking 4 steps ahead, a points system considers factors such as the value of pieces, the strength of areas of the board, and speed of capture/victory. It calls an external function to update the display, and the display code calls functions to trigger moves.

Graphics were adapted from Pinot W. Ichwandardi’s thread imagining The Queen’s Gambit as an MS-DOS game. You can discover more of his work on Twitter and Instagram.

You can also follow Oscar Toledo G. on Twitter to find out about his boot sector and retro games.

Huge thanks to Pinot and Oscar for letting VOLE.wtf reuse their work.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6535753,"math_prob":0.98197263,"size":2122,"snap":"2021-31-2021-39","text_gpt3_token_len":904,"char_repetition_ratio":0.060906515,"word_repetition_ratio":0.0,"special_character_ratio":0.4057493,"punctuation_ratio":0.20862308,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9815157,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-18T20:39:27Z\",\"WARC-Record-ID\":\"<urn:uuid:a58fa8b1-8bd1-4167-ad36-ad232ee72a4c>\",\"Content-Length\":\"38405\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d3da854b-a160-437c-9af3-b7e6420ecc54>\",\"WARC-Concurrent-To\":\"<urn:uuid:bf71c307-eaa5-49bc-b149-d9e718e5f4c6>\",\"WARC-IP-Address\":\"104.21.58.151\",\"WARC-Target-URI\":\"https://vole.wtf/kilobytes-gambit/?utm_campaign=Infrequent%20IT%28sec%29%20commentary%20from%20Anant&utm_medium=email&utm_source=Revue%20newsletter\",\"WARC-Payload-Digest\":\"sha1:D7RYN22JI2WDGQJMCZXGBFWN6ATSAG67\",\"WARC-Block-Digest\":\"sha1:DMXXNLVV4LDT7NP3Z47KRWTC7UNCQHPQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780056572.96_warc_CC-MAIN-20210918184640-20210918214640-00575.warc.gz\"}"}