Guess Game

This is a source code of "Guess Game", which i have created. you will guess the name of a personality, place or sports player etc. It is played using keyboard only. compile with turbo c/c++ (recommended)

#include<stdlib.h>
#include<ctype.h>
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<time.h>

//function prototypes

display(char name[25]);//displays menu
getready(char array[25],char name[25],int *x,int *ggg);//prepare arrays for playing
used_letters(int *y,char test[25]);//shows used letters
input_error(int *o,int *d,char *item);//displays errors
check_input(int *y,int *o,char test[25],char item);//ckecks input for errors
search(int *b,int *c,int *d,int *y,char item,char name[25],char array[25]);//searches input, compares input with array
result(int *x,int *b,char sname[25],char name[25]);//shows result, win or loose

main()
{

int i,j,z,n,b=0,c=0,d=0,x=0,ggg,kk;
int y=0,o=0,ddd=-1;
char array[25],name[25];
char item,test[26],q;

cout<<"\n\n\t\t\t      Important Notes";
cout<<"\n\t\t\t      ===============";
cout<<"\n\n\tThis game is played using keyboard only.\n\n\tYou have to Guess the name of a person, place, city or player etc.";
cout<<"\n\n\tTo play enter an alphabet and press enter.\n\n\tYou have only 6 chances of mistakes otherwise you loose the game.";

cout<<"\n\n\n\n\tEnter your name : ";
gets(name);

start:

kk=0;

b=0;c=0;d=2;x=0;y=0;o=0;
struct
{
char id[50];
char name[25];
}std[41];
n=41;

time_t t;
srand(unsigned(time(&t)));
z=rand()%n;

if(ddd==z)
goto start;

strcpy(std[0].id,"Current Cricket Player");
strcpy(std[0].name,"shahid khan afridi");

strcpy(std[1].id,"Current Cricket Player");
strcpy(std[1].name,"brain lara");

strcpy(std[2].id,"Current Cricket Player");
strcpy(std[2].name,"glenn mcgrath");

strcpy(std[3].id,"Current Cricket Player");
strcpy(std[3].name,"shoaib akhtar");

strcpy(std[4].id,"Current Cricket Player");
strcpy(std[4].name,"shane warne");

strcpy(std[5].id,"Current Cricket Player");
strcpy(std[5].name,"stephen flemming");

strcpy(std[6].id,"Former Football Player");
strcpy(std[6].name,"roberto bagio");

strcpy(std[7].id,"Current Football Player");
strcpy(std[7].name,"oliver kahn");

strcpy(std[8].id,"Former World Leader");
strcpy(std[8].name,"nelson mandela");

strcpy(std[9].id,"Current World Leader");
strcpy(std[9].name,"jacques chirac");

strcpy(std[10].id,"Current World Leader");
strcpy(std[10].name,"gerhard scheroder");

strcpy(std[11].id,"Former World Leader");
strcpy(std[11].name,"colin powel");

strcpy(std[12].id,"Former World Leader");
strcpy(std[12].name,"yasir arafat");

strcpy(std[13].id,"Great Leader");
strcpy(std[13].name,"muhammad ali jinnah");

strcpy(std[14].id,"Great Leader");
strcpy(std[14].name,"ibrahim lincon");

strcpy(std[15].id,"Place/Point");
strcpy(std[15].name,"mount averest");

strcpy(std[16].id,"International City");
strcpy(std[16].name,"manchester");

strcpy(std[17].id,"International City");
strcpy(std[17].name,"montreol");

strcpy(std[18].id,"International City");
strcpy(std[18].name,"cairo");

strcpy(std[19].id,"International City");
strcpy(std[19].name,"viena");

strcpy(std[20].id,"Personality");
strcpy(std[20].name,"imran khan");

strcpy(std[21].id,"International city");
strcpy(std[21].name,"dubai");

strcpy(std[22].id,"Current Hockey Player");
strcpy(std[22].name,"kashif jawad");

strcpy(std[23].id,"Current Hockey Player");
strcpy(std[23].name,"tariq aziz");

strcpy(std[24].id,"Current Tennis Player");
strcpy(std[24].name,"andy roddick");

strcpy(std[25].id,"Current Sports Player");
strcpy(std[25].name,"lance armstrong");

strcpy(std[26].id,"Current Sports Player");
strcpy(std[26].name,"michael shumacher");

strcpy(std[27].id,"Current Pakistani Actor");
strcpy(std[27].name,"talat hussain");

strcpy(std[28].id,"Current American Actor");
strcpy(std[28].name,"leonardo de capri");

strcpy(std[29].id,"Current American Star");
strcpy(std[29].name,"undertaker");

strcpy(std[30].id,"Current Pakistani Actor");
strcpy(std[30].name,"firdous jamal");

strcpy(std[31].id,"Current Pakistani Singer");
strcpy(std[31].name,"rahim shah");

strcpy(std[32].id,"Current Pakistani Singer");
strcpy(std[32].name,"najam sheraz");

strcpy(std[33].id,"Current Pakistani Actor");
strcpy(std[33].name,"sohail asghar");

strcpy(std[34].id,"Current Sports Player");
strcpy(std[34].name,"tiger woods");

strcpy(std[35].id,"Current Sports Player");
strcpy(std[35].name,"roger federer");

strcpy(std[36].id,"Current Cricket Player");
strcpy(std[36].name,"yousaf youhanna");

strcpy(std[37].id,"International City");
strcpy(std[37].name,"berlin");

strcpy(std[38].id,"Place/Point");
strcpy(std[38].name,"Eiffel Tower");

strcpy(std[39].id,"Former American Star");
strcpy(std[39].name,"hulk hogan");

strcpy(std[40].id,"Former Cricket Player");
strcpy(std[40].name,"garfield sobers");

ggg=0;

for(i=0;i<25;i++)
{
array[i]='_';
}

for(i=0;i<26;i++)
{
test[i]=' ';
}

getready(array,std[z].name,&x,&ggg);

while(c!=6 && b<strlen(std[z].name)-x)
{
point1:

display(name);

cout<<"\n\n\t\tTo skip play, enter 1";
cout<<"\n\n\t\t\Guess Type : "<<std[z].id;
cout<<"\n\n\tTries Left = "<<(6-c);
cout<<"\n\n\t";

for(j=0;j<strlen(std[z].name);j++)
{
cout<<array[j]<<" ";
}

cout<<"\n\n\tHow many guess left : "<<(strlen(std[z].name)-b-ggg);

if(kk==1)
goto result;

used_letters(&y,test);

if(o==0)
{
cout<<"\n\n\tEnter your guess : ";
cin>>item;
item=tolower(item);
d=0;
}

if(item=='1')
{
ddd=z;
goto start;
}

input_error(&o,&d,&item);

int c_i;
c_i = check_input(&y,&o,test,item);
if(c_i==1)
{
goto point1;
}

search(&b,&c,&d,&y,item,std[z].name,array);

}

if(kk==0)
{
kk=1;
goto point1;
}

result:
result(&x,&b,std[z].name,name);

cout<<"\n\n\tTo play again, enter Y or press any other key to exit : ";
cin>>q;

ddd=z;

if(q=='y' || q=='Y')
goto start;

clrscr();
cout<<"\n\t\t\tThank you for playing Guess Game (V.1)";
cout<<"\n\n\t\t\t\tThe game is over";
cout<<"\n\n\t\t\t\t   Kashif khan";
cout<<"\n\n\t\t\tFor Contact : kashifkhan14@yahoo.com";
return 0;
}

//functions

display(char name[25])
{
clrscr();
cout<<"\n\t\t\tWelcome "<<name<<" to Guess Game (V.1)";
cout<<"\n\t\t\tCopyright 2005, Kashif Khan";
return 0;
}

getready(char array[25],char name[25],int *x,int *ggg)//prepares arrays for playing
{
int i;
for(i=0;i<strlen(name);i++)
{
if(name[i]==' ')
{
*ggg=*ggg+1;
*x=*x+1;
array[i]=' ';
}
}
return 0;
}

used_letters(int *y,char test[25])
{
int h;
if(*y>0)
{
cout<<"\n\n\tThese letters are used : [";
for(h=0;h<*y;h++)
{
if(h==0)
cout<<test[h]<<"";
else
cout<<","<<test[h];
}
cout<<"]";
}
return 0;
}

input_error(int *o,int *d,char *item)//displays input errors
{
if(*o==1)
{
cout<<"\n\n\tThis letter is used. Enter your guess : ";
cin>>*item;
*item=tolower(*item);
*d=0;
*o=0;
}

if(*o==3)
{
cout<<"\n\n\tDon't enter invalid characters (except 1 for skipping).\n\tEnter your guess : ";
cin>>*item;
*item=tolower(*item);
*d=0;
*o=0;
}
return 0;
}

check_input(int *y,int *o,char test[25],char item)//ckecks inputs
{
int c=0;
if(item=='a' || item=='b' || item=='c' || item=='d' || item=='e' || item=='f' || item=='g' || item=='h' || item=='i' || item=='j' || item=='k' || item=='l' || item=='m' || item=='n' || item=='o' || item=='p' || item=='q' || item=='r' || item=='s' || item=='t' || item=='u' || item=='v' || item=='w' || item=='x' || item=='y' || item=='z')
{
*o=0;
}
else
{
*o=3;
c=1;
}
for(int f=0;f<26;f++)
{
if(item==test[f])
{
*o=1;
c=1;
}
}
test[*y]=item;
return(c);
}

search(int *b,int *c,int *d,int *y,char item,char name[25],char array[25])//searches input, compares with the data in the array
{
int k;
for(k=0;k<strlen(name);k++)
{
if(name[k]==' ')
{
array[k]=' ';
}
else
{
if(item==name[k])
{
array[k]=item;
*b=*b+1;
*d=1;
}
}
}
*y=*y+1;

if(*d==0)
{
*c=*c+1;
}
return 0;
}

result(int *x,int *b,char sname[25],char name[25])//shows result, win or loose
{
if(*b==strlen(sname)-*x)
{
cout<<"\n\n\t\tCongratulations! "<<name<<", you have won.";
}
else
{
cout<<"\n\n\tGame Over. Sorry! "<<name<<", you have lost.";
cout<<"\n\n\tThe answer is : "<<sname;
}
return 0;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.