Developer

2048 AI

An AI agent that plays the 2048 game using strategic algorithms.

Timeframe
July 2021
Stack
Python3 · Algorithms · Object-Oriented Programming
  • AI agent that plays 2048 strategically
  • Game logic implementation
  • High score achievement through algorithmic play

Project Overview

2048 is a tile/board game in which the user moves tiles back, forth, and vertically around a 4x4 grid to create the number 2048. The numbers go up by 2^n, where n is the amount of merges. So to get 8 you have to merge 3 times: 2+2=4, 4+4=8. With this project I decided to recreate the game, as well as create an AI for it. The AI uses Monte Carlo Simulation to quickly get to 2048.

2048 AI in action

Monte Carlo Simulations

Monte Carlo Simulations are used to find the best move in 2048. The Monte Carlo method is the idea of using a large number of random simulations of an experiment to gain insights into the experiment's end results. In other words, Monte Carlo simulations are a way to estimate what will happen in a given experiment without having to implement any specific algorithms.

To implement this into 2048, I would cycle each move [left, right, up, down] for the amount of simulations given. So if 200 simulations were given, each move [left, right, up, down] would be simulated 50 times. After each move, I would then have the simulation move randomly until the game was over. So it would simulate 200 games before making a move, and score each first move and then at the end return the best first move, which would be the move in the actual game.

Game Implementation

For the game I used pygame and Tkinter. Pygame was for the actual game and Tkinter was for the UI to choose whether to simulate or play the game. I created a Block class which was the super class of Board. To make it simpler I shifted a list in Python and then updated the GUI based on the board.

Here's the code for shifting left, which demonstrates the core game logic:

def shift_left(self):
    for i in range(len(self.board)):
        v=[]
        w=[]
        for j in range(len(self.board[0])):
            if(self.board[i][j] != 0):
                v.append(self.board[i][j])

        j=0
        while(j < len(v)):
            if(j < len(v) - 1 and v[j].size == v[j+1].size):
                v[j].set_size(v[j].size*2)
                w.append(v[j])
                self.score += v[j].size
                j+=1
            else:
                w.append(v[j])
            j += 1
            
        for j in range(len(self.board)):
            self.board[i][j] = 0
        j=0
        for it in w:
            self.board[i][j] = it
            j+=1

The code processes each row by collecting non-zero tiles, merging adjacent tiles with the same value, and then placing them back in the row from left to right.

Here's what the game looks like:

2048 Game Interface

I also created a dark mode for the GUI:

2048 Dark Mode

Future Plans

  • Add other themes for further customization