Skip to content

Using python I created a text user interface for the popular game 2048 and wrote an algorithm that beats the game ~30% of the time.

Notifications You must be signed in to change notification settings

zroe1/2048-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Beating 2048 with python

This project contains two parts:

  1. 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.
  2. 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).
In this README.md file, each of these components are explained in detail along with information about how to run the code on your own machine, and what you should expect to see if everything is working properly.

How the AI works

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.

Running the code

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:

Screenshot 2023-06-04 at 12 06 18 AM

Running my 2048-beating Algorithm

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:

Screenshot 2023-06-04 at 12 20 21 AM

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!

About

Using python I created a text user interface for the popular game 2048 and wrote an algorithm that beats the game ~30% of the time.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages