Coders Packet

Creating a Puzzle Game 2048 In C++

By John Jayakumar H

Hi friends, Today let us see how to create a single-player sliding puzzle game 2048 using C++.

Here is the code for 2048 using C++

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

void up(int a[4][4])
{
  int i,j,lt,ri;
  for(j=0;j<4;j++)
  {
    lt=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[lt][ri]==a[i][j])
          {
            a[lt][ri]*=2;
            a[i][j]=0;
          }
          else
          {
            if(a[lt][ri]==0)
            {
              a[lt][ri]=a[i][j];
              a[i][j]=0;
            }
            else
            {
              a[++lt][ri]=a[i][j];
              a[i][j]=0;
            }
          }
        }
        else lt++;
      } 		}  		} 		}

void down(int a[4][4])
{
  int i,j,lt,ri;
  for(j=0;j<4;j++)
  {
    lt=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[lt][ri]==a[i][j])
          {
            a[lt][ri]*=2;
            a[i][j]=0;
          }
          else
          {
            if(a[lt][ri]==0)
            {
              a[lt][ri]=a[i][j];
              a[i][j]=0;
            }
            else
            {
              a[--lt][ri]=a[i][j];
              a[i][j]=0;
            }
          }
        }
        else lt--;
  }	}	}	}

void left(int a[4][4])
{
  int i,j,lt,ri;
  for(i=0;i<4;i++)
  {
    lt=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[lt][ri]==a[i][j])
          {
            a[lt][ri]*=2;
            a[i][j]=0;
          }
          else
          {
            if(a[lt][ri]==0)
            {
              a[lt][ri]=a[i][j];
              a[i][j]=0;
            }
            else
            {
              a[lt][++ri]=a[i][j];
              a[i][j]=0;
            }
          }
        }
        else ri++;
      }	}	}	}

void right(int a[4][4])
{
  int i,j,lt,ri;
  for(i=0;i<4;i++)
  {
    lt=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[lt][ri]==a[i][j])
          {
            a[lt][ri]*=2;
            a[i][j]=0;
          }
          else
          {
            if(a[lt][ri]==0)
            {
              a[lt][ri]=a[i][j];
              a[i][j]=0;
            }
            else
            {
              a[lt][--ri]=a[i][j];
              a[i][j]=0;
            }
          }
        }
        else ri--;
      }
    }
  }
}

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

}

void disp(int a[4][4])
{
  cout<<"\n\t\t Press Esc 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 temp[4][4],int a[4][4])
{
  int fl=1,i,j;
  for(i=0;i<4;i++)
    	for(j=0;j<4;j++)
    		if(temp[i][j]!=a[i][j])
    		{
    			fl=0;
    			break;
      }
  return fl;
}

int checkgameover(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\t Let's Play' 2048 \n\n\n\t\t Press Enter key to continue....................";
  getch();
  system("cls");
  int i1,i2,i3,i4,i,j;
  int a[4][4]={0},temp[4][4]={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++)
    			temp[i][j]=a[i][j];
    	ch=getch();
    	system("cls");
    	if(ch==72) up(a);
    	if(ch==80) down(a);
    	if(ch==75) left(a);
    	if(ch==77) right(a);
    if(ch==27) break;
    
    if(!check(temp,a))
      join(a);
    disp(a);
      
    if(!checkgameover(a))
    {
      cout<<"\n\n\n\t\t\t You've LOST The Game !!\n\n\n";
      getch();
      break;
    }	
  }
  return 0;
}

The Output for the code is :

                     

UNDERSTANDING THE ALGORITHM

Functions Created:

up - For upward movement

down - For downward movement

left - For leftwards movement

right - For rightwards movement

join - To merge two blocks into one

disp - To display greeting messages

check - To check if the box is full

checkgameover - To check if there are no more moves to be made

 

These functions are set of codes that can be reused to perform few actions. We've used void because these functions do not return any value. Then in the main function, we'll display the welcome screen using cout. Here we've used system("cls") because it grants permission to send commands directly to the operating system’s command processor. By using this we get a new blank screen when we start the game. Then we use the rand() function to produce random numbers for new blocks. Then it checks if the box is full to stop the arrival of new blocks. Then it also checks if the game is over and there can be no more moves that can be made and then prints You've LOST The Game !!

 

Download project

Reviews Report

Submitted by John Jayakumar H (JohnJayakumar)

Download packets of source code on Coders Packet