-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPROJECT.CPP
More file actions
264 lines (240 loc) · 5.84 KB
/
PROJECT.CPP
File metadata and controls
264 lines (240 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#define MAXMISSES 6
#define MAXLENGTH 30
class player
{
public:
char name[30];
long int score;
player()
{
score=0;
strcpy(name,"Unknown");
}
void determine_score(char[]);
}ob;
void player::determine_score(char word[])
{
if(strlen(word)>10)
score+=10;
else if(strlen(word)>5)
score+=20;
else
score+=30;
}
void display_rules() //To display Rules of the game.
{
clrscr();
cout<<"\n\t\tWelcome to Hangman! Guess an Automobile Brand Name.";
cout<<"\n\n\t\tEach letter is represented by a star.";
cout<<"\n\n\t\tYou have to type only one CAPITAL letter in one try.";
cout<<"\n\n\t\tIf the letter is in the word(s), then it would appear";
cout<<"\n\n\t\teverywhere in the word(s).";
cout<<"\n\n\t\tIf the letter isn't in the word(s), then a body part";
cout<<"\n\n\t\tis added to the gallows.";
cout<<"\n\n\t\tYou can continue guessing letters until you get the";
cout<<"\n\n\t\tword(s) or all 6 body parts are on the gallows.";
cout<<"\n\n\n\t\tPress 'ENTER' to continue...";
getchar();
}
void hangman(int b) //Function for displaying hangman gallows.
{
switch(b)
{
case 0 : cout<<"\n_____";
cout<<"\n | ";
break;
case 1 : cout<<"\n_____";
cout<<"\n | ";
cout<<"\n O ";
break;
case 2 : cout<<"\n_____";
cout<<"\n | ";
cout<<"\n O ";
cout<<"\n | ";
break;
case 3 : cout<<"\n_____";
cout<<"\n | ";
cout<<"\n O ";
cout<<"\n /| ";
break;
case 4 : cout<<"\n_____";
cout<<"\n | ";
cout<<"\n O ";
cout<<"\n /|\\";
break;
case 5 : cout<<"\n_____";
cout<<"\n | ";
cout<<"\n O ";
cout<<"\n /|\\";
cout<<"\n / ";
break;
case 6 : cout<<"\n_____";
cout<<"\n | ";
cout<<"\n O ";
cout<<"\n /|\\";
cout<<"\n / \\";
break;
}
}
void initword(char[],char[]);
int updateword(char,char[],char[]);
void main()
{
display_rules();
cout<<"\nEnter your name : ";
cin>>ob.name;
char choice='y';
while(choice=='y'||choice=='Y')
{
int no_of_misses = 0 ,btcounter=0,j;
char letter;
char badtries[]="******";
char word[MAXLENGTH], dummy[MAXLENGTH];
char words[][MAXLENGTH]= {
"AUDI",
"PORSCHE",
"BMW",
"FIAT",
"HONDA",
"BAJAJ",
"TATA",
"TVS",
"VOLVO",
"FORCE",
"TOYOTA",
"JAGUAR",
"NISSAN",
"HYUNDAI",
"CHEVROLET",
"CHRYSLER",
"BENTLEY",
"FORD",
"BUGATTI",
"PREMIER",
"RENAULT",
"HUMMER",
"SKODA",
"FERRARI",
"MAHINDRA",
"STANDARD",
"MITSUBISHI",
"LAMBORGHINI",
"VOLKSWAGEN",
"MERCEDES BENZ",
"ASTON MARTIN",
"LAND ROVER",
"ASHOK LEYLAND",
"ROYAL ENFIELD"
}; //No. of strings:34
randomize();
int n = random(34);
strcpy(word,words[n]); //Generates a random string.
initword(word,dummy); //To create a hidden format of the string.
while(no_of_misses < MAXMISSES)
{
clrscr();
if(strcmp(badtries,"******")!=0) //To show the letters entered
{ //by the user that are not
cout<<"\nBad Tries: "; //present in the string.
for(j=0;j<no_of_misses;j++)
cout<<badtries[j]<<" ";
}
hangman(no_of_misses);
cout<<"\n\n";
puts(dummy);
cout<<"\nGuess a letter: ";
cin>>letter;
int temp = updateword(letter,word,dummy); //To update the hidden format of the string
//with the letters present in the string that
if( temp==-1 ) //are entered by the user.
{
cout<<"\n\nAlready Guessed, please TRY AGAIN!"; //Validation to prevent
} // same letter guessing.
else if( temp==0 )
{
int dtmnr=0;
for(int i=0;i<btcounter;i++)
if(letter==badtries[i]) //Validation to prevent same
{ //letter guessing.
cout<<"\n\nAlready Guessed, please TRY AGAIN!";
++dtmnr;
break;
}
if(!dtmnr)
{
cout<<"\n\nWhoops! That letter isn't in there!\n";
++no_of_misses;
badtries[no_of_misses-1]=letter;
++btcounter;
}
}
else {
cout <<"\n\nYou found a letter! Isn't that exciting!" ;
}//End of ELSE
cout << "\n\nYou have " << MAXMISSES - no_of_misses<< " guesses left.";
if (strcmp(word, dummy) == 0)
{
clrscr();
cout <<"\n"<<word;
cout <<"\nYeah! You got it!";
ob.determine_score(word);
break;
} //End of IF
cout<<"\n\nPress 'ENTER' to continue.";
getchar();
}//END of 2nd WHILE
if(no_of_misses == MAXMISSES)
{
clrscr();
hangman(no_of_misses);
cout << "\n\nSorry, you lose...you've been hanged." << endl;
cout << "The word was : " << word << endl;
}
cout<<"\n\nDo you want to continue?(Y/N) ";
cin>>choice;
}//End of 1st WHILE
cout<<"\nPlayer name is "<<ob.name;
cout<<"\nPlayer score is "<<ob.score;
ofstream file("player-list.dat",ios::app|ios::binary);
if(!file)
{
cout<<"Cannot open file!!";
exit(1);
}
file<<ob.name<<" "<<ob.score<<"|";
file.close();
getch();
}
void initword(char word[], char dummy[])
{
int i;
for(i=0 ;word[i]!='\0' ; i++)
{
if(word[i]!=' ')
dummy[i]='*';
else
dummy[i]=' ';
}
dummy[i]='\0';
}
int updateword(char letter, char word[], char dummy[])
{
int i;
int matches=0;
for(i=0; word[i]!='\0'; i++)
{
if(letter==dummy[i])
return -1;
if(letter==word[i])
{
dummy[i]=letter;
matches++;
}
}
return matches;
}