Make a 2048 game in python – 2048 game codes in python

- - Tutorials

2048 Game in Python

By the end of this read you will be able to make a popular game 2048 on your own. Python programming language will be used to implement the logic of the game in this tutorial.2048 is a game that reached maximum popularity in my college. This started with an email from my college’s director of academics with the header 2048 : A game to make. I choose python programming language to build 2048 game.

2048 game is confined within a 4*4 matrix. This is the basic thing you need. Nested lists would be the quick choice to implement a 4*4 matrix in python. Next up the numbers within the 4*4 matrix are even numbers starting from 2. The combination of same numbers will result in their sum. If you succeed to make a single block of 2048 then you win. You have four valid moves namely left, right, down and up. Every time you make a move(left, right, up, down) a new number(2) is generated at one of the positions within the 4*4 matrix where the value is zero(nothing). If there are three same numbers in the format as shown below(for example), and left move is made then the number closer to the extreme positions has a higher precedence then that present somewhere middle of the matrix.

Taking only a single row as an example

2 2 2 16

When left move is activated, the above row becomes

4 2 16 0

2048 game codes in python

import random
myArray = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
inducerlist = [0,1,2,3]
myArray[random.choice(inducerlist)][random.choice(inducerlist)]=2
def users_choice(myArray,user_input):
    if user_input == "u":
        i=0
        for j in range(0,4):
            if myArray[i][j]!=0 or myArray[i+1][j]!=0 or myArray[i+2][j]!=0 or myArray[i+3][j]!=0:
                if myArray[i][j]==0:
                    while myArray[i][j]==0:
                        myArray[i][j]=myArray[i+1][j]
                        myArray[i+1][j]=myArray[i+2][j]
                        myArray[i+2][j] = myArray[i+3][j]
                        myArray[i+3][j]=0
                if myArray[i+1][j]==0 and (myArray[i+2][j]!=0 or myArray[i+3][j]!=0):
                    while myArray[i+1][j]==0:
                        
                        myArray[i+1][j]=myArray[i+2][j]
                        myArray[i+2][j]=myArray[i+3][j]
                        myArray[i+3][j]=0
                if myArray[i+2][j]==0 and (myArray[i+3][j]!=0):
                    while myArray[i+2][j]==0:
                        myArray[i+2][j]=myArray[i+3][j]
                        myArray[i+3][j]=0
        i=0
        for j in range(0,4):
            if myArray[i][j]==myArray[i+1][j]:
                myArray[i][j]=myArray[i][j]+myArray[i+1][j]
                myArray[i+1][j]=myArray[i+2][j]
                myArray[i+2][j]=myArray[i+3][j]
                myArray[i+3][j]=0
            if myArray[i+1][j]==myArray[i+2][j]:
                myArray[i+1][j]=myArray[i+1][j]+myArray[i+2][j]
                myArray[i+2][j]=myArray[i+3][j]
                myArray[i+3][j]=0
            if myArray[i+2][j]==myArray[i+3][j]:
                myArray[i+2][j]=myArray[i+2][j]+myArray[i+3][j]
                myArray[i+3][j]=0
                        
                        
          
    elif user_input == "d":
        i=0
        for j in range(0,4):
            if myArray[i][j]!=0 or myArray[i+1][j]!=0 or myArray[i+2][j]!=0 or myArray[i+3][j]!=0:
                if myArray[i+3][j]==0:
                    while myArray[i+3][j]==0:
                        myArray[i+3][j]=myArray[i+2][j]
                        myArray[i+2][j]=myArray[i+1][j]
                        myArray[i+1][j]=myArray[i][j]
                        myArray[i][j]=0
                if myArray[i+2][j]==0 and (myArray[i+1][j]!=0 or myArray[i][j]!=0):
                    while myArray[i+2][j]==0:
                        myArray[i+2][j]=myArray[i+1][j]
                        myArray[i+1][j]=myArray[i][j]
                        myArray[i][j]=0

                if myArray[i+1][j]==0 and myArray[i][j]!=0:
                    while myArray[i+1][j]==0:
                        myArray[i+1][j]=myArray[i][j]
                        myArray[i][j]=0
        i=0
        for j in range(0,4):
            if myArray[i+3][j]==myArray[i+2][j]:
                myArray[i+3][j]=myArray[i+3][j] + myArray[i+2][j]
                myArray[i+2][j]=myArray[i+1][j]
                myArray[i+1][j]=myArray[i][j]
                myArray[i][j]=0
            if myArray[i+2][j]==myArray[i+1][j]:
                myArray[i+2][j]=myArray[i+2][j]+myArray[i+1][j]
                myArray[i+1][j]=myArray[i][j]
                myArray[i][j]=0
            if myArray[i+1][j]==myArray[i][j]:
                myArray[i+1][j]=myArray[i+1][j]+myArray[i][j]
                myArray[i][j]=0
            
    elif user_input == "l":
        j=0
        for i in range(0,4):

            if myArray[i][j]!=0 or myArray[i][j+1]!=0 or myArray[i][j+2]!=0 or myArray[i][j+3]!=0:
                if myArray[i][j]==0:
                    while myArray[i][j]==0:
                        myArray[i][j]=myArray[i][j+1]
                        myArray[i][j+1]=myArray[i][j+2]
                        myArray[i][j+2] = myArray[i][j+3]
                        myArray[i][j+3]=0
                if myArray[i][j+1]==0 and (myArray[i][j+2]!=0 or myArray[i][j+3]!=0):
                    while myArray[i][j+1]==0:
                        myArray[i][j+1]=myArray[i][j+2]
                        myArray[i][j+2]=myArray[i][j+3]
                        myArray[i][j+3]=0
                if myArray[i][j+2]==0 and (myArray[i][j+3]!=0):
                    while myArray[i][j+2]==0:
                        myArray[i][j+2]=myArray[i][j+3]
                        myArray[i][j+3]=0
        j=0
        for i in range(0,4):
            if myArray[i][j]==myArray[i][j+1]:
                myArray[i][j]=myArray[i][j]+myArray[i][j+1]
                myArray[i][j+1]=myArray[i][j+2]
                myArray[i][j+2]=myArray[i][j+3]
                myArray[i][j+3]=0
            if myArray[i][j+1]==myArray[i][j+2]:
                myArray[i][j+1]=myArray[i][j+1]+myArray[i][j+2]
                myArray[i][j+2]=myArray[i][j+3]
                myArray[i][j+3]=0
            if myArray[i][j+2]==myArray[i][j+3]:
                myArray[i][j+2]=myArray[i][j+2]+myArray[i][j+3]
                myArray[i][j+3]=0
    elif user_input == "r":
        j=0
        for i in range(0,4):
            if myArray[i][j]!=0 or myArray[i][j+1]!=0 or myArray[i][j+2]!=0 or myArray[i][j+3]!=0:
                if myArray[i][j+3]==0:
                    while myArray[i][j+3]==0:
                        myArray[i][j+3]=myArray[i][j+2]
                        myArray[i][j+2]=myArray[i][j+1]
                        myArray[i][j+1]=myArray[i][j]
                        myArray[i][j]=0
                if myArray[i][j+2]==0 and (myArray[i][j+1]!=0 or myArray[i][j]!=0):
                    while myArray[i][j+2]==0:
                        myArray[i][j+2]=myArray[i][j+1]
                        myArray[i][j+1]=myArray[i][j]
                        myArray[i][j]=0

                if myArray[i][j+1]==0 and myArray[i][j]!=0:
                    while myArray[i][j+1]==0:
                        myArray[i][j+1]=myArray[i][j]
                        myArray[i][j]=0
        j=0
        for i in range(0,4):
            if myArray[i][j+3]==myArray[i][j+2]:
                myArray[i][j+3]=myArray[i][j+3] + myArray[i][j+2]
                myArray[i][j+2]=myArray[i][j+1]
                myArray[i][j+1]=myArray[i][j]
                myArray[i][j]=0
            if myArray[i][j+2]==myArray[i][j+1]:
                myArray[i][j+2]=myArray[i][j+2]+myArray[i][j+1]
                myArray[i][j+1]=myArray[i][j]
                myArray[i][j]=0
            if myArray[i][j+1]==myArray[i][j]:
                myArray[i][j+1]=myArray[i][j+1]+myArray[i][j]
                myArray[i][j]=0
                
                
while True:
    print myArray[0][0],"\t",myArray[0][1],"\t",myArray[0][2],"\t",myArray[0][3],"\n"
    print myArray[1][0],"\t",myArray[1][1],"\t",myArray[1][2],"\t",myArray[1][3],"\n"
    print myArray[2][0],"\t",myArray[2][1],"\t",myArray[2][2],"\t",myArray[2][3],"\n"
    print myArray[3][0],"\t",myArray[3][1],"\t",myArray[3][2],"\t",myArray[3][3],"\n"
    user_input = raw_input("u for upward direction, d for downwards, l for left and r for Right")
    users_choice(myArray,user_input)
    listfori = []
    listforj = []
    count = 0
    for i in range(0,4):
        for j in range(0,4):
            if myArray[i][j]==0:
                count+=1
                listfori.append(i)
                listforj.append(j)
    if count > 0:
        if count > 1:
            randomindex = listfori.index(random.choice(listfori))
            myArray[listfori[randomindex]][listforj[randomindex]]=2
        else:
            myArray[listfori[0]][listforj[0]]=2
    else:
        break
print "Game over"

The Implementation of 2048 logic in python

1. Modules

We’ll only use one module i.e random. This module is used to place a number(2) every time a move is made to a random position where there exists no other numbers(free space). For now we’re assuming free space as a zero.

2. Positioning the first number(2) in the 4*4 matrix ( The beginning of the game)

By default every element within the 4*4 matrix is a zero meaning all the valid positions has a value zero. Then a position myArray[i][j] is generated in a random where the first number of the game is placed.

3. Printing out the values contained in the indexes starting from 0*0 to 4*4

The 4*4 matrix is now printed meaning their values are printed. This block of implementation goes inside a loop meaning it is printed until a player looses the game or wins the game.

4. Asking a player for a move (w for up, s for down, a for left and d for right)

An input is taken from the player. The valid inputs are w, s, a and d. This part of the code also resides inside the loop mentioned earlier.

5. Calling the function

A function is now called with parameters as 4*4 matrix, user_input. user_input contains the input from the user(w,s,a,d).

6. The function

The previous function called takes two parameters namely myArray and user_input where myArray is the 4*4 matrix and user_input is the character input given by the user on the move he/she wants to make. According to the input taken different block of code is executed.

 

When user_input ==’s’ (this means player wants to make a downward move)

The thing to understand here is that this movement takes place in columns only. This means the movement shall be in downward direction. First of all we need to check a condition that at least one location in each column contains a non-zero value. Then if the lower most location of a column contains a zero then the location is replaced with a value that is present just above it(second last location of a column) , the second last location is replaced with the third last(second) and third last(second) with the first and the first location is assigned to a value zero until the last location in a column becomes non zero.

Now again another condition is checked i.e

if the second last location in a column contains zero and the second or the first location is none-zero then the second last location is replaced with the value contained in the third last(second) location and the third last(second) with the value of the first location and first location is assigned to a value zero until the second last location in a column becomes non zero.

After that another condition is checked i.e

if the third last(second) location contains a zero value and the first location in a column contains a non zero value then second location is replaced with a value of the first location and first with zero.

 

After the above implementation now summation work is to be done. This begins with condition

if the lower most location in a column holds a value similar to the one present just above it(second last location) then the lower most column is replaced with the sum of the lower most position and second last position’s value, the second last is replaced with the value at the second location(row) of that column. The second is replaced with first row’s value and first row’s value is assigned to zero.

Now another condition is if the value at the third row of a column is same to the value at second row of a column then third row holds the sum of the value at third row and second row in that column, the second rows takes the value at first row and first row takes a value zero. Furthermore if the second row of a column and the first row of that column has same value then the second row holds the sum of second and first row’s value and first row is assigned to zero.

 

When user_input=’w’/’a’/’d’ ( up/ left/ right)

The similar implementation runs here as of that for user_input=’s’

7. Defining two empty lists

Now two empty list is defined. These are the list which holds the ith and jth value of the 4*4 matrix where the value is zero.

8. Count

count is a variable that works as the counter whose value gives the number of location in the 4*4 matrix where the value is zero.

9. Assigning a number to a random location where value is zero

If the value of the variable count is greater then 1 then the random location where value is zero is assigned to a number(2) and the program loops for another time. If count is equal to 1 then the location where value is zero is replaced with a number(2) and the block of code loops for another time. If count is zero then the loop is terminated and the message is printed “Game Over”

The 2048 game is ready in python. If you’ve got any questions comment below. Any explanations needed please mention below.

Bhishan Bhandari [22] Brewing contents directly from the Himalayas of Nepal. I am a hobbyist programmer and enjoy writing scripts for automation. If you'd like a process to be automated through programming, I also sell my services at Fiverr . Lately, I like to refresh my Quora feeds. Shoot me messages at bbhishan@gmail.com  

There Are 3 Comments On This Article.

Leave a Reply

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