Purpose of this post

This post publishes the source code used for the Tetris AI. The code is loaded only when you open a file.

Models used

  • stable-baselines3 DQN: the low-level 7-action models (300,000 and 370,000 steps) and the macro 40-action model (300,000 steps). The model files are not included.
  • Qwen2.5-3B-Instruct GGUF q4_k_m (llama-cpp-python): text board judge, picking a candidate number
  • Qwen2.5-VL-3B-Instruct (transformers): image board judge, picking spots directly

Folder layout

  • tetris: C++ engine (tetris_engine), C API (tetris_c_api), Qt6 screen and TCP server (main_window, tetris_widget), CMake
  • local_tetris_env.py: gymnasium environment wrapping the engine with ctypes
  • simple_tetris_env.py: a pure Python environment used when the engine is not built. Its observation and action formats differ, so models cannot be swapped between them.
  • macro_planning_env.py: the 40-action environment that chooses once per piece
  • train_tetris_rl.py: DQN training without a screen
  • train_qt_live.py: shows the current model on the Qt screen while training. It also has a mode that trains on games played by a person.
  • qt_rl_client.py: attaches a trained model to the Qt screen
  • llm_teacher.py, vlm_teacher.py, llm_reward_wrapper.py: LLM and VLM reward judges
  • vlm_macro_client.py: client where the VLM picks spots directly
  • llm_macro_client.py: candidate computation and LLM number picking
  • moe_macro_policy.py: candidate selection mixing four rules
  • config/rl_config.py: training settings

Build outputs, virtual environments, model weights and trained model files are excluded.

Full source

25 files · 4,969 lines — open a folder and click a file to load its code.

Project root14 files
llm_macro_client.pypython · 894 lines · 33KB

Loading code…

llm_reward_wrapper.pypython · 96 lines · 3KB

Loading code…

llm_teacher.pypython · 128 lines · 4KB

Loading code…

local_tetris_env.pypython · 252 lines · 9KB

Loading code…

macro_planning_env.pypython · 152 lines · 5KB

Loading code…

moe_macro_policy.pypython · 81 lines · 3KB

Loading code…

project.mdmarkdown · 46 lines · 2KB

Loading code…

qt_rl_client.pypython · 148 lines · 5KB

Loading code…

requirements.txttext · 11 lines · 128B

Loading code…

simple_tetris_env.pypython · 154 lines · 6KB

Loading code…

train_qt_live.pypython · 726 lines · 28KB

Loading code…

train_tetris_rl.pypython · 316 lines · 11KB

Loading code…

vlm_macro_client.pypython · 236 lines · 8KB

Loading code…

vlm_teacher.pypython · 179 lines · 6KB

Loading code…

config/2 files
config/__init__.pypython · 2 lines · 55B

Loading code…

config/rl_config.pypython · 101 lines · 3KB

Loading code…

tetris/9 files
tetris/CMakeLists.txttext · 57 lines · 2KB

Loading code…

tetris/src/main_window.cppc++ · 408 lines · 13KB

Loading code…

tetris/src/main_window.hc++ · 58 lines · 1KB

Loading code…

tetris/src/main.cppc++ · 38 lines · 1KB

Loading code…

tetris/src/tetris_c_api.cppc++ · 167 lines · 6KB

Loading code…

tetris/src/tetris_engine.cppc++ · 446 lines · 13KB

Loading code…

tetris/src/tetris_engine.hc++ · 130 lines · 4KB

Loading code…

tetris/src/tetris_widget.cppc++ · 117 lines · 3KB

Loading code…

tetris/src/tetris_widget.hc++ · 26 lines · 536B

Loading code…