2048 Game Using C++

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

A Project Report on

2048 Game Using C++

BACHELOR OF COMPUTER APPLICATION

By

Ishaan Kumar
AJU/210217

Under the guidance of

Mr. Akash Bhagat

ARKA JAIN UNIVERSITY

JAMSHEDPUR, JHARKHAND

DEPARTMENT OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY


BONAFIDE CERTIFICATE

This is to certify that the project entitled “2048 Game Using C++”, is bonafide
work of Ishaan Kumar bearing enrolment Number: AJU/210217 under the
guidance of Mr. Akash Bhagat submitted in partial fulfilment of the requirement
for the award of degree in BACHELOR OF COMPUTER APPLICATION from
ARKA JAIN UNIVERSITY, JHARKHAND during the year 2021 - 2022.

Dr. Arvind Kumar Pandey


Head of the Department
Department of CS & IT
ARKA JAIN UNIVERSITY
Gamharia, Jharkhand
dr.arvind@arkajainuniversity.ac.in
ABSTRACT

Creating a 2048 Game in C++ language is an excellent method to become acquainted with a
new computer using C++ programming language.

The game primarily evaluates the game player’s status based on the two-dimensional array of
player activities (press the buttons W, A, S and D to move).

As a result, employ variables to keep track of the player’s current status.


ACKNOWLEDGEMENT

I would like to express my special thanks of gratitude to my mentor Mr. Akash Bhagat who
gave me the golden opportunity to do this wonderful project on “2048 Game Using C++”.

I came to know about many new things I am really thankful to them.

Last but not the least I would like to thank my batch mates who have helped me a lot.
DECLARATION

I, Ishaan Kumar, hereby declare that the Project entitled “2048 Game using C++”done at
ARKA JAIN UNIVERSITY has not been in any case duplicated to submit to any other
university for the award of any degree. To the best of my knowledge other than me, no one has
submitted to any other university.

This project is done in partial fulfilment of the requirement for the award of degree of
BACHELOR OF COMPUTER APPLICATION to be submitted as final semester project as
part of our curriculum.

Ishaan Kumar
AJU/210217
CONTENTS

• Specification Requirement

• Code

• Conclusion

• Reference
SPECIFICATION REQUIREMENT

Hardware:

• Intel i3 minimum
• Minimum 64 MB RAM
• At least 32 GB storage

Software:

• OS: Windows 7
• Dev C++
• Code::Blocks
CODE

#include<bits/stdc++.h>
#include<conio.h>
using namespace std;
void up(int a[4][4])//Moving the contents Up
{
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])//Moving the contents down


{
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])//Moving the contents to the left


{
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])//Moving the contents to the right


{
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 display(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 i,j,f=1;

for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(temp[i][j]!=a[i][j])
{
f=0;
break;
}
}
}
return(f);
}

int checkgameover(int a[4][4])


{
int f=0,g=0,i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(a[i][j]==0)
{
f=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])
{
g=1;
break;
}
}
}
if(f||g)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
cout<<"\n\n\n\n\t\t\t 2048 GAME \n\n\n\t\t PRESS ENTER TO BEGIN........";
getch();
system("cls");
int i1,i2,i3,i4,i,j;
int a[4][4]={0};
int 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;
display(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);
}
display(a);
if(!checkgameover(a))
{
cout<<"\n\n\n\t\t\t GAME OVER \n\n\n";
getch();
break;
}
}
return 0;
}
CONCLUSION

It was a wonderful and learning experience for me while working on this project. This
project took me through the various phases of project development. The joy and the thrill
involved while tackling the various problems and challenges gave me a feel of
developer’s industry.

I enjoyed each and every bit of work I had put into this mini project i.e.,”2048 Game
Using C++”.
REFERENCE

• https://code-projects.org
• https://www.geeksforgeeks.org
• Programming in ANSI C
• https://ofstack.com
OUTPUT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy