Tetris AI

Tetris AI Bot using computer vision to play game automatically

  • bot.py – main application.
  • criteria.py – AI part, includes fitness function and attributes.
  • recognition.py – Computer Vision part.
  • tetromino.py – some information about tetrominoes (shape, color, start position).

Watch the video

click on the picture to see demo

Computer Vision

How it works?

When bot started he try to find game aplication and wait until game is started. When game is started bot detectes the board, the tetrominoes in the game using Opencv. Grabs the images in realtime and transfroms board to bollean matrix (0 – empty cell , 1 – occupied cell) and identify current tetromino…

AI algorithm

When all information about the board is known, AI determines all possible positions of the current tetromino on the board. Choose the best possible position via fitness function and transmits all the necessary moves by emulating keystrokes on the keyboard.

Fitness function parameters:

  • Height – maximum height of the grid.
  • Aggregate Height – the sum of the height of each column.
  • Number of lines cleared – the number of complete lines in the grid.
  • Number of holes – empty space such that there is at least one tile in the same column above.
  • Bumpiness – sum of the difference between heights of adjacent pairs of columns.

How to try

Work only on Windows OS

alt text

  • Download bot.exe from dist folder
  • Configurate game options

alt text

  • Run bot.exe
  • Start new Game
  • Enjoy:)

alt text

GitHub

View Github