A simple AI solver for the game 2048 written in Python
The puzzle game 2048 is a fun and addicting game involving the combination of instances of the same number to form 2048. The original game can be found here: https://gabrielecirulli.github.io/2048/
The file PlayerAI.py contains the main logic for the AI solver. The AI uses a minimax algorithm with alpha-beta pruning. The default search depth is 4 layers.
For heuristics, two are currently used, this part can still be improved greatly.
The first heuristic uses a snake score. That is the number in the corner is assigned the biggest weight. The weighting descends as it moves along each cell in a snake-like manner. A bonus score is awarded if the highest value cell is in the corner. The total sum of (cellValue*weight) is the snakeScore.
Another heuristic is an empty tiles heuristic. The system awards higher points to board states with more empty cells.
The gradient score is similar to the snake score, but instead of weighting based on a snake-like manner, weighting is determined strictly by the distance from the corner. In practice, this heuristic performed a lot worse than the snake score. I have kept the code here as it has room for improvement.
The total score is the sum of the snake score and empty tile score.
The AI can reach 2048 about 30% of the time. The best score I have obtained is 4096 + 2048. There is room for improvement by using a deeper search tree (though this might impractical in Python). Other heuristic might also give better performance.
To run, type the command: $ python GameManager.py