Sunteți pe pagina 1din 32

AISSCE 2019-2020

COMPUTER SCIENCE PROJECT


TOY STORE USING C++

NAME: UPAL CHOUDHURY

CLASS: XII-SCIENCE

ROLL NO.: _____________

SESSION: 2019-2020

SUBJECT CODE: 083


CONTENTS

 ACKNOWLEDGEMENT
 OBJECTIVE
 SOFTWARE AND HARDWARE
REQUIRED
 SOURCE CODE
 SAMPLE OUTPUT
 CERTIFICATE
Acknowledgement

I would like to express my special thanks of gratitude


to my teacher Mr Krishnendu Sen who gave me the
golden opportunity to do this wonderful project on the
topic Implementation of the game 2048 using c++,
which also helped me in doing a lot of Research and I
came to know about so many new things I am really
thankful to them.
Secondly I would also like to thank my parents and
friends who helped me a lot in finalizing this project
within the limited time frame.
OBJECTIVE

To demonstrate the working of the


game 2048 using c++.
HARDWARE AND
SOFTWARE REQUIRED

Hardware:
 Printer, to print the required documents of
the project.
 Compact Drive.
 Processor: Intel CORE i3.
 Ram: 64 MB.
 Hard Disk: 20 GB.

SOFTWARE:
 Operating system: Windows 10.
 Turbo C++, for execution of program.
 MS word, for presentation of the project.
SOURCE CODE
//your work is in the functions in lines 651 7 673 .You need
to add something such that highscores when viewed in the
program are sorted from highest to lowest.
//does not matter if it is not sorted in txt file. If you use a
function, declare it in class Game, in line 62, and define it
inside the respective functions in
//lines 651 or 673.

#include<iostream>
#include<time.h> //for time function
#include<unistd.h> //for sleep function
#include<stdlib.h> //for rand,srand functions
#include<math.h> //for log function
#include <fstream>

using namespace std;

int random_index(int); //go to line 368

class Game;

class Game_AI //main class


{

int end; //variable that checks for game end


conditions
char keypress; //variable that checks whick key
was pressed
int response; //variable that executes certain
functions depending on its value
public:

int max; //current highest value tile in grid


int win; //checks if highest value tile is
equal to 2048
int plus; //adds to current score
int score; //current score
int grid[4][4]; //the grid
int pgrid[4][4]; //the grid of previous move

Game_AI():
score(0),plus(0),win(2048),max(0),response(0),end(1) {}
//initialize values

void logic_flow(Game*); //the game logic, go to line


217
void game_end_check(Game*); //check whether game
is over, go to line 280
void key_press(); //accepts character, go to line
210
void start_grid(); //forms grid, go to line 311
void update_grid(); //updates grid after a move
and adds up the merging numbers, go to line 461
void fill_space(); //determines empty spaces in
grid and how to fill them, go to line 384
void spawn(); //spawns new numbrs in grid, go
to line 533
void find_greatest_tile(); //finds the current greatest
value tile, go to line 557
void backup_grid(); //the function that maintains
pgrid, go to line 376
int full(); //checks wheter grid is full or not,
go to line 565
int block_moves(); //checks if game can block
moves or not, go to line 693
};

class Game : public Game_AI //derived class


{
char option;
string name;

public:
void display_grid(); //displays grid, go to line 334
void display_instructions_screen(); //displays instruction
screen, go to line 580
void display_credits_screen(); //displays credits screen,
go to line 600
void display_win_screen(); //displays credits screen,
go to line 613
void display_loser_screen(); //displays credits screen,
go to line 626
void save_highscore(); //saves a highscore, go to
line 651
void display_highscores(); //displays highscore
screen, go to line 673
char display_try_again_screen(int); //displays try again
screen and asks if want to play again, go to line 637

};

int main()
{
system("cls"); //main screen design
cout<<"\n\n\n";
for(int i=0;i<10;i++)
{
for(int j=0;j<28;j++)
{
cout<<" ";
}
for(int k=29;k<57;k++)
{
switch(i)
{
case 0:
if(k==45||k==46||k%7==0)
{
cout<<" ";
}
else
{
cout<<"X";
}
break;
case 1:
if(k==45||k==46||k%7==0)
{
cout<<" ";
}
else
{
cout<<"X";
}
break;
case 2:
if(k==29||k==30||k==31||k==32||k==37||k==38|
|k==39||k==40||k==45||k==46||k==51||k==52||k==
53||k==54||k%7==0)
{
cout<<" ";
}
else
{
cout<<"X";
}
break;
case 3:

if(k==29||k==30||k==31||k==32||k==37||k==38|
|k==39||k==40||k==45||k==46||k==51||k==52||k==
53||k==54||k%7==0)
{
cout<<" ";
}
else
{
cout<<"X";
}
break;
case 4:

if(k==37||k==38||k==39||k==40||k%7==0)
{
cout<<" ";
}
else
{
cout<<"X";
}
break;
case 5:

if(k==37||k==38||k==39||k==40||k%7==0)
{
cout<<" ";
}
else
{
cout<<"X";
}
break;
case 6:

if(k==31||k==32||k==33||k==34||k==37||k==38|
|k==39||k==40||k==43||k==44||k==45||k==46||k==
51||k==52||k==53||k==54||k%7==0)
{
cout<<" ";
}
else
{
cout<<"X";
}
break;
case 7:

if(k==31||k==32||k==33||k==34||k==37||k==38|
|k==39||k==40||k==43||k==44||k==45||k==46||k==
51||k==52||k==53||k==54||k%7==0)
{
cout<<" ";
}
else
{
cout<<"X";
}
break;
case 8:

if(k==43||k==44||k==45||k==46||k%7==0)
{
cout<<" ";
}
else
{
cout<<"X";
}
break;
case 9:

if(k==43||k==44||k==45||k==46||k%7==0)
{
cout<<" ";
}
else
{
cout<<"X";
}
break;
}
}
cout<<"\n";
}
cout<<"\n\n\n\n\n\n\n\n";
for(int x=0;x<1;x++)
{
cout<<"\n\n";
for(int y=0;y<21;y++)
{
cout<<" ";
}
cout<<"Press Enter to PLAY....";
}
getchar();

Game exec;

srand(time(NULL)); //to ensure that game starts


in a random manner each time

exec.start_grid();

while(1)
{
exec.display_grid();
exec.key_press();
exec.logic_flow(&exec);
exec.game_end_check(&exec);
};

return 0;
}

void Game_AI::key_press()
{
cout<<"\n\n";
cin>>keypress;
}

void Game_AI::logic_flow(Game *execute)


{

switch(keypress)
{
case 'w':
case 'a':
case 's':
case 'd':
execute->backup_grid();
execute->fill_space();
execute->update_grid();
execute->fill_space();
execute->find_greatest_tile();
execute->display_grid();

if(execute->full()&&end)
{
response=-1;
break;
}
else if(execute->block_moves())
{
execute->spawn();
break;
}
else
{
response=0;
break;
}

case 'h':
execute->display_highscores();
getchar();
execute->display_grid();
break;

case 'r':
execute->start_grid();
score=0;
plus=0;
break;

case 'q':
response=-1;
break;

case 'c':
execute->display_credits_screen();
getchar();
execute->display_grid();
break;

case 'i':
execute->display_instructions_screen();
getchar();
execute->display_grid();
break;

}
}
void Game_AI::game_end_check(Game *screen)
{
if(max==win)
{
screen->display_win_screen();

if(screen->display_try_again_screen(0)=='n')
response=-1;
else
win*=2;
}

else if(response==-1)
{
screen->display_loser_screen();

if(screen->display_try_again_screen(1)=='y')
{
screen->start_grid();
response=0;
}
}

if(response==-1)
{
cout<<"\n\n\t\t > Thank you for playing. ";
getchar();
exit(0);
}
}

void Game_AI::start_grid()
{
int i,j;

plus=0;
score=0;
max=0;

for(i=0;i<4;i++)
for(j=0;j<4;j++)
grid[i][j]=0;

i=random_index(4);
j=random_index(4);

grid[i][j]=2;

i=random_index(4);
j=random_index(4);

grid[i][j]=2;
}

void Game::display_grid()
{
system("cls");

cout<<"\n ::[ "<<win<<" ]::\n\n\t";

if(plus)
cout<<"+"<<plus<<"!";
else
cout<<" ";
cout<<"\t\t\t\t\t\tSCORE::"<<score<<"
\n\n\n\n";

for(int i=0;i<4;i++)
{
cout<<"\t\t |";

for(int j=0;j<4;j++)
{
if(grid[i][j])
printf("%4d
|",grid[i][j]);
else
printf("%4c |",' ');
}

cout<<"\n\n";
}

cout<<"\n\n Controls -\t\t\t\t\tH -


Highscores\tR - Restart\n\n\tW\t\t \t\t\tQ - Quit\tC -
Credits\t\t\t\t\t\t"
<<"\n A S D\t\t "
<<"\tI - Instructions\n Your Input: ";

int random_index(int x)
{
int index;
index=rand()%x+0;

return index;
}

void Game_AI::backup_grid()
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
pgrid[i][j]=grid[i][j];
}

void Game_AI::fill_space()
{

switch(keypress)
{
case 'w':
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(!grid[j][i])
{
for(int
k=j+1;k<4;k++)
if(grid[k][i])
{

grid[j][i]=grid[k][i];

grid[k][i]=0;
break;
}
}
}break;

case 's':
for(int i=0;i<4;i++)
for(int j=3;j>=0;j--)
{
if(!grid[j][i])
{
for(int k=j-
1;k>=0;k--)
if(grid[k][i])
{

grid[j][i]=grid[k][i];

grid[k][i]=0;
break;
}
}

}break;
case 'a':
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(!grid[i][j])
{
for(int
k=j+1;k<4;k++)
if(grid[i][k])
{

grid[i][j]=grid[i][k];
grid[i][k]=0;
break;
}
}

}break;

case 'd':
for(int i=0;i<4;i++)
for(int j=3;j>=0;j--)
{
if(!grid[i][j])
{
for(int k=j-
1;k>=0;k--)
if(grid[i][k])
{

grid[i][j]=grid[i][k];

grid[i][k]=0;
break;
}
}

}break;

}
}
void Game_AI::update_grid()
{
plus=0;
end=1;

switch(keypress)
{
case 'w':
for(int i=0;i<4;i++)
for(int j=0;j<3;j++)
{

if(grid[j][i]&&grid[j][i]==grid[j+1][i])
{
end=0;

grid[j][i]+=grid[j+1][i];
grid[j+1][i]=0;

plus+=(((log2(grid[j][i]))-1)*grid[j][i]);

score+=(((log2(grid[j][i]))-1)*grid[j][i]);

}
}break;

case 's':
for(int i=0;i<4;i++)
for(int j=3;j>0;j--)
{

if(grid[j][i]&&grid[j][i]==grid[j-1][i])
{
end=0;
grid[j][i]+=grid[j-
1][i];
grid[j-1][i]=0;

plus+=(((log2(grid[j][i]))-1)*grid[j][i]);

score+=(((log2(grid[j][i]))-1)*grid[j][i]);
}
}break;

case 'a':
for(int i=0;i<4;i++)
for(int j=0;j<3;j++)
{

if(grid[i][j]&&grid[i][j]==grid[i][j+1])
{
end=0;

grid[i][j]+=grid[i][j+1];
grid[i][j+1]=0;

plus+=((log2(grid[i][j]))-1)*grid[i][j];

score+=((log2(grid[i][j]))-1)*grid[i][j];
}
}break;

case 'd':
for(int i=0;i<4;i++)
for(int j=3;j>0;j--)
{
if(grid[i][j]&&grid[i][j]==grid[i][j-1])
{
end=0;

grid[i][j]+=grid[i][j-1];
grid[i][j-1]=0;

plus+=((log2(grid[i][j]))-1)*grid[i][j];

score+=(((log2(grid[i][j]))-1)*grid[i][j]);
}
}break;

void Game_AI::spawn()
{
int i,j,k;

do
{
i=random_index(4);
j=random_index(4);
k=random_index(10);

}while(grid[i][j]);
if(k<2)
grid[i][j]=4;

else
grid[i][j]=2;

void Game_AI::find_greatest_tile()
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
if(grid[i][j]>max)
max=grid[i][j];
}

int Game_AI::full()
{
int k=1;

for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(!grid[i][j])
k=0;

}
return k;
}

void Game::display_instructions_screen()
{
system("cls");

cout<<endl<<" ::[ 2048 ]::";

char text[]="> The game starts with 2 randomly placed


numbers in a 4x4 grid (16 cells).> Use the controls to move
the cells. ( Press W/A/S/D )> 2 cells of the same number
will merge and add up.> New 2s and 4s will appear randomly
on an empty cell with each move you make.> Your objective
is to make 2048 in a cell without getting stuck!> Press Enter
to continue...";
getchar();
for(int i=0; text[i] ; i++)
{
if(text[i]=='>')
{
sleep(1);
cout<<"\n\n\n";
}

cout<<text[i];
}
}

void Game::display_credits_screen()
{
system("cls");
cout<<endl<<" ::[ 2048 ]::";

cout<<"\n\n\n\n\n\n\n"
<<"\n\n\t\t\tGame developed by:"
<<"\n\n\t\t\tSusmit Kar | Tuneer Saha | Upal
Chaudhury"
<<"\n\n\t\t\tClass XII B5, Hariyana Vidya
Mandir";
getchar();
}

void Game::display_win_screen()
{
system("cls");

cout<<endl<<endl;
cout<<"\n\t\t\t :: [ YOU MADE "<<win<<"! ] ::"
<<"\n\n\t\t\t :: [ YOU WON THE GAME ] ::"
<<"\n\n\n\n\t\t\t TILE\t SCORE\t
NAME";
printf("\n\n\t\t\t %4d\t %6d\t ",max,score);
cin>>name;

void Game::display_loser_screen()
{
system("cls");

cout<<"\n\n\n\t\t\t :: [ G A M E O V E R ] ::"
<<"\n\n\n\n\t\t\t TILE\t SCORE\t
NAME";
printf("\n\n\t\t\t %4d\t %6d\t ",max,score);
cin>>name;
save_highscore();
}

char Game::display_try_again_screen(int lose)


{
if(lose)
cout<<"\n\n\n\t > Winnning this game is easy.
Quitting is easier. "
<<"\n\n\n\t Would you like to try again? (y/n)::
";

else
cout<<"\n\n\n\t > The game's name can be
modified for you. "
<<"\n\n\n\t Would you like to continue
playing? (y/n):: ";
cin>>option;

return option;
}

void Game::save_highscore()
{
ofstream fout("scores.txt",ios::app);
//scores.txt is the notepad file
fout<<name<<"\t\t\t\t"<<score<<"\n";
fout.close();
system("cls");
ifstream fin("scores.txt",ios::in);
fin.seekg(0);
cout<<"\n";
cout<<"Highscores:\n\n";
cout<<"Player name\t\tScore\n\n";
for(int i=0;i<10;i++)
{
fin>>name;
fin>>score;
cout<<name<<"\t\t\t";
cout<<score<<"\n";
}
fin.close();
getchar();
}

void Game::display_highscores()
{
system("cls");
ifstream fin("scores.txt",ios::in);
fin.seekg(0);
cout<<"\n";
cout<<"Highscores:\n\n";
cout<<"Player name\t\tScore\n\n";
for(int i=0;i<10;i++)
{
fin>>name;
fin>>score;
cout<<name<<"\t\t\t";
cout<<score<<"\n";
}
fin.close();
getchar();
}

int Game_AI::block_moves()
{
int k=0;

for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
if(pgrid[i][j]!=grid[i][j])
{
k=1;
break;
}
return k;
}
SAMPLE OUTPUT
Certificate

This is to certify that Upal Choudhury of class


XII SCIENCE has successfully completed her
project on the topic WORKING OF THE
GAME 2048 and has done all aspects of it.

___________________ ____________________
Signature of External Examiner Signature of Subject Teacher

S-ar putea să vă placă și