Tetris.← Play

Inside the AI

How does the AI
choose a move?

A piece can fit in several places. The AI’s job is to choose the placement that helps it clear lines while leaving room for the pieces that come next.

  1. 01GeneratePossible landings
  2. 02EvaluateResulting boards
  3. 03ChooseOne target placement

1. Balance the next line against the next piece

Filling a row clears it and frees space. But a move that clears a line can also leave a tall stack or trap empty cells beneath it. Those problems make later pieces harder to place.

The AI therefore judges both the immediate benefit and the shape of the board it leaves behind. Its goal is to clear as many lines as possible before the stack reaches the top.

2. Imagine possible landings

The AI starts with the settled blocks and the current falling piece. For each distinct orientation, it tries different columns and simulates dropping the piece until something stops it.

Placements that cross a wall, overlap a block, or leave part of the landed piece above the board are discarded. Each remaining placement produces a candidate board: a copy of the current board with that piece added. The actual game has not moved yet.

The AI measures each candidate before completed rows disappear. That lets it count the lines the placement would clear, along with the structure of the stack.

3. Describe a board with a few numbers

A feature is a numerical measurement of a board. Instead of judging every cell separately, the basic AI uses four measurements: height, completed lines, holes, and unevenness.

A hole beneath the stack A five-column board. An outlined empty cell is trapped below a filled cell; the top of the stack is uneven.

The outlined cell is a hole: a block above it prevents a piece from dropping straight into it. Reaching that cell may require clearing the blocks above it first. The uneven top surface also restricts where future pieces can fit.

The four basic features

4 features

A column’s height is the distance from its highest filled cell to the bottom, including any holes beneath it. The weights will turn these measurements into a score in the next step.

Basic features and signed scoring weights
FeatureMeasuresWeight
Total heightSum of column heights; empty columns count as zero.−0.434
Complete linesNumber of full rows.+0.238
HolesEmpty cells below the top filled cell in each column.−0.247
BumpinessSum of absolute height differences between neighboring columns. Heights 2, 5, 3 contribute 3 + 2 = 5.−0.081

4. Turn the measurements into a score

A weight controls how strongly a feature affects the decision. Completed lines have a positive weight. Height, holes, and bumpiness have negative weights, so increasing them lowers the score.

Score = −0.434 × height + 0.238 × lines − 0.247 × holes − 0.081 × bumpiness

For each candidate, multiply the four measurements by their weights and add the results. The highest score wins. Scores can all be negative: −8 is better than −10.

A tradeoff in numbers

Consider two hypothetical candidate boards with the same total height:

Example of scoring two candidate boards
CandidateHeightLinesHolesBumpinessScore
A20126−9.422
B20002−8.842

For A: −8.680 + 0.238 − 0.494 − 0.486 = −9.422. For B: −8.680 − 0.162 = −8.842.

The AI chooses B. With these weights, avoiding two holes and leaving a smoother surface outweighs clearing one line now. Different weights could change that decision.

After choosing, the AI rotates and moves toward its target, then drops the piece. The game locks it, clears any full rows, and supplies the next piece. The same decision process starts again.

5. Look more closely at the board

8 features

The advanced AI keeps the same search-and-score process but replaces the four measurements with eight. It distinguishes shallow holes from deeply buried ones, notices fragmented surfaces, and measures how much of the current piece contributes to cleared lines.

Advanced features and signed scoring weights
FeatureMeasuresWeight
Landing heightBoard height minus the landed piece’s anchor row.−12.63
ErosionCompleted rows × cells of the current piece in those rows.+6.60
Row transitionsFilled/empty changes across nonempty rows, including filled side walls.−9.22
Column transitionsFilled/empty changes down columns, with filled top and bottom boundaries.−19.77
HolesThe same buried empty cells as in the basic scorer.−13.08
Cumulative wellsOpen vertical gaps bounded on both sides. A depth of 3 contributes 1 + 2 + 3.−10.49
Hole depthsSum of the number of filled cells above each hole.−1.61
Lines with holesNumber of rows containing at least one hole.−24.04

A transition is a change from empty to filled, or filled to empty. More transitions indicate a fragmented row or column. A well is different from a hole: it is open above, so a piece can still fall into it. Summing its successive depths gives deeper wells a larger penalty.

Each advanced measurement is multiplied by the signed weight shown above, and the terms are added just as in the basic scorer. More detail can help the AI distinguish boards that look equally good under the four basic measurements; it does not guarantee a better choice every time.

6. Understand the hints and the limits

With hints enabled, the outlined piece shows the landing preferred by the selected scorer. Switching features can change the hint because the same candidate boards are judged by different measurements and weights.

Comparison mode gives the basic and advanced AIs the same sequence of pieces. Their decisions produce different boards, and eventually different totals of cleared lines. A longer run or a different sequence can produce a different outcome.

This AI plans one placement at a time. It knows the current piece but has no preview of the next one. Its features estimate whether a board will be useful later; it does not simulate several future pieces to prove that.

It also searches straight-drop landings rather than every movement path. A desirable landing may become unreachable while the piece falls. The score is a practical guide to a good move, not a promise of the best possible game.