Update: Full source code and implementation now available here!
This algorithm is part of a project I was working on last term as part of my Artificial Intelligence class. This algorithm, which I will be referring to as “El-Tetris”, is an algorithm for playing Tetris by inspecting only one piece at a time (as opposed to two or three pieces in some variations of the game). It is based on Pierre Delacherie’s Tetris algorithm, which is known as one of the best one-piece Tetris playing algorithms.
Before discussing the details of the algorithm, let’s briefly look into how you, a human player, would play tetris.
When you play tetris, you are faced with two decisions every time you are given a tetris piece:
- Where to position the piece
- Which orientation of the piece to play
Naturally, you want to eliminate as many rows as possible and maximize your score. To accomplish that, you would (subconsciously) be doing the following:
while the game is not over: examine piece given find the best move possible (an orientation and a position) play piece repeat
What would be the best possible move then? You would usually try to eyeball certain features to help you determine that. You might, for example, ask yourself these questions:
- If I were to play the move, would that create holes in the game board?
- If I were to play the move, how many rows would I clear?
- If I were to play the move, what would be the height of the highest column?
- … and so on
That’s exactly what El-Tetris does. For every given piece, it evaluates every possible orientation and position against a set of features. The move with the best evaluation is the one that is played.
