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.)
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. :-)
Update: I’ve now added my version below.
23 April 2008 by Ben 8 comments