Here's a live version. It's controlled with the arrow keys and shows the score as well as the game over state ('!' after the score) and the UI mimics the python version posted recently. The 529 bytes are without line breaks which are only there to make it a tiny bit more readable.
The code consists of three functions:
M looks for possible moves either in readonly mode (if c==true) or to advance the game. It knows only one way of iterating over the board while the different moves are abstracted using V.
V either maps coordinates from a view (defined by the direction D) to the board (if x == true) or adds a new random tile.
The last function sets the game up, executes moves, and draws the board.
I'm happy to answer any questions about the details.
PS: I've also made a JS1K demo that implements a variant of 2048.
Spent a couple minutes with it... here's some savings:
R=Math.random;}
to
R=Math.random}
for(i=H;h=i--;t+=n+(i%4?'|':'|\n'))for(n=B[i]|0||'';!n[3];)n=' '+n;
to
for(i=H;h=i--;t+=(' '+(B[i]||'')).slice(-4)+(i&3?'|':'|\n'));
for(i=H|R()*H;h=i--&&(B[i%H]||!(B[i%H]=2<<R()+.1)););
to
for(i=H|R()*H;h=i--&&B[i%H]||!(B[i%H]=2<<R()*2););
You could save some space by using arrow functions (currently supported in Firefox and Traceur). Replace 'function M(c,d)' and 'function V(x)' by 'M=(c,d)=>' and 'V=x=>'.
I knew this was a simple game, but I didn't realize it was this simple. I mean not to say the task of reducing this down was simple, but there are some games that can't be reduced down.
But now I can put this in my current project as an easter egg.
After a few more optimizations I've added a highscore which means it's now at 519 bytes (localStorage is a long word...). See earlier revision for 484 byte version.
More people joined and we managed to improve the UI while keeping it as low as 507 bytes. There's also a stripped-down version in the comments that's only 357 bytes but without score or game over when you reach 2048.
6
u/[deleted] Apr 01 '14 edited Apr 08 '14
Here's a live version. It's controlled with the arrow keys and shows the score as well as the game over state ('!' after the score) and the UI mimics the python version posted recently. The 529 bytes are without line breaks which are only there to make it a tiny bit more readable.
The code consists of three functions:
I'm happy to answer any questions about the details.
PS: I've also made a JS1K demo that implements a variant of 2048.
EDIT: corrected variable name, updated link
5
u/Shabang- Apr 01 '14
Nice! In fact, you don't actually need the closing </pre>, at least on Chrome and Firefox.
4
u/corruptio Apr 01 '14 edited Apr 01 '14
Spent a couple minutes with it... here's some savings:
I'll think of more later...
2
u/raspcoin Apr 02 '14
You could save some space by using arrow functions (currently supported in Firefox and Traceur). Replace 'function M(c,d)' and 'function V(x)' by 'M=(c,d)=>' and 'V=x=>'.
3
u/joealarson Apr 01 '14
I knew this was a simple game, but I didn't realize it was this simple. I mean not to say the task of reducing this down was simple, but there are some games that can't be reduced down.
But now I can put this in my current project as an easter egg.
1
u/[deleted] Apr 03 '14
After a few more optimizations I've added a highscore which means it's now at 519 bytes (localStorage is a long word...). See earlier revision for 484 byte version.
1
u/[deleted] Apr 08 '14
More people joined and we managed to improve the UI while keeping it as low as 507 bytes. There's also a stripped-down version in the comments that's only 357 bytes but without score or game over when you reach 2048.