Saturday, 7 January 2017

Hangman (beta)


#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<ctype.h>
using namespace std;

int play();

void writef();
void readf();

class player

{
char name[20];
int score;
public:
player()
{
strcpy(name,"New Player");
score=0;
}
void updatescore();
void getname();
void stat();
int retscore();
};

player cur,obj;


void readf()

{
fstream f;
f.open("score.dat",ios::in|ios::binary);
if(f)
{
f.read((char*)&obj,sizeof(obj));
}
f.close();
}

void writef()

{
fstream f;
f.open("score.dat",ios::out|ios::binary|ios::trunc);
f.write((char*)& cur,sizeof(cur));
f.close();
}

int player::retscore()

{
return score;
}

void player::updatescore()

{
++score;
}

void player::stat()

{
cout<<"\n\nName:"<<name;
cout<<"\nScore:"<<score<<endl;
}

void player::getname()

{
cout<<"\n\t\t\tEnter name:";
gets(name);
cin.getline(name,20);
}

int play()

{
    cur.getname();
fstream file;
file.open("data.txt",ios::in);
char word[10],dash[10],c='!';
int temp;
while(1)
{
int m=rand() % 10 + 1,trial;
file.seekp (0);
for(int i=0;i<m;++i)
file>>word;
m=strlen(word);
strcpy(dash,word);
for(int i=0;i<m;++i)
dash[i]='-';
trial=m+3;
while(trial!=1)
{
            //system("cls");
temp=0;
for(int i=0;i<m;++i)
{

if(word[i]==c)

{
dash[i]=c;
                    temp=1;
}


}

if(strcmp(word,dash)==0)
{
cur.updatescore();
cout<<"The Name Was : "<<word<<endl;
cout<<"\nCongrags You've Won\nCurrent Score";
cur.stat();
c='!';
char ch;
cout<<"\nPlay again(y/n):";
cin>>ch;
if(ch=='y'||ch=='Y')
{
goto end;
}
else
{
trial=-1;
break;
}

}


--trial;

            if (temp)
++trial;
cout<<"\nGuess the name\n"<<trial<<" lives left\n\t\t\t\t";
puts(dash);
c='!';
cout<<"\nEnter a character:";
cin>>c;
c=toupper(c);

}



if(cur.retscore()>obj.retscore())

{ writef();
file.close();
}

if(trial==-1)

{
return 0;
}
if(strcmp(word,dash))
{
cout<<"\nBetter luck next time:)\n";
getch();
file.close();
return 0;
}

end:

    ;
}


}


void highscore()

{
system("cls");
readf();
cout<<"\n\t\t##################High Score##################";
obj.stat();
cout<<"\n";
}
int main()
{ int op;
readf();

while(1)

{
        op=-1;
system("cls");
cout<<"1.Play\n2.View Highscore\n3.About\n4.Exit\nEnter choice:";
cin>>op;
if(op==1)
play();
else if(op==2)
{ highscore();
getch();
}
else if(op==3);

else if (op==4)

exit(0);
else
{
            cout<<"\nInvalid Choice";
getch();
        }

}


}


download