Tetris, the new FizzBuzz

Tetris screenshotNo, 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)

Ben 23 Apr 2008, 22:09 link

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.

Joe Larson 24 Apr 2008, 03:28 link

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.

Ben 24 Apr 2008, 19:28 link

Cool — languages galore. So far from the reddit comments there’s a:

Keep them coming!

Update: just got another neat JavaScript version, so I might wait another couple of days for any final solutions.

Ben 1 May 2008, 20:26 link

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. :-)

Ben 2 May 2008, 10:25 link

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.

TimothyP 26 Jul 2008, 14:48 link

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 […]

Install Software 7 Feb 2010, 05:32 link

Another great post. Thanks for the tips and help. Everyone, bookmark this site.

Add a comment

We reserve the right to edit or remove your comment if it’s spammy, offensive, unintelligible, or if you don’t give a valid email address. :-)

We’ll never share your email address.