This project contains two parts:
- Recreating the popular game 2048 with python in the terminal with human readable output. The rules of the game are simple. You can move up, down, left, or right and the tiles move that direction and combine if an adjacent tile has the same value. The goal is to combine tiles until you get 2048. To try out the game visit https://play2048.co.
- Creating an algorithm that suggests moves that can ultimately beat the game. The algorithm I created beats the game about 30% of the time, which seems likely to be better than many humans. There are some things that could be done to improve the algorithm, but I suspect this is within an order of magnitude of what is possible without a widely less efficient algorithm or entirely different approach (e.g., neural net).
The AI that I implemented to play 2048 a symbolic AI that works similarly on a high level to the famous IBM computer Deep Blue which beat world chess champion Garry Kasparov in 1997: https://stanford.edu/~cpiech/cs221/apps/deepBlue.html
The approach the algorithm takes is it takes all possible sequences of three moves (excluding those that include moving up which is generally poor strategy) and then evaluates each sequence to see which will most likely leave the user in a favorable board position. After assigning point values to each board position, the algorithm chooses the most favorable from the tree of possibilities and makes the first move in the sequence. The process restarts for every move.
This section assumes that you have a newer version of python installed on your computer. Outside of that, no extra software is needed.
To run the text user interface for 2048 type the following command into terminal:
python3 TUI.py
This should allow you to play 2048 using nothing but your terminal and keyboard. The initial output when typing the command explains how to make moves using your keyboard. You should see something like this:
To run my 2048-beating algorithm, type the following command into terminal:
python3 simulategames.py
This runs 1000 games of 2048 (it takes some time) and displays the results of the games in terminal. The output should look something like this:
This gives you an idea of how the algorithm I wrote performs. As you can see, in this batch of games, the algorithm I wrote won 308 out of 1000 games and even got tiles above 2048 in 3 of the games played.
You can change the variable NUM_GAMES in simulategames.py to simulate less or more games. You can also change the method suggest_move in classes2048.py to try your own algorithm and see if you can beat the game. Happy hacking!