Documente Academic
Documente Profesional
Documente Cultură
CLASS: XII-SCIENCE
SESSION: 2019-2020
ACKNOWLEDGEMENT
OBJECTIVE
SOFTWARE AND HARDWARE
REQUIRED
SOURCE CODE
SAMPLE OUTPUT
CERTIFICATE
Acknowledgement
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>
class Game;
Game_AI():
score(0),plus(0),win(2048),max(0),response(0),end(1) {}
//initialize values
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;
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;
}
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");
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";
}
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<<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();
}
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
___________________ ____________________
Signature of External Examiner Signature of Subject Teacher