Tetris In 140 Bytes
Written by Ian Elliot   
Saturday, 18 February 2012 11:17

Is it possible to write a program in no more than a tweet's length? A website called 140byt.es says it is and has an implementation of Tetris to prove it.

 

You might think that programmers had enough to keep themselves amused without thinking up silly challenges. 140byt.es is a website devoted to short things you can say in JavaScript. Write a function that does something impressive using 140 characters or less and you can display it for all to see. Putting this another way, you can write an impressive JavaScript function that could be sent in an SMS or tweet.

 

140bytesLogo

 

Of course such attempts at compressing programs into tiny spaces go against the ethos of clear maintainable code, but this is day-off stuff and purely for fun.

You might think that 140 characters aren't sufficient to make any JavaScript tweet worth looking at, but you would be wrong. What about a Mandelbrot viewer with zoom and pan, or something useful like a credit card number checker.

One that caught my attention in particular was an implementation of Tetris - ok, it only has two types of  block - hence its title "Binary Tetris" - and there's no rotate, but it works. The blocks fall down the screen and you steer them into place. You can try it out by playing the demo.

The complete function is

   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]}

Of course the fun for the reader is to work out how it works. To do this you need to know that the function doesn't include the display routine or the user interaction. You also need to know some tricks of the trade. In particular, if you are wondering what the Date object is doing in there, it's a cheap way to get random numbers.

If you are stumped then a clue is that the board and falling blocks are represented as bit patterns and the whole logic is implemented as bit manipulation. The good news is that this tiny program is explained in great detail. If only all programs were documented to this degree!

 

binarytetris

To join the 140byt.es simple go to their website, fork the standard code and start work. There you will also find some interesting tips on how to save space while writing JavaScript.

 

 

More Information

http://140byt.es/

Binary Tetris

Disqus seems to have lost all of the comments to this article. We are looking into where they have gone - apologies.

blog comments powered by Disqus

 

To be informed about new articles on I Programmer, subscribe to the RSS feed, follow us on Google+, Twitter, Linkedin or Facebook or sign up for our weekly newsletter.

 

Banner


Learn Scratch With Creative Computing Online Workshop
07/06/2013

A free online course that introduces computational thinking and the Scratch programming language has just started. Aimed primarily at K-12 educators it is open to anyone who wants to join in. 



Google Compute Engine Now Open To All
23/05/2013

The initial announcement of Google Compute Engine last year limited access to a small number of specifically invited developers, but Google has now widened access to the preview to anyone who wants to [ ... ]


More News

Last Updated ( Monday, 20 February 2012 09:24 )
 
 

   
RSS feed of news items only
I Programmer News
Copyright © 2013 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.