Afterglowscreensavers

Tetris AI Screensaver

Tetris AI is a free screensaver in which a bot plays a real game of Tetris on the 10×20 board: it scores every rotation and column with a four-term heuristic, draws its chosen target as a ghost and drops the piece there. It is an honest, imperfect player that eventually tops out and restarts. Press ESC to exit.

Live preview

Tetris AI screensaver — A heuristic bot plays real Tetris until gravity beats it.

Press ESC to leave fullscreen. Settings are saved in your browser only.

How the Tetris AI screensaver works

The engine is ordinary Tetris. Pieces come from a 7-bag randomiser (all seven tetrominoes shuffled, dealt, then reshuffled), each piece has four rotation states, full rows clear, and a level rises every ten lines. Gravity follows a guideline-style curve, starting at 0.8 seconds per row at level 1 and falling to a few milliseconds per row by the high teens. Scoring is 100, 300, 500 or 800 points times the level for one to four lines, plus one point per row of soft drop.

The bot is the classic one-ply heuristic published by Yiyuan Lee in 2013. For every rotation and every column the piece can reach, it drops the piece on a copy of the board, clears any full rows, and scores the result as −0.510066 × aggregate height + 0.760666 × lines cleared − 0.35663 × holes − 0.184483 × bumpiness (the sum of height differences between neighbouring columns). The best score wins. This version looks only at the current piece, not the next one, so the next-piece box is for you.

The bot does not teleport pieces. It presses keys at a limited rate, one rotation or shift per tick, while the piece falls under real gravity and then soft-drops once it is in position. Early on that is plenty. At higher levels gravity outruns its fingers, pieces lock before they reach their target, holes appear, and the stack climbs. The "fell off plan" counter in the side panel counts those pieces. Nothing is rigged: the same rules and the same bot run every game, and a top-out simply starts a new game.

The side panel shows the four feature values and weights for the placement the bot has chosen, the number of placements it checked, and a running pieces-per-second figure measured from the game clock. On small tiles the panel is hidden and only score, lines, level and the next piece remain.

About the heuristic

The four-feature evaluation used here was popularised by Yiyuan Lee's 2013 blog post "Tetris AI – The (Near) Perfect Bot". He chose aggregate height, complete lines, holes and bumpiness as features, tuned their weights with a genetic algorithm, and reported a bot that played for a very long time. The weights in this saver are the ones from that post.

Lee's own bot looked one piece ahead; this saver does not, and it adds a limited key rate and real gravity, which the original experiment did not impose. So the play you see is not a reproduction of his results, and no score or line record is claimed here.

Tetris was created by Alexey Pajitnov in 1984. This saver is an independent game engine written for the site; it uses no code, art or sound from any commercial Tetris product.

A worked example

In the preview frame the bot has chosen a placement with aggregate height 18, no completed line, no holes and bumpiness 7. Its score is −0.510066 × 18 + 0.760666 × 0 − 0.35663 × 0 − 0.184483 × 7 = −9.181 − 1.291 = −10.47, the figure shown in the panel. In six headless test games at the default speed this engine topped out between 140 and 152 lines (level 15 or 16).

Settings & tips

Frequently asked questions

Is this a perfect Tetris player?
No. It is a greedy one-piece heuristic with four fixed weights, no lookahead beyond the current piece, and a limited key rate. It plays cleanly for a long time and then loses when gravity gets too fast for it.
Where do the weights come from?
From Yiyuan Lee's 2013 write-up of his Tetris bot (codemyroad.wordpress.com), which found the weights for aggregate height, lines, holes and bumpiness with a genetic algorithm. They are used here unchanged.
Why does it eventually lose?
Because the simulated fingers are limited to a fixed number of inputs per second while gravity keeps speeding up with the level. Once a piece falls faster than the bot can move it, it locks in the wrong place. The bot is not told to fail; it simply runs out of time.
Is the game rigged or scripted?
No. Pieces come from a random 7-bag, the bot decides every placement from the live board, and failures are left to happen. Different games end at different levels.
What do the numbers in the side panel mean?
They are the four features of the placement the bot picked, each multiplied by its weight, and the sum is the bot's score for that placement. The panel also shows how many placements it checked and how many pieces landed off its plan.
Does the bot use the next piece shown?
No. It scores only where the current piece can go. Using the next piece as well would make it stronger, but this saver keeps the simpler one-piece version.
Does it use much CPU?
Very little. Choosing a placement means scoring at most a few dozen candidate boards per piece, and the board is only 200 cells.

← All Arcade screensavers