Simple, Silly Tetris AI

Tetris, Alexej Pajitnov's main claim to fame (apart from elfish, if you ask me), doesn't need an introduction. But here's a little about an AI I wrote, that with any luck is puttering along somewhere to the right.

I had never written a game of Tetris before, which was kind of strange because everyone and their garden pond has written one at some point. Normally I wouldn't waste my time on it (I have other things to waste my time on), but I wanted to write some game AIs, so here we are.

When a piece drops, it simple goes through all rotations and horizontal movements of these to find the place where it's best to drop, for some definition of best. In this case is the total mass of the board where the weight of each square is the cubed distance from the top, minus a big penalty for each covered hole.

That didn't say much, I suppose. Let me try again: for each non-empty square (x,y), sum up y^3. Subtract the number of empty squares with a filled square over it (ie, they are unreachable by dropping blocks).

Strangely, this produces a fairly non-embarrasing AI. It's terrible in diseased cases like with just S/Z-blocks or after a tetrinet block bomb, but for a plain game, it can go on for quite a while.

The reasoning behind this approach is that we want to prefer to fill lower positions (larger y). However, we don't want to create a holes either (making a line untakable) and prefer building a bit of height. With this approach, it figures out that it should take as many lines as possible (because that means the piece fills more lines lower), but not if it means taking half of a 4 combo (because that would leave a hole). If forced to build too high, it will prefer making a hole so as not to build itself to death. It's all pretty nifty unless you try another block distribution or something.

If you want to take a look at my uncommented source code, get it here.

PS: Don't bother waiting for it to die, it just starts over.

Back to other junk by Vidar