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'.

This definition should punish bad behavior and reward good behavior. In this case good behavior is placing a block as low as possible (the game ends when reaching the top, after all). Bad behavior is doing so while leaving a covered hole that a simple, block dropping AI can't fill.

But, of course, we don't want to stack forever waiting for a certain piece, but will prefer making a hole when we build too high on the sides.

With that in mind, I did the following: for each non-empty square (x,y) from the bottom, add (height-y)^3 (so 30^3 for each block in row 1, 29^3 for each in row 2), then subtract 10000 for each hole.

Interestingly, 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.

Obvious enhancements would be to avoid double parking on a covered hole, since it's easier to clear one than two rows, and to modify the weighting so that it doesn't start stressing when it gets taller, since holes are relatively much worse than any gains you get by placing a piece lower.

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