Tiny Tetris

by Alistair Turnbull

If your web browser understood Java applets, there would be a Tetris game here.


This is an attempt to write the shortest possible Java program that plays tetris. There are a variety of keys that work, none of them particularly convenient. D, F, G and Y work tolerably for me.

The idea came from Rheolism, an extraordinary one-line BBC Basic program written for a bet. I've stolen most of the techniques from it, but many needed modifying. In particular, all the nice coincidences regarding the numbering of the VDU codes do not carry over into Java. Oh, and I had to write a random number generator. I've had a go at redesigning the original authors wonderful bit-twiddling to compensate, but I'm sure there are plenty of tricks I've missed.

There's also the issue of fighting with the Java API. For example, there are 50 characters in the line 'g.setColor(new Color(255 * (b[i]*16513 & 65793)));' which you can save if you find another way of redrawing the screen - it's not easy because colours are not numbered in Java! Also, I've used some deprecated Java 1.0 API to read the keyboard. Maybe there's a better way?

So far, I've omitted score and gradual speed-up.

Please feel free to contribute improvements.

Source code

Version 1 (901 bytes) and my thoughts while designing the piece encoding.

The following may be useful for squashing programs. I'm sure there's a better way of doing this, but this is my first use of Perl.

perl -pe "s://.*$::g" # Remove // comments.
perl -pe "s:\n: :g" # Concatenate lines.
perl -pe "s:/\*([^*]*\*[^/])*[^*]*\*/::g" # Remove /* */ comments (ouch!)
perl -pe "s:(?<=int\s)([^;]*);\s*int\s:\1,:g" # Merge int declarations.
perl -pe "s:\s\B::g" # Remove spaces that do not precede words.
perl -pe "s:\B\s::g" # Remove spaces that do not follow words.