Shortris



This page is about my attempt to make as small Tetris-game as possible with Java. In this case, with "small" I mean shortest possible source code, not size of Java class-file or actual pixel-size of game.


Original version

First time I tried this at year 2001 and managed to put game in 1.347 bytes. Not only source was short but game itself was really tiny, only 12*25 pixels. Wasn't very enjoyable to play...

However, that game actually came in good use soon, whole source code is printed in back of my business card in Apaja with only some changes to class and method names:


New version

During November 2002, I decided to make new version of that game. This time more playable than original, meaning that at least pixel-size of game should be bigger. Now it is 192*400 pixels. Game also should have increasing difficulty during game so that there is some challenge to play it. I named this new game to Shortris.

My goal was to shrink source in less than one kilobyte (1.024 bytes) or maybe even less than 1.000 bytes. After about week that goal was already reached. And after another week nights spent, final result was actually quite enjoyable Tetris which source fit to 886 bytes! Compiled class is only 1.884 bytes. But game still have these features!

Here's full source code Shortris-game (wrapped to 83 bytes per line):



(Yes, it is image. Source is in this page just to show what result looks like.)

This color-selecting is one of my favorite statements:
t=(P%11<1&a>3)|a>23|(O>1&P==I+i%4+1&a==G+i/4&(T>>i++&1)>0)?0:P%11>0&a<24&o!=null&&(o[a]>>P-1&1)>0?a*9:4145151<<(O<1?n:2);

And I'm kind a proud of this command too which select new block and describe shape (out of those seven tetrominoes) of that selected block to program:
T=(int)(0x3C736CD38BC6L>>_%7*7&255)*n;


Try Shortris

[No Java!] Since Shortris is applet, it's playable directly with all www-browsers that have Java-support. Shortris is running on left.

(If you see just text [No Java!] instead of red&black Tetris-game, your browsers doesn't support Java or Java is disabled. If you just see empty white or gray box, applet is started but game doesn't start for some reason. Might happen with Opera, see Technical features below.)

Click mouse above game to activate applet and press any key to start game. After game starts, these are keys to control dropping block (planned to be played with numpad):
[4] Left
[6] Right
[5] Turn
[2] Drop (speed up)

Many other keys on keyboard may work too, for example [Space] can be used to turn blocks too


(Game on left is exactly that 886 bytes Tetris, except class name is changed from S to Shortris and added getAppletInfo()-method to return info of applet when requested. Those changes doesn't affect the game itself in any way, but makes class-file somewhat bigger than that 1.884 bytes mentioned above. Some one would have notified me about this sooner or later :-)

Note! Applet doesn't work correctly with Java versions 5 or higher, sorry :-/

Shortris game features

- Restartable game
    - After game is over, new game can be started just by pressing any key (or if you don't know where any-key is, just press [Space] ;-)
- Random blocks, different in every game
    - There's only few rare exceptions when game may start to repeat same blocks but that never happens during normal playing
- Increasing difficulty, blocks start falling faster during game
    - Speed increases after each 50 seconds played
    - Maximum speed is reached after about 6 minutes and 40 seconds
- Flicker free 4 color graphics
    - Background is white during game and red after game is finished, works as kind of "Game over"-message for player
    - Borders and currently dropping block are black
    - Dropped blocks are blue, fading to black toward top of game area
- Borders around game area
    - Game area borders also show when game is going to end: Game is over after pile of dropped blocks goes higher than top of borders

Technical features

- Compiles with JDK 1.1.8 and J2SDK 1.4.0 (just one warning about deprecated API)
- Works correctly with Appletviewer, Netscape 4.76 and IE 4 and 6
    (Only problem is with Opera 5 and 6 where applet doesn't start or blocks drop down without any delay)
- CPU/OS-friendly, no tight loops, uses about 3-9% of CPU time with AMD 800 Mhz
- 100% Java-exception free during program run
- Threadsafe, value of variable 'N' can changed at any time in method 'keyDown(Event,int)' without problems
- No parameters passed to applet from html-page


Comparing to others short Tetris-games

Actually, there's no need to compare this program to any other program since when I started to do Shortris I didn't have goal to make it smaller than some other same kind of game. Only goal was my own 1.000 bytes limit. So this was just challenging, fun(?) and good practising of logical operations and bit shifts ;-)

Also, comparing this kind of games is quite difficult because different programming languages and different game and technical features. To be able to compare which program is shortest, there should be exact rules what those games should implement and what they doesn't need.

Afterward I've seen Java-Tetris in less than 800 bytes but that game didn't have for example borders around game area, game wasn't restartable (applet had to be loaded again to play new game) and difficulty didn't increase during game. So game was smaller but didn't have all the same functions than Shortris.

(Shortris itself or any part of it isn't based to any other Tetris!)




Comments? Something to ask? Feel free to send mail.




Java is a trademark of Sun Microsystems, Inc.




www.tiikoni.net/shortris/ Full page map Copyright © Pasi Laaksonen