2048 Player


Minimax with a heuristic to play 2048

GitHub  ■  Methodology Blog Post

X

Algorithm Overview

This program uses an implementation of the Minimax algorithm with 5 ply to play 2048. The 5 ply is specifically used to have the AI make each move in under .1 seconds. Each possible board position is evaluated using a heuristic. This heuristic is weighted to prefer higher block values at the top compared the bottom, higher values on the left rather than the right, and higher block values in general. Furthermore, as the board begins to get filled significant preference is given to the number of empty squares.

PlayerAI.py contains the Minimax implementation and heuristic functions. All visualization and helper functions were provided by Dr. Ansaf Salleb-Aouissi. The handy embedded interpreter is provided by repl.it
 

2048 Rules

The premise of 2048 is to combine blocks containing multiples of 2 until you have a block of value 2048 or higher. Blocks are combined through collision and only equivalent blocks can be combined. Each turn, the Player inputs: up, down, left, or right – which causes all blocks to shift as far as possible in that direction. In addition, each move made by the Player spawns either a 4 or 2 on a random empty block. “Winning” is reaching a score of 2048. The game ends when no further moves are possible. Give it a shot! 2048 is more intuitive than it sounds.
 

Running Instructions

Click the Run Code button below. Register or close the modal. Click Play.
The AI will compete to win the 2048 game for about 5 minutes. You can see the selected move and the board position update for each move.

 


Leave a Reply

Your email address will not be published. Required fields are marked *