# How to build 2048 game in Python

In this tutorial, we will build the 2048 game in Python. We will go through the different moves during the build of the game.

*I will use 4×4 matrix to explain this game in this context*

### Some detail on the 2048 game

In this game initially, we have been provided with two 2’s at random place in the matrix.

When moving up, down, left or right, it will merges with the only similar numbers present in its path.

For example, below is the screenshot image of the game:

After this, if we press key ‘W’ or moves up, we will go through the following changes:

- 4’s at (0,0) and (1,0) merges to 8.
- 2 at (2,0) moves to (1,0).
- 16’s at (1,1) and (2,1) merges to 32 as shown.
- no change in 8.
- 2 at (2,3) moves to (2,1)

This will result in the following matrix:

#### I would recommend you to play this game by yours 3-4 times to understand it thoroughly.

Let us first go through different functions to be used, I will explain them afterwards :

#### Compress:

def compress(mat): new_mat=[[0 for i in range(4)] for i in range(4)] for i in range(4): pos=0 for j in range(4): if mat[i][j]!=0: new_mat[i][pos]=mat[i][j] #This compress function is for lest move. pos+=1 return new_mat

#### Merge:

def merge(mat): for i in range(4): for j in range(3): if mat[i][j]==mat[i][j+1] and mat[i][j]!=0: mat[i][j]+=mat[i][j]. #This merge function is for left move. mat[i][j+1]=0 return mat

#### Reverse:

def reverse(mat): new_mat=[] for i in range(4): new_mat.append([]) for j in range(4): new_mat[i].append(mat[i][3-j]) return new_mat

#### Transpose:

def transp(mat): new_mat=[[0 for i in range(4)] for i in range(4)] for i in range(4): for j in range(4): new_mat[i][j]=mat[j][i] return new_mat

#### These four functions are majorly used in the whole game.

In simple words, the compress function finishes the gap between the required numbers, or make the important numbers, on which we want to perform further actions, close.

Merge function, merges the similar numbers present comparing next matrix.

Reverse and transpose functions reverses and transposes matrix, simultaneously.

Also, read: How To Convert Image To Matrix Using Python

Initially merge and compress functions are coded for the left move, we can use the same function for different moves using reverse and transpose functions. Various functions for various moves are as follows:

#### Left Move:

- Compress
- Merge
- Compress

#### Right Move:

- Reverse
- Compress
- Merge
- Compress
- Reverse

In this by reversing and re-reversing make a matrix to deal it like Left move.

#### Up Move:

- Transpose
- Compress
- Merge
- Compress
- Transpose

Transpose function makes matrix work like Left move, afterwards transposing it again to work flawlessly.

#### Down Move:

- Transpose
- Reverse
- Compress
- Merge
- Compress
- Reverse
- Transpose

Transpose and reverse make matrix work like the left move.

This is how we code for different moves.

This is how the logic part works for the 2048 game.

## Some more changes

After every work is done on this game, or whenever compress or merge function does some work, a new 2 at random place will be added to our game.

Changes in code are as follows:

def merge(mat): for i in range(4): for j in range(3): if mat[i][j]==mat[i][j+1] and mat[i][j]!=0: mat[i][j]+=mat[i][j] mat[i][j+1]=0 change=True #change is true if this if function processes return mat

def compress(mat): new_mat=[[0 for i in range(4)] for i in range(4)] for i in range(4): pos=0 for j in range(4): if mat[i][j]!=0: new_mat[i][pos]=mat[i][j] #row(i) of new_mat and mat are same, if columns are different, this implies work has been done. if j!=pos: change = True pos+=1 return new_mat

Complete Logic code for this game is :

import random def start_game(): return [[0 for i in range(4)] for i in range(4)] def reverse(mat): new_mat=[] for i in range(4): new_mat.append([]) for j in range(4): new_mat[i].append(mat[i][3-j]) return new_mat def transp(mat): new_mat=[[0 for i in range(4)] for i in range(4)] for i in range(4): for j in range(4): new_mat[i][j]=mat[j][i] return new_mat def merge(mat): for i in range(4): for j in range(3): if mat[i][j]==mat[i][j+1] and mat[i][j]!=0: mat[i][j]+=mat[i][j] mat[i][j+1]=0 return mat def compress(mat): new_mat=[[0 for i in range(4)] for i in range(4)] for i in range(4): pos=0 for j in range(4): if mat[i][j]!=0: new_mat[i][pos]=mat[i][j] pos+=1 return new_mat def moveLeft(arr): st1=compress(arr) st2=merge(st1) st3=compress(st2) return st3 def moveRight(arr): st0=reverse(arr) st1=compress(st0) st2=merge(st1) st3=compress(st2) st4=reverse(st3) return st4 def moveUp(arr): st0=transp(arr) st1=compress(st0) st2=merge(st1) st3=compress(st2) st4=transp(st3) return st4 def moveDown(arr): st0=transp(arr) st=reverse(st0) st1=compress(st) st2=merge(st1) st3=compress(st2) st3=reverse(st3) st4=transp(st3) return st4 input=list(map(int,input().split())) arr=start_game() arr[1][3]=2 #initial array setup, choosen random numbers at random places. arr[2][2]=2 arr[3][0]=4 arr[3][1]=8 arr[2][1]=4 for i in input: if i==1: #move Up arr=moveUp(arr) elif i==2: #move Down arr=moveDown(arr) elif i==3: #move left arr=moveLeft(arr) elif i==4: #move right arr=moveRight(arr) print(arr)

while giving input: 1 2 3 4

we will get the output : [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 4], [0, 4, 8, 4]]

## Leave a Reply

You must be logged in to post a comment.