Search⌘ K
AI Features

Creating and Running the Tetris Game

Explore how to create and run a Tetris AI agent using genetic algorithms in Elixir. Understand encoding actions, defining fitness functions, setting termination criteria, and executing the evolutionary algorithm to improve gameplay agents. This lesson helps you visualize genetic algorithm results and apply them to game AI development.

Creating a tetris agent

Before we can get started evolving Tetris agents, we need to download a copy of an Atari 2600 Tetris ROM. Fortunately, the ALE repo offers the official Tetris ROM. Atari 2600 ROMs are also available from a number of websites. If the contents of a ROM are of concern, the checksums of supported ROMs are accessible here.

Note: This has already been done for you in this course.

Once tetris.bin is downloaded, create a new folder named priv and place tetris.bin into it. Next, in tetris.exs, create a new problem:

C++
defmodule Tetris do
@behaviour Problem
alias Types.Chromosome
@impl true
def genotype, do: # ...
@impl true
def fitness_function(chromosome), do: # ...
@impl true
def terminate?(population, generation), do: # ...
end

Now let’s get started encoding the problem.

TheTetris AI will need to perform a series of actions that change the game state. Recall that Tetris is a game where tiles fall into the playing field until they stack on another tile or hit the bottom. If the tiles are stacked such that they create a horizontal line filling the entire playing field, all of the blocks on the horizontal line disappear, and the player is awarded points.

In Tetris, the player can choose to move left or right or rotate a tile. They can also choose to speed up or slow down ...