-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hangman project final.cpp
362 lines (298 loc) · 11.1 KB
/
Hangman project final.cpp
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
//Function declaration.
void shape(int);
int main()
{
int i,j,k,l,count1,count2,score=120,number,option,pcount,p;
char a[30],b[30],c[30],d[30],poriginal[10],pduplicate[10];
strcpy(poriginal,"vishal");//"poriginal contains the saved password. Password is copied to poriginal.
for(pcount=0;pcount<5;pcount++)//Loop gives 5 chances to enter the password.
{
printf("\t\t\t******Enter Password*****\n");
gets(pduplicate);//"pduplicate" contians the entered password. Entered word id copied into "pduplicate".
system("cls");
p=strcmp(poriginal,pduplicate);//Compares the entered and saved password.
if(p==0)//If password is correct is correct it enters to the game.
{
printf("Choose an option:\n1.Play\n2.Rules\n3.Quit\n");
scanf("%d",&option);//"option" is for selecting rules or game or exit.
for(;option!=3;)//Loop make sure that the program to run till we want to exit the game.
{
switch(option)//Select whether to play game or to see rules.
{
//To play game.
case 1:system("cls");
//To generate a random number.
srand(time(NULL));
number=rand()%10+1;//"number" is to select the question.
//To select a clue and the word.
switch(number)
{
case 1: a[0]='v';a[1]='o';a[2]='l';a[3]='k';a[4]='s';a[5]='w';a[6]='a';a[7]='g';a[8]='e';a[9]='n';a[10]='\0';
printf("German car company founded by Nazis\n");
break;
case 2: a[0]='h';a[1]='o';a[2]='n';a[3]='d';a[4]='a';a[5]='\0';
printf("Largest Japanese motorcycle manufacturers\n");
break;
case 3: a[0]='b';a[1]='e';a[2]='e';a[3]='t';a[4]='l';a[5]='e';a[6]='\0';
printf("Famous car model released by Volkswagen");
break;
case 4: a[0]='b';a[1]='o';a[2]='e';a[3]='i';a[4]='n';a[5]='g';a[6]='\0';
printf("Aircraft manufacturer\n");
break;
case 5: a[0]='t';a[1]='o';a[2]='y';a[3]='o';a[4]='t';a[5]='a';a[6]='\0';
printf("Largest car manufacturers in Japan\n");
break;
case 6: a[0]='m';a[1]='i';a[2]='t';a[3]='s';a[4]='u';a[5]='b';a[6]='i';a[7]='s';a[8]='h';a[9]='i';a[10]='\0';
printf("Car and aircraft manufacturer\n");
break;
case 7: a[0]='c';a[1]='o';a[2]='r';a[3]='v';a[4]='e';a[5]='t';a[6]='t';a[7]='e';a[8]='\0';
printf("Famous car model released by chevrolet\n");
break;
case 8: a[0]='b';a[1]='e';a[2]='n';a[3]='t';a[4]='l';a[5]='y';a[6]='\0';
printf("Handcrafted car manufacturer\n");
break;
case 9: a[0]='p';a[1]='o';a[2]='r';a[3]='s';a[4]='c';a[5]='h';a[6]='e';a[7]='\0';
printf("Sports car manufacturer\n");
break;
case 10: a[0]='m';a[1]='u';a[2]='s';a[3]='t';a[4]='a';a[5]='n';a[6]='g';a[7]='\0';
printf("Famous car model by ford\n");
break;
default: a[0]='t';a[1]='a';a[2]='t';a[3]='a';a[4]='\0';
printf("Indian car manufacturer\n");
}
//To find the lenght of the word.
i=strlen(a);
//To tell the player how letters are there in the word.
for(j=0;j<i;j++)
{
printf("_ ");
}
//For initializing the guessed answer(b) and typed alphabets array(d) to space for printing.
for(j=0;j<30;j++)
{
b[j]=' ';
d[j]=' ';
}
//To give a blank for answer.
for(j=0;j<i;j++)
{
b[j]='_';
}
//Main logic: To find the gussed letters are correct or not.
printf("\nEnter an alphabet:");
scanf(" %c",&c[0]);/*Array "c" takes all the inputed alphabets.*/
for(k=0,count2=0;k<i;)/*"k" coutines till the lenght of the word."i" is the lenght of the answer.count2" keeps the tracks of number of alphabets entered.*/
{
//To ensure the aplhabet is not entered already. If entered already it ask for new one to enter.
for(j=0;j<=count2;j++)
{
if(d[j]==c[k])
{
printf("\nYou have already entered this alphabet\nEnter other alphabet:");
scanf(" %c",&c[k]);
j=0;
}
}
d[count2]=c[k];/*String "d" stores all the aplhabets entered by the players.*/
for(l=0,count1=0;l<i;l++)//To find the gussed letter is correct and count how many times the alphabet is repeted in the word.
{
if(a[l]==c[k])
{
b[l]=c[k];/*Array "b" stores the typed correct answer alphabets.*/
count1++;//"count1" counts numbers same repeated alphabet in the clue.
}
}
//If the letter matches if asks for next alphabet, if it dosen't match it's asks to enter other alphabet.
if(count1>0)
{
system("cls");//To clear old things on the output screen.
switch(number)//To show the clue.
{
case 1: printf("German car company founded by Nazis\n");
break;
case 2: printf("Largest Japanese motorcycle manufacturers\n");
break;
case 3: printf("Famous car model released by Volkswagen");
break;
case 4: printf("Aircraft manufacturer\n");
break;
case 5: printf("Largest car manufacturers in Japan\n");
break;
case 6: printf("Car and aircraft manufacturer\n");
break;
case 7: printf("Famous car model released by chevrolet\n");
break;
case 8: printf("Handcrafted car manufacturer\n");
break;
case 9: printf("Sports car manufacturer\n");
break;
case 10: printf("Famous car model by ford\n");
break;
default: printf("Indian car manufacturer\n");
}
shape(score);
k=k+count1;
printf("%s(Reamining Letters %d, Chances %d)\n",b,i-k,(score/20)+1);
if(k<i)
{
printf("Already entered alphabets:%s\nEnter next alphabet:",d);
scanf(" %c",&c[k]);
count2++;
}
}
else
{
system("cls");//To clear old things on the output screen.
switch(number)//To show the clue.
{
case 1: printf("German car company founded by Nazis\n");
break;
case 2: printf("Largest Japanese motorcycle manufacturers\n");
break;
case 3: printf("Famous car model released by Volkswagen");
break;
case 4: printf("Aircraft manufacturer\n");
break;
case 5: printf("Largest car manufacturers in Japan\n");
break;
case 6: printf("Car and aircraft manufacturer\n");
break;
case 7: printf("Famous car model released by chevrolet\n");
break;
case 8: printf("Handcrafted car manufacturer\n");
break;
case 9: printf("Sports car manufacturer\n");
break;
case 10: printf("Famous car model by ford\n");
break;
default: printf("Indian car manufacturer\n");
}
printf("\nWrong Guess\n");
score=score-20;
//Prints the character.
shape(score);
printf("\n%s(Remaining Letters %d, Chances %d)",b,i-k,(score/20)+1);
printf("\nAlready entered alphabets:%s\nEnter other alphabet:",d);
scanf(" %c",&c[k]);
count2++;
}
//If chances are done it exits the loop.
if(score==0)
{
k=33;
}
}
//prints the game status win or loose.
if(score>0)
{
printf("YOU WON");
}
else//Prints finall hangman if everything is wrong.
{
printf("_________\n");
printf("| |\n");
printf("| O\n");
printf("| /|\\ ");
printf("\n| |\n");
printf("| / \\\n");
printf("|\n");
printf("---------\n\n");
printf("Better luck next time\nAnswer:%s",a);
}
getch();
system("cls");
printf("Choose an option:\n1.Play\n2.Rules\n3.Quit\n");
scanf("%d",&option);
break;
//To see the rules.
case 2:system("cls");
printf("\t\t\t\tGAME RULES\n");
printf("Play individually or in groups.\nPlayer should guess the answer.");
printf("\nFor every word there is a clue and the number of alphabets in the letter are specified.");
printf("\nPlayer should type the alphabet, which he thinks would be the word.");
printf("\nChances:7\nEnjoy playing the game.\n\t\t\t\tPress any key");
getch();
system("cls");
printf("Choose an option:\n1.Play\n2.Rules\n3.Quit\n");
scanf("%d",&option);break;
}
}
pcount=6;//If password is correct the loop ends.
}
else//If the password is wrong it gives chance to re enter the password and gives clue.
{
printf("Wrong guess, Try Again\n(Hint:Nice Person)\n");
}
}
system("cls");
if(pcount==5)
{
printf("You can't play the game.");
}
return 0;
}
void shape(int n)//Is the function that prints the hangman.
{
switch(n)
{
//Prints hangman for first wrong guess.
case 100: printf("_________ \n");
printf("| \n");
printf("| \n");
printf("| \n");
printf("| \n");
printf("| \n");
printf("| \n");break;
//Prints hangman for second wrong guess.
case 80: printf("--------- \n\n");
printf("_________\n");
printf("| |\n");
printf("|\n");
printf("|\n");
printf("|\n");
printf("|\n");
printf("|\n");break;
//Prints hangman for third wrong guess.
case 60:printf("---------\n\n");
printf("_________\n");
printf("| |\n");
printf("| o\n");
printf("|\n");
printf("|\n");
printf("|\n");
printf("|\n");break;
//Prints hangman for fourth wrong guess.
case 40: printf("---------\n\n");
printf("_________\n");
printf("| |\n");
printf("| o\n");
printf("| |\n");
printf("|\n");
printf("|\n");
printf("|\n");break;
//Prints hangman for fifth wrong guess.
case 20:printf("---------\n\n");
printf("_________\n");
printf("| |\n");
printf("| o\n");
printf("| /|\\ ");
printf("\n|\n");
printf("|\n");
printf("|\n");
printf("---------\n\n");break;
//Prints hangman for sixth wrong guess.
case 0:printf("_________\n");
printf("| |\n");
printf("| O\n");
printf("| /|\\ ");
printf("\n| |\n");
printf("|\n");
printf("|\n");
printf("---------\n\n");
}
}