include<stdlib.h>
#include<conio.h>
#include<ctype.h>
#include<graphics.h>
#include<dos.h>
#include<time.h>
#include<stdio.h>
#include<string.h>
#include<fstream.h>
//function prototypes
void hangman1();
void puzzle();
//functions for hangman game
void hangman(int num)
{
textcolor(15);//predefined function
char a[][20]={"HANGMAN","GREAT SCORE"};
int i=1,x=12,q=25,k=1,z=0,r=0;
int c=strlen(a[num]);
while(r!=c)
{
while(i!=x)
{
gotoxy(q+z,i);//predefined function
cout<<a[num][r];
delay(10);//predefined function
if(k!=x-1)
{
gotoxy(q+z,k);//predefined function
cout<<" ";
}
i++;
k++;
}
r++;
z+=3;
i=1,k=1;
}
}
void save(int=0,char[]=0);
void get_name(int high,int p,char name[])
{
if(p==0)
{
clrscr();
cout<<"\nYour score is "<<high;
cout<<"You have made the highest score. Enter your name"<<endl;
gets(name);
}
}
void save(int high,char name[])
{
fstream f;
f.open("hangman.txt",ios::out);
f<<high<<"\n"<<name;
f.close();
}
void restore(int &high,char name[])
{
fstream f;
f.open("hangman.txt",ios::in);
f>>high>>name;
f.close();
}
//The Game function
int THE_GAME()
{
textcolor(15);//predefined function
char input;
char name[30]={0};
char temp[10];
static int wining_streak=0,game_lost=0,total_score=...
gotoxy(50,18);//predefined function
cout<<"Highest Score:\t";
restore(highest_score,name);
gotoxy(70,18);//predefined function
cout<<highest_score;
gotoxy(50,16);//predefined function
cout<<name;
char a[][30]={"afghanistan","bahrain","bangla...
"cyprus","india","japan","israel","iraq"...
"jordon","cambodia","kazakhstan","northk...
"kuwait","kyrgyzstan","laos","mangolia",...
"lebanon","myanmar","nepal","oman","p**i...
"syria","taiwan","tajikistan","thailand"...
"unitedarabemirates","uxbekistan","vietn...
randomize();//predefined function
int k=rand()%44;//predefined function
char b[]="_____________";
int length=strlen(a[k]);
int turn=length,dummy=0,decide=0,z=0,score=0...
gotoxy(25,12);//predefined function
for(int i=0;i<length;++i)
cout<<b[i];
gotoxy(50,22);//predefined function
cout<<"Your Total Score:\t";
gotoxy(70,22);//predefined function
cout<<total_score;
gotoxy(50,1);//predefined function
cout<<"The Words that are used"<<endl;
gotoxy(1,4);//predefined function
cout<<"Your wining streak: "<<wining_streak;
gotoxy(1,6);//predefined function
cout<<"Game you lost: "<<game_lost;
while(turn!=0)
{
if(strcmp(temp,a[k])==0)
{
gotoxy(1,18);//predefined function
cout<<"You Have Guessed"<<endl;
break;
}
gotoxy(1,8);//predefined function
cout<<"Turns left: ";
gotoxy(15,8);//predefined function
cout<<turn;
gotoxy(1,1);//predefined function
cout<<"Enter the character: ";
cin>>input;
gotoxy(50,z+3);//predefined function
cout<<input;
input=tolower(input);
for(int r=0;r<length;++r)
{
if(input==a[k][r]&&b[r]=='_')
{
decide=1;
turn=length-dummy;
break;
}
else
decide=0;
}
if(decide==0)
{
dummy++;
turn=length-dummy;
}
gotoxy(15,8);//predefined function
cout<<turn;
gotoxy(50,20);//predefined function
cout<<"Your Current Score:\t";
gotoxy(70,20);//predefined function
score=turn*5;
cout<<score;
for(r=0;r<length;++r)
if(input==a[k][r])
{
b[r]=a[k][r];
temp[r]=a[k][r];
temp[length]='\0';
gotoxy(25+r,12);//predefined function
b[r]=toupper(b[r]);
cout<<b[r];
}
z++;
}
if(strcmp(temp,a[k])!=0)
{
char guess[20];
gotoxy(1,16);//predefined function
cout<<"Sorry, Turns are over";
gotoxy(1,18);//predefined function
cout<<"Make a guess"<<endl<<endl;
cin>>guess;
if(strcmp(guess,a[k])==0)
{
clrscr();
gotoxy(25,12);//predefined function
cout<<"Great Guess\n"<<endl;
gotoxy(25,16);//predefined function
cout<<"You Won"<<endl;
wining_streak++;
total_score+=score;
if(total_score>=highest_score)
{
highest_score=total_score;
getch();
get_name(highest_score,p,name);
save(highest_score,name);
p++;
}
return 0;
}
else
{
gotoxy(1,22);//predefined function
cout<<"Sorry, You Lost"<<endl;
game_lost++;
total_score+=score;
gotoxy(50,22);//predefined function
cout<<"Your Total Score:\t";
gotoxy(70,22);//predefined function
cout<<total_score;
}
getch();
clrscr();
cout<<"The correct Answer is "<<endl;
gotoxy(25,12);//predefined function
cout<<a[k];
return 0;
}
else
{
gotoxy(1,20);//predefined function
cout<<"Congratulations";
}
total_score+=score;
if(total_score>=highest_score)
{
highest_score=total_score;
getch();
get_name(highest_score,p,name);
save(highest_score,name);
p++;
}
wining_streak++;
return 0;
}
//functions for puzzle game
#define maximum 10
#define sqr(n) n*n
void shuffle(void);//shuffles the board
int found(int board[][maximum],int no);//helper function for shuffling
void dispboard(void);//displays the current contents of board
int getkey(void);//used to track arrow keys
void swap(int x2,int y2);//swaps x,y with x2,y2
int board[maximum][maximum];
int x,y;
int win,turns;
int ORDER;
void shuffle(void)
{
randomize();//predefined function
for(int i=0;i<ORDER;++i)
for(int j=0;j<ORDER;++j)
board[i][j]=0;//empties the board
int temp,M=sqr(ORDER)-1;
for(i=0;i<ORDER;++i)
for(j=0;j<ORDER;++j)
{
if((i==ORDER-1)&&(j==ORDER-1))
{
board[i][j]=M+1;//sets the largest integer(space/gap)
turns=0;
x=y=ORDER-1;
return;
}
do
{
temp=rand()%M+1;//predefined function
}while(found(board,temp));//the loop generates unique numbers between the specic limit
board[i][j]=temp;
}
}
int found(int board[][maximum],int no)
{
for(int i=0;i<ORDER;++i)
for(int j=0;j<ORDER;++j)
if(board[i][j]==no)
return 1;
return 0;
}
void dispboard(void)
{
clrscr();
int i,j,chk=0;
win=1;
cout<<"Press \"Esc\" at any time to quit\n\n";
cout<<"Press Arrow Keys to move\n";
cout<<"Press \"S\" to shuffle\n\n\n";
for(i=0;i<ORDER;++i)
{
for(j=0;j<ORDER;++j)
{
if(!((++chk==board[i][j])&&(win==1)))
win=0;
if(board[i][j]==sqr(ORDER))
cout<<" ";
else
printf("%2d",board[i][j]);//predefined function
}
cout<<"\n\n";
}
printf("Turns=%d",turns);//predefined function
}
int getkey(void)
{
union REGS i,o;
while(!kbhit());
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}
void swap(int x2,int y2)
{
if((x2>=0)&&(x2<=ORDER-1)&&(y2>=0)&&(y2<...
{
board[y][x]=board[y2][x2];
board[y2][x2]=sqr(ORDER);
x=x2;
y=y2;
turns++;
}
}
//main code for hangman
void hangman1()
{
clrscr();
char RESPONSE;
do
{
clrscr();
hangman(0);
getch();
clrscr();
THE_GAME();
getch();
clrscr();
gotoxy(25,12);//predefined function
cout<<"Do you want to continue(Y/N):";
cin>>RESPONSE;
}while(RESPONSE=='Y'||RESPONSE=='y');
return;
}
//main code for puzzle
void puzzle()
{
clrscr();
textcolor(1);//predefined function
gotoxy(38,4);//predefined function
cout<<"Puzzle";
gotoxy(20,16);//predefined function
cout<<"Press any key to begin.....";
getch();
textcolor(2);//predefined function
cout<<"\n\n\t\tEnter order (Maximum 10):";
cin>>ORDER;
if(ORDER<2||ORDER>10)
return;//ends the program
shuffle();
dispboard();
do
{
int move=0;
while(!(move==72||move==80||move==75||mo...
{
move=getkey();
}
if(move==1)//escape key
return;
else if(move==31)
shuffle();
else if(move==80)
swap(x,y-1);
else if(move==72)
swap(x,y+1);
else if(move==77)
swap(x-1,y);
else if(move==75)
swap(x+1,y);
dispboard();
}while(win!=1);
printf("Congratulations You won in %d turns",turns);//predefined function
getch();
return;
}
Tags: