Tetris, the new FizzBuzz
No, I don’t really think Tetris is going to become the new FizzBuzz anytime soon. But a few days ago a colleague and I were talking about Tetris, and he mentioned that it’s apparently the most ported game out there. Not hard to believe — it’s fun, simple, and doesn’t require any fancy graphics.
Well, I’ve never actually written a Tetris clone. (I think you’re allowed to say that as a programmer.) Mainly I was curious how long it’d take. Things always take longer than you expect, so I guessed maybe half a day or a couple of evenings.
Turns out it wasn’t too tricky — I was having fun with the finished product in a short while (about two and a half hours). Almost half of that was spent on the “graphics” and keyboard input.
I wanted to time myself excluding writing the always-fiddly I/O, so what I did was start by writing four simple functions:
void setup(void); void teardown(void); void putsquare(int x, int y, int colour); int waitkey(void);
I wrote a fairly basic (but colourful) implementation of these for the Windows console, figuring that I really wanted to concentrate on writing the gameplay, not spend time on fancy graphics. (Half of my game projects as a teenager died because I started with the graphics, and keeping track of all those VGA registers just got too hard.)
So here’s the challenge: take my Tetris I/O header file and my Win32 implementation, and see how long it takes to write your own tetris.c
.
Or choose your favourite language and write your own versions of the I/O routines, and then time yourself writing the gameplay.
Email me your own version, and I’ll post it here along with mine in a couple of days. And no cheating, Googlers. :-)
Just for fun, for the version I like best I’ll give $15 for the author to pledge to a project of his choice over at microPledge, or a DecentURL premium subscription if you prefer.
Update: I’ve now added my version below.
23 April 2008 by Ben 8 comments
8 comments and pings (oldest first)
Heh, seems to be a tetris revival. I recently wrote one in Curses and make a few variations Okay, Unitris is a joke, okay. But Alley-tris seems to be doing very well and is even making me consider a variable width game.
The code will eventually become a part of my website, but I’m going to re-write the whole thing. It’s woefully spaghetti-like at the moment.
Cool — languages galore. So far from the reddit comments there’s a:
- C version
- Python version
- JavaScript version
- Ruby version (pic) that was written before my blog entry
Keep them coming!
Update: just got another neat JavaScript version, so I might wait another couple of days for any final solutions.
I hereby award the $15 microPledge voucher to nostrademons for his JavaScript version. It looks like a decent implementation, but it also works cross-browser (which often isn’t trivial), uses plain CSS, and doesn’t use any fancy JS libraries.
Must say that I had a hard time choosing, because none of the solutions were bad. Special mention goes to Laurie Cheers for his 55-minute version, Jon Palin’s neat two-game version, and flen_paris because he did it in Python. :-)
Oh, almost forgot to post my version. Nothing too elegant, just straight-forward code. It’s made up of tetris.c and pieces.c, and you’ll also need the I/O header file and Win32 implementation, tio.h and tio_win.c.
Or just download the whole lot as a .zip file, including a ready-to-go Windows executable (6 KB).
To compile this, I used TCC with the command line tcc tetris.c pieces.c tio_win.c -lkernel32
. But it should work fine under VC++ or MinGW or whatever.
Hi,
I have a C# version over at http://code.google.com/p/netris/ and am currrently porting to Silverlight 2 :-)
It was originally a GDI+ version, then ported it to SDL.NET
[…] Tetris is the new FizzBuzz, I have to write an implementation of this game, too. At the moment it does not work very well, but […]
Another great post. Thanks for the tips and help. Everyone, bookmark this site.
By the way, I forget to add that I used Fabrice Bellard’s superb Tiny C Compiler for this. I must say I’m impressed with TCC: fast, small, and the -run “C script” option is great.