BreakPoint

cancel
Showing results for 
Search instead for 
Did you mean: 

Are you smarter than a kilobyte of javascript code?

Try to beat this. Pretty impressive minimalistic code.

 

Spoiler
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)}

 

I wonder how easy it would be to translate to LabVIEW. 😄

 

altenbach_0-1615313616283.png

 

(... and yes, I beat it on the first try ;))

 

 

 

Message 1 of 12
(9,391 Views)

Impressive piece of code. Or is it? It is completely unreadable. Not even LabVIEW can be that unreadable... can it?

Certified LabVIEW Architect
0 Kudos
Message 2 of 12
(9,326 Views)

@altenbach wrote:

 

(... and yes, I beat it on the first try ;))

 


Do you mean you made a VI that does the same, and it's smaller than 1 kB?

 

 An empty VI is 4kB...

Message 3 of 12
(9,319 Views)

wiebe@CARYA wrote:

@altenbach wrote:

 

(... and yes, I beat it on the first try ;))

 


Do you mean you made a VI that does the same, and it's smaller than 1 kB?

 

 An empty VI is 4kB...


Do you question Altenbach? Of course he did! 😀

 

Ok, maybe not, but a fair comparison would be to only compare the same piece of code.

Certified LabVIEW Architect
0 Kudos
Message 4 of 12
(9,315 Views)

The whole application isn't 1kb, just the "game engine" the UI appears to be something extra.  So in that case I'd argue that the block diagram and front panel shouldn't be included, and just the code.  Still impressive, and I don't intend on submitting any LabVIEW equivalent. 

 

Also about 10 moves in I remembered I'm not as good at chess as I once playing kids my own age, it was probably going to beat me.

0 Kudos
Message 5 of 12
(9,289 Views)

Yes, I did not mean to match the code size, but just to implement the same algorithm in LabVIEW. (Unfortunately, I don't understand the code 😞 )

 

Chess programming has a long history and back in the early eighties there were commercial chess computers that ran on tiny microcontrollers. (e.g. on a 400kHz Hitachi 4 bit processor with 2k of ROM (10bit words) and 80 Bytes (0.08kB!) of RAM. details).

 

The main strength of this program is the unpredictability of its moves (jungle chess strategy) and since it thinks a small fixed number of moves ahead, to win 100%, simply do not make any glaring mistakes or it will immediately pounce. 😮 (It plays very aggressively and typically leaves its pawn structure in shambles. I haven't lost yet except when I played to lose to test things. Make sure to mentally keep track of the position because the pieces are (maybe intentionally) difficult to tell apart, especially if parts of the black pieces overlap.  😉 )

 

Maybe somebody wants to play it against our friend here. 😄

0 Kudos
Message 6 of 12
(9,275 Views)

Glaring errors like moving the R 1st when castling, inadvertent touches, or missing which black piece moved when blinking.   Not the best UX.

 

Back in the day, I trimmed Othello down <6k in Basic including display and score.   Like Checkers and connect 4, Othello is not a game.  When there exists a solution,  it becomes a puzzle. I suspect "Go" is also a puzzle, but the permutations are excessive. 


"Should be" isn't "Is" -Jay
0 Kudos
Message 7 of 12
(9,242 Views)

@JÞB wrote:

When there exists a solution,  it becomes a puzzle. I suspect "Go" is also a puzzle, but the permutations are excessive. 


By that definition, Chess is also a puzzle and it has been fully solved for 7 pieces or less. The chess tablebase for 7 pieces nicely fits into 140TB. You can extrapolate that it will take an infinite time and resource to solve the full game, even for 8 pieces it is getting very difficult. (see also).

 

Note that I did a similar thing in my LabVIEW 4x4 tic-tac-toe game. It simply contains a database of all possible decisive positions, making it nice and small (Few MB) because I eliminated all transformations (rotation, mirror, etc.) and all positions that are a draw or impossible to reach. It was a lot of fun to efficiently generate that database. It might be worth revisiting that topic now that we have maps, but I think my algorithms back then were quite efficient and would still be hard to beat.

 

Go has a significantly steeper combinatorial explosion and is thus much harder than chess using classic search algorithms. Moderns AI methods seem to work well, though. The success of e.g. AlphaZero (and previous iterations of it) against top humans, just proves the limitations of the human brain. You don't really need to be very smart (on a cosmic scale!) to be the world champion, you simply need to be smarter than the second best human. 😄

 

In chess, top computer programs are now significantly better than any humans. Top humans can still win occasionally with sufficient handicap (e.g. knight odds).

Message 8 of 12
(9,200 Views)

@altenbach wrote:

@JÞB wrote:

When there exists a solution,  it becomes a puzzle. I suspect "Go" is also a puzzle, but the permutations are excessive. 


By that definition, Chess is also a puzzle and it has been fully solved for 7 pieces or less. The chess tablebase for 7 pieces nicely fits into 140TB. You can extrapolate that it will take an infinite time and resource to solve the full game, even for 8 pieces it is getting very difficult. (see also).


The likelyhood of a solution does not infer that one has been calculated into existence. Although the time and resources available today are not sufficient to produce a solution,  I would not extrapolate that those requirements are not finite.  So, Chess is still a game!  Masters of the game persue solutions to specific states of the solution matrices.


"Should be" isn't "Is" -Jay
0 Kudos
Message 9 of 12
(9,192 Views)

@JÞB wrote:
Although the time and resources available today are not sufficient to produce a solution,  I would not extrapolate that those requirements are not finite.

Once the number of possible states approaches the number of atoms in our solar system, storage alone might be a slight problem. 😄

0 Kudos
Message 10 of 12
(9,181 Views)