For our TIPE (personal project in CPGE), I built a tetris game in C++ and different AIs to learn to play the game. In the end, the goal is to compare the results of the different AIs.
- Clone the repository
- Install MLX library. You can use
brew install mlxon macOS. - Run the following commands:
cmake -B build .
cmake --build build
./build/tetris-ai -m 3Quick note about the parameters:
-m 0is for training a simple genetic mutation without neural networks-m 1is for training a RL algorithm-m 2is for training a Genetic Neural Network-m 3is for using a custom heuristic to play the game
Tetris is an arcade game created by Alexey Pajitnov in 1984. The game consists of a matrix of 10x20 cells, where the player has to place the falling pieces in a way that they form a line. When a line is formed, it disappears and the player scores points. The game ends when the pieces reach the top of the matrix.
You can check the scores in the scores/ folder.
Our genetic algorithm is based on the following steps:
- Create an initial population of random individuals, with random weights
- Evaluate the fitness of each individual, by making them play 1 game of tetris
- Sort the individuals by fitness
- Eliminate the worst individuals (1/2 of the population)
- Create new individuals by crossing over the best individuals (choosing random weights from the best individuals to create the new ones)
- Repeat step 2 to 6
Our RL algorithm is based on the following steps:
- Create a NN
- Define a heuristic function that evaluates the state of the game
- Play the game and randomly store states in a replay buffer
- Once each game ends, train the NN with the states stored in the replay buffer for 10 epochs
Same as the basic genetic algorithm but the individuals are neural networks. Currently this is the best we have. Managed to get a score of 1084 lines cleared within 7 minutes of training in boost mode. For reference, I scored 180 and gamers can score around 220.
Our max score was around 2500 (in roughly 10 minute of training).