2026ML · RL

Tetris DQL

A Tetris variant with value-weighted scoring and a Deep Q-Learning agent that learns whether to clear rows quickly or wait for high-value blocks.

Tetris DQL screenshot

Overview

TetrisDQL is a Tetris game with a modified scoring system, together with an AI agent that learns to play it using Deep Q-Learning (DQL). The project has two goals: implement a playable version of the game and train an agent that learns a good strategy for it without any hand-written rules.

Modified scoring

In this version every cell of a falling piece carries a random value from 1 to 9. When a row is cleared, the player receives the square of the sum of the values in that row.

This changes the strategy. Clearing rows as quickly as possible is no longer necessarily optimal: it may pay off to keep building and wait for high-value blocks, so that a single cleared row is worth much more.

To check whether the agent actually picks up this strategy, two scoring modes are available:

  • SQUARE_OF_SUM: the modified scoring described above
  • CONSTANT: classic scoring, where every cleared row is worth 1 point

A separate agent is trained for each mode. A comparison view runs both agents side by side, with both games scored using SQUARE_OF_SUM, so their results can be compared directly.

How the agent works

Instead of choosing individual key presses, the agent decides where the current piece should land. For every piece the environment generates all possible placements (every rotation and column) and simulates the resulting board.

Each resulting board is described by a set of features:

  • column heights
  • number of holes
  • bumpiness of the surface
  • number of cleared lines
  • values of blocks in columns and rows

A small neural network (two hidden layers of 128 and 64 neurons, implemented in PyTorch) estimates the value of each resulting board, and the agent picks the placement with the highest score.

Training follows the standard DQL setup:

  • ε-greedy exploration: the agent starts with random moves and gradually shifts to its own predictions
  • Replay buffer: past transitions are stored and sampled in batches, which breaks the correlation between consecutive moves
  • Target network: a separately updated copy of the network provides stable learning targets

Training runs on a GPU automatically when one is available, and both agents can be trained in parallel.

Usage

The project requires Python 3.10 or newer:

git clone https://github.com/CenturionTheMan/TetrisDQL.git
cd TetrisDQL
pip install -r requirements.txt

The game can be played manually (arrow keys to move, rotate and drop):

python src/main.py

A trained agent can be watched alone, or both agents can be compared side by side:

python src/agent/play.py
python src/agent/play_compare.py

Both agents can be retrained with:

./train_both.sh
View code on GitHub
Next project In-Context Learning vs. Fine-Tuning for NER →