# m140byt.es

Created: 2012-03-24 23:53
Updated: 2013-12-06 10:13

# Binary Tetris - 140byt.es

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

### Source

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

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
``````