Instantly share code, notes, and snippets.

Last active April 12, 2024 21:27
Show Gist options
• Save aemkei/1672254 to your computer and use it in GitHub Desktop.
Binary Tetris - 140byt.es

# Binary Tetris - 140byt.es

A simplified variant of the classic tetris game done in less than 140 bytes of JavaScript.

NEW: A standalone version in 300 bytes: aem1k.com/tetris

### Source

```    function(a,b,c,d){d+=c;
return[d<0|a&b<<d?a=
parseInt((a|b<<c)
.toString(d=32,b=new Date&2|1)
.replace('v',''),d):
a|b<<d,a,b,d]}```

Feel free to edit the source and follow me on Twitter (@aemkei).

### Example Layout

``````1.       2.       3.       4.       5.

.....    .....    .....    .....    .....
.....    .....    .....    .....    .....
...##    .....    .....    .....    .....
.....    ...##    .....    .....    .....
##...    ##...    ##.##    ##...    .....
###..    ###..    ###..    #####    ##...
``````

The main logic to move blocks, detect collision, assign new blocks, remove full lines and render the layout are included. Excluded are keyboard controls and the final rendering.

This version is heavy based on binary numbers and bit shift operators. I had to limit the board size to 5x6 (30 bits), because JavaScript converts numbers to 32-bit integers when using bitwise operators. The two left bits are later uses to add the correct padding when dealing with numbers that start with "0".

### Basic Concept

The Board

``````                  00000         .....
798               11000   =>    ##...
11110         ####.
``````

The Block

``````3                 00011  =>     ...##
``````

Checking for Collision

``````                  00000         .....
798&3 = 2         00000  =>     .....
00010         ...X.
``````

Moving the Block at X Axis

``````3 << 1 = 6        00110   =>    ..##.
``````

Moving the Block at Y Axis

``````                  00011         ...##
3<<10 = 3072      00000   =>    .....
00000         .....
``````

Combining Block and Board

``````                  00011         ...##
798|3072 = 3870   11000   =>    ##...
11110         ####.
``````

Find full line (using base 32)

``````                  00011         3
3999              11100   =>    s
11111         v
``````

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 function ( a, // current board b, // current block c, // current position d, // new block offset for position e // placeholder: the layout to return ){ return d += c, // add offset to position e = a | b << d, // render layout based on board and moved block d < 0 | a & b< 1) 3 // double "##" (3 => 111) ), [ // the final return a, // new board b, // new block d, // new position e // final layout to render ] }
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 function(a,b,c,d,e){return d+=c,e=a|b<
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2011 YOUR_NAME_HERE Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. You just DO WHAT THE FUCK YOU WANT TO.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 { "name": "tetris", "description": "A binary variant of the classic Tetris game.", "keywords": [ "tetris", "game", "binary" ] }
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters


### plaes commented Feb 15, 2012

Just a little inspiration: http://bytex64.net/code/datasnake/

### aemkei commented Feb 15, 2012

@plaes: Have you seen https://gist.github.com/1449250 the 140byt.es version of Snake?

### plaes commented Feb 15, 2012

@aemkei I was hinting that it would be cool to see a data-uri version of this snippet..

### tsaniel commented Feb 15, 2012

@plaes: What about make it an 140bytes entry? (just the logic part fitted in 140 bytes, as "Tweetris")

Excellent work!

### nathanpc commented Feb 15, 2012

Awesome code mate. Congratulations!

### kendhia commented Feb 18, 2012

Awsome code. you made javascript more beautifull with this game !
Congrats!

### interval1066 commented Feb 19, 2012

Your licensing scheme is too restrictive.

### syntacticsugar commented Feb 19, 2012

Oh Wow wow wow wow wow!!!!! I am shocked, amazed, and laughing my head off!

And then I saw the licensing scheme! I am highly offended by the licensing scheme for it is entirely too restrictive! :D :D Why don't you loosen it up and allow me to rip it off and claim it as my own???

<3 <3 <3 <3 <3 love everything! thanks! inspires me to pursue JavaScript!

Beautiful.

very awesome

awesome

### Gemberkoekje commented Feb 21, 2012

I found you a bug if you keep holding the key to the left, it crashes, probably because the bits go off top. But other then that silly thing (The fact that I love breaking stuff) I have mad respect for doing it in 140 chars.

amazing work.

### Nushooz commented Feb 21, 2012

Hey you left out the control and display code. This game is like 900 bytes working. You're a phony! Hey everybody, this guy is a great big phony!

### plicaplica commented Feb 22, 2012

"0. You just DO WHAT THE FUCK YOU WANT TO."

I just wanna fuck.

### iznax commented Feb 23, 2012

I made a version with a larger play field and no edge wrap-around problems, but I can't quite get it down to 140 characters. http://jsbin.com/aparub/3

HAHA, so cute,

### iznax commented Feb 27, 2012

here's my full version of Tetris with rotating pieces, the core function is 255 characters. http://jsbin.com/ujewuk/2

function(p,r,t,g,z){var k,d=z[0];z=((r+z[1])&15)-r;for(k=4;k--;)if(g[p+d+t[r+z+k]]){if(d>1){for(k=4;k--;)g[p+t[r+k]]=1;for(k=0;k<N*Y;k+=Y){for(d=2;d<Y;)d=g[k+d]?d+1:F;if(d<F)for(d=k;--d;)g[d+Y]=g[d];}p=W/2;t=T[new Date%7]}d=z=0;break}return[p+d,r+z,t,g]}

wow .It's good.

### viktor-evdokimov commented Nov 19, 2013

Can you guys please send me examples of small games like this, I want to create collection of links. Thanks. 30lines.info

### ewerybody commented Feb 28, 2017

this 140bytes website... maybe you should remove the link ;]