tetris-ai

command module
v0.0.0-...-9e740c1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2023 License: MIT Imports: 4 Imported by: 0

README

tetris-ai

An algorithm that plays Tetris better that you, visualized.

Watch as the AI drops a few dozen tetrominoes per second!

  • Based on Minimax
  • Supports Android, GUI and CLI.

Supports easy extending with

  • different user interfaces
  • different AI algorithms

Installation

OS X Linux
go get github.com/ozhi/tetris-ai
sudo apt install \
  libglu1-mesa-dev \
  libgles2-mesa-dev \
  libxrandr-dev \
  libxcursor-dev \
  libxinerama-dev \
  libxi-dev \
  libasound2-dev

go get github.com/ozhi/tetris-ai

Running

Android GUI CLI
gomobile build go run main.go go run main.go -cli
screenshot-android.png screenshot-gui.png screenshot-cli.png
Press the buttons Press the buttons or <space> - drop next, A - automatic mode Automatic mode only

Documentation

Code documentation on godoc.org/github.com/ozhi/tetris-ai.

Tetris-AI has four packages:

  • tetris contains structs and behaviour of the basic components of the tetris game - the board and tetromino.

  • ai contains the artificial intelligence that plays tetris.

    For each move, it searches the state space of the game - all possible board states that can be reached after dropping the current and next tetromino. Each of those states is evaluated and the "best" is chosen.

    The AI evaluates boards using the minimax algorithm - how "good" will the state be even if the next tetromino happens to be very "bad".

    How "good" a board is is determined with a utility function that takes in to account:

    • the number of lines cleared in the game (more is better)
    • the aggregated height of the columns (less is better)
    • the 'bumpiness' of the board (difference in column heights) (less is better)
    • the number of 'holes' in the board (less is better)

    Each of these is taken with a different coefficient, chosen with trial and error.

    In order for the AI to play fast enough (tens of tetrominoes every second), the alpha-beta pruning optimization is used to reduce the size of the state tree to be searched.

  • gui contains the graphical user interface of the app.

    It uses the ebiten library for visualizations.

  • cli contains the command-line interface of the app.

    It is simple and boring.

License

Tetris-ai is licensed under MIT license. See LICENSE.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal
ai
Package ai contains artificial intelligence that plays tetris.
Package ai contains artificial intelligence that plays tetris.
cli
Package cli contains the command-line interface of Tetris-AI.
Package cli contains the command-line interface of Tetris-AI.
gui
Package gui contains the graphical user interface of Tetris-AI.
Package gui contains the graphical user interface of Tetris-AI.
tetris
Package tetris contains structs and behavior of the basic components of the tetris game - Board and Tetromino.
Package tetris contains structs and behavior of the basic components of the tetris game - Board and Tetromino.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL