How to Create Responsive 2048 Game in C++ With Source Code

2048 Game in C++ With Source Code

Hello, coders. Welcome to the codewithrandom blog. In this article, we will learn how to create 2048 Game using C++.

2048 game

Let us get acquainted with the 2048 game.

 

  • 2048 is a single-player mathematical, sliding tile puzzle video game. The system of this game is based on the concept of moving numbers on a grid to merge them to form the number 2048.
  • However, you can keep on playing the game by creating larger numbers. The game has simple and straightforward mechanics and dynamics which are quite easy to understand and get through.
  • These are well supported by the game’s simplistic and mindful educational objectives.

What is the conclusion of the 2048 game code in C++?

 

A move is legal if at least one tile can be slid into an empty spot or if the tiles can be combined in the chosen direction. The game ends when the user does not have any legal moves left.
 

 

Overview of the UI:

 

The UI of the game is simple:

The First output on the console will have the following –

2048 GAME

Press any key to continue

Upon pressing any key to continue, the game will be shown on the console where the user can use the arrow keys to play the game.

2048 Game in C++ With Source Code

You can take help from the code to learn and create the game or you directly copy the source code into your IDE and start playing.

#include<bits/stdc++.h>
#include<conio.h>
using namespace std;

void upmove(int a[4][4])
{
    int i,j,li,ri;
    for(j=0;j<4;j++)
    {
        li=0,ri=j;
        for(i=1;i<4;i++)
        {
            if(a[i][j]!=0)
            {
                if(a[i-1][j]==0 || a[i-1][j]==a[i][j])
                {
                    if(a[li][ri]==a[i][j])
                    {
                        a[li][ri]*=2;
                        a[i][j]=0;
                    }
                    else
                    {
                        if(a[li][ri]==0)
                        {
                            a[li][ri]=a[i][j];
                            a[i][j]=0;
                        }
                        else
                        {
                            a[++li][ri]=a[i][j];
                            a[i][j]=0;
                        }
                    }
                }
                else li++;
            }
        }
    }
}

void downmove(int a[4][4])
{
    int i,j,li,ri;
    for(j=0;j<4;j++)
    {
        li=3,ri=j;
        for(i=2;i>=0;i--)
        {
            if(a[i][j]!=0)
            {
                if(a[i+1][j]==0 || a[i+1][j]==a[i][j])
                {
                    if(a[li][ri]==a[i][j])
                    {
                        a[li][ri]*=2;
                        a[i][j]=0;
                    }
                    else
                    {
                        if(a[li][ri]==0)
                        {
                            a[li][ri]=a[i][j];
                            a[i][j]=0;
                        }
                        else
                        {
                            a[--li][ri]=a[i][j];
                            a[i][j]=0;
                        }
                    }
                }
                else li--;
            }
        }
    }
}

void leftmove(int a[4][4])
{
    int i,j,li,ri;
    for(i=0;i<4;i++)
    {
        li=i,ri=0;
        for(j=1;j<4;j++)
        {
            if(a[i][j]!=0)
            {
                if(a[i][j-1]==0 || a[i][j-1]==a[i][j])
                {
                    if(a[li][ri]==a[i][j])
                    {
                        a[li][ri]*=2;
                        a[i][j]=0;
                    }
                    else
                    {
                        if(a[li][ri]==0)
                        {
                            a[li][ri]=a[i][j];
                            a[i][j]=0;
                        }
                        else
                        {
                            a[li][++ri]=a[i][j];
                            a[i][j]=0;
                        }
                    }
                }
                else ri++;
            }
        }
    }
}

void rightmove(int a[4][4])
{
    int i,j,li,ri;
    for(i=0;i<4;i++)
    {
        li=i,ri=3;
        for(j=2;j>=0;j--)
        {
            if(a[i][j]!=0)
            {
                if(a[i][j+1]==0 || a[i][j+1]==a[i][j])
                {
                    if(a[li][ri]==a[i][j])
                    {
                        a[li][ri]*=2;
                        a[i][j]=0;
                    }
                    else
                    {
                        if(a[li][ri]==0)
                        {
                            a[li][ri]=a[i][j];
                            a[i][j]=0;
                        }
                        else
                        {
                            a[li][--ri]=a[i][j];
                            a[i][j]=0;
                        }
                    }
                }
                else ri--;
            }
        }
    }
}

void addblock(int a[4][4])
{
    int li,ri;
    srand(time(0));
    while(1)
    {
        li=rand()%4;
        ri=rand()%4;
        if(a[li][ri]==0)
        {
            a[li][ri]=pow(2,li%2 + 1);
            break;
        }
    }

}

void disp(int a[4][4])
{
    cout<<"\n\t\tPress Esc anytime to quit the game";
    cout<<"\n\n\n\n";
    int i,j;
    for(i=0;i<4;i++)
    {
        cout<<"\t\t\t\t-----------------\n\t\t\t\t";
        for(j=0;j<4;j++)
        {
            if(a[i][j]==0) cout<<"|   ";
            else
                cout<<"| "<<a[i][j]<<" ";
        }
        cout<<"|"<<endl;
    }
    cout<<"\t\t\t\t-----------------\n";
}

int check(int tmp[4][4],int a[4][4])
{
    int fl=1,i,j;
    for(i=0;i<4;i++)
    	for(j=0;j<4;j++)
    		if(tmp[i][j]!=a[i][j])
    		{
    			fl=0;
    			break;
            }
    return fl;
}

int checkover(int a[4][4])
{
    int fl=0,gl=0,i,j;
    for(i=0;i<4;i++)
    	for(j=0;j<4;j++)
    		if(a[i][j]==0)
    		{
    			fl=1;
                break;	
            }
            
    for(i=0;i<3;i++)
    	for(j=0;j<3;j++)
    		if(a[i+1][j]==a[i][j] || a[i][j+1]==a[i][j])
    		{
    			gl=1;
    			break;
            }
            
    if(fl || gl) return 1;
    else return 0;
}

int main()
{
    cout<<"\n\n\n\n\t\t\t2048 GAME\n\n\n\t\tPress any key to continue";
    getch();
    system("cls");
    int i1,i2,i3,i4,i,j;
    int a[4][4]={0},tmp[4][4]={0};
    srand(time(0));
    i1=rand()%4;
    i2=rand()%4;
    while(1)
    {
        i3=rand()%4;
        i4=rand()%4;
        if(i3!=i1 && i4!=i2) break;
    }
    a[i1][i2]=2;
    a[i3][i4]=4;
    disp(a);
    
    int ch;
    while (1)
    {
    	for(i=0;i<4;i++)
    		for(j=0;j<4;j++)
    			tmp[i][j]=a[i][j];
    	ch=getch();
    	system("cls");
    	if(ch==72) upmove(a);
    	if(ch==80) downmove(a);
    	if(ch==75) leftmove(a);
    	if(ch==77) rightmove(a);
        if(ch==27) break;
        
        if(!check(tmp,a))
            addblock(a);
        disp(a);
            
        if(!checkover(a))
        {
            cout<<"\n\n\t\t\tGAME OVER!!\n\n\n";
            getch();
            break;
        }	
    }
    return 0;
}

Now let us understand the code:-

After writing the header of the code with the required libraries, we have declared functions, the functions are – upmove(), downmove(), leftmove(), rightmove(), addblock(), disp(), check(), checkover() and then the main() function. We’ve used void as these functions defined do not return any value.

The upmove() function:

This function uses the upwards arrow key to slide the numbers upwards. We have declared variables for the looping. The nested “for” loop is used for the functionality of sliding upward of the numbers to play the game. Inside the loop we have used the if-else control statement to set the down key functionality.

The downmove() function:

This function uses the downward arrow key to slide the numbers downwards. We have declared variables for the looping in this function. The nested “for” loop is used for the functionality of sliding upward of the numbers to play the game. Inside the loop we have used the if-else control statement to set the down key functionality.

The leftmove() function:

This function uses the left arrow key to slide the numbers in the left direction. We have declared variables for the looping. The nested “for” loop is used for the functionality of sliding in the left direction of the numbers to play the game. Inside the loop we have used the if-else control statement to set the down key functionality.

The rightmove() function:

This function uses the right arrow key to slide the numbers in the right direction. We have declared variables for the looping. The nested “for” loop is used for the functionality of sliding in the right direction of the numbers to play the game. Inside the loop we have used the if-else control statement to set the down key functionality.

The addblock() function:

This function takes an array as a parameter.

The disp() function:

This function displays the greeting messages. It takes an array as a parameter. It contains the functionality to display the game on the console. It consists of a for-loop and an if-else control statement.

The check() function:

This function takes 2 arrays as parameters, it is to check if the box is full or not.

ADVERTISEMENT

The checkover() function:

ADVERTISEMENT

This function checks if there are no more moves to be made further in the game.

ADVERTISEMENT

The main() function:

ADVERTISEMENT

In the main function, we’ll display the welcome screen using console output- cout. We will use system(“cls”) as it grants permission to send commands directly to the operating system’s command processor. By using this we will get a blank new screen when we start the 2048 game.

ADVERTISEMENT

The rand() function defined is to produce random numbers for the new blocks. The function then checks if the box is full so that the arrival of new blocks can be stopped further in the game. It then also checks if the game is over and no more moves can be made, then it prints GAME OVER!! on the console.

Creating a Wi-fi Password generator using C++

Final Output:-

2048 Game in C++ With Source Code

2048 Game in C++ With Source Code

After using the arrow keys to play the game:

2048 Game in C++ With Source Code

Continue playing the game to get to the desired goal-2048.

Conclusion:

We have reached the end of this article and have a lot more projects in C++ coming, so stay tuned. We have started with more interesting and fun projects for you all to understand C++. Learning C++ by creating fun projects makes learning easy and interactive.

Base Convertor project using C++ (With source code)

If you enjoyed the article and learned something new today, let us know in the comments.

Thank you.

Happy Reading! 🙂

Follow: CodeWithRandom 

FAQs related to 2048 games

What is the 2048 game?

 2048 is a single-player mathematical, sliding tile puzzle video game. The system of this game is based on the concept of moving numbers on a grid to merge them to form the number 2048.

Explain the Rightmove () function.

This function uses the right arrow key to slide the numbers in the right direction. We have declared variables for the looping. The nested “for” loop is used for the functionality of sliding in the right direction of the numbers to play the game. Inside the loop we have used the if-else control statement to set the down key functionality.



Leave a Reply