-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgame.c
243 lines (199 loc) · 7.74 KB
/
game.c
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
#include<stdio.h> /*Header file declaration*/
#include<string.h> /*<string.h> for strcmp();,strlen(); functions use*/
#include<stdlib.h>
void showHangman(int);
int main(void)
{
char hangmanWord[100], tempWord[100]; /**hangmanWord[] array for the original word and tempWord[] array to get the alphabet from user and compare it with original word**/
char hangmanOutput[100]; /**This array will show the remaining blanks and correct inputs**/
int wrongTry = 6 , matchFound = 0; /**player will get 5 chance, so we use wrongTry as chance counter**/
/**matchFound to search the alphabet, if the alphabet from user does not exist
in the original word it will remain 0, upon finding the word, matchFound will
be set as 1**/
int counter = 0 , position = 0, winner, length , i;
char alphabetFromUser;
system("cls"); /**for clearing the screen**/
printf("\n\n Enter any word in small case and hit >>ENTER<<");
printf("\n\n\t Enter HERE ==> ");
scanf("%s",hangmanWord); /**get the string from opponent**/
printf("\n\n Now give the COMPUTER to your friend and see if he/she can CRACK it!!!");
printf("\n\n\tHIT >>ENTER<<");
getchar(); /**hold the computer screen**/
length = strlen(hangmanWord); /**get the length of the word**/
system("cls");
printf("\n\n !!!!!!!!!!!!!!!!!!!Welcome to the HANGMAN GAME!!!!!!!!!!!!!!!!!\n\n\n"); /**Brief description of the game**/
printf("\n\n You will get 5 chances to guess the right word");
printf("\n\n So help the Man and get...set...GO..!!");
getchar();
printf("\n\n\tHIT >>ENTER<< ");
getchar();
system("cls");
printf("\n\t||===== "); /**show the HANGMAN**/
printf("\n\t|| | ");
printf("\n\t|| ");
printf("\n\t|| ");
printf("\n\t|| ");
printf("\n\t|| ");
printf("\n\n The word has %d alphabets \n\n",length); /**tell the user how many alphabets the word has**/
for( i = 0; i < length ; i++)
{
hangmanOutput[i] = '_';
hangmanOutput[length] = '\0';
}
for(i = 0 ; i < length ; i++)
{
printf(" ");
printf("%c",hangmanOutput[i]); /**Show the Word With n(length of the original word) number of underscores (_)**/
}
while(wrongTry != 0) /**while loop for exiting the program when no try left**/
{
matchFound = 0;
printf("\n\n enter any alphabet from a to z and please use small case!!");
printf("\n\n\t Enter HERE ==> ");
fflush(stdin);
scanf("%c",&alphabetFromUser); /**get alphabet from user**/
if(alphabetFromUser < 'a' || alphabetFromUser > 'z') /**In case player gives input other than 'a' to 'z' the console will ask again**/
{
system("cls");
printf("\n\n\t Wrong input TRY AGAIN ");
matchFound = 2;
}
fflush(stdin);
if (matchFound != 2)
{
for(counter=0;counter<length;counter++) /**for loop to check whether player input alphabet exists or not in the word**/
{
if(alphabetFromUser==hangmanWord[counter])
{
matchFound = 1;
}//end of if()
}//end of for()
if(matchFound == 0) /**in case of wrong guess**/
{
printf("\n\t :( You have %d tries left ",--wrongTry);
getchar();
showHangman(wrongTry);
getchar();
}//end of if()
else
{
for(counter = 0; counter < length; counter++)
{
matchFound = 0;
if(alphabetFromUser == hangmanWord[counter])
{
position = counter ;
matchFound = 1;
}//end of if
if(matchFound == 1)
{
for(i = 0 ; i < length ; i++)
{
if( i == position)
{
hangmanOutput[i] = alphabetFromUser; /**Put the alphabet at right position**/
}
else if( hangmanOutput[i] >= 'a' && hangmanOutput[i] <= 'z' ) /** If the position already occupied
by same alphabet then no need to
fill again EASY!! and continue */
{
continue;
}
else
{
hangmanOutput[i] = '_'; /** Put a blank at not guessed alphabet position **/
}
}
tempWord[position] = alphabetFromUser; /**put the alphabet in another char array to check with the original word**/
tempWord[length] = '\0'; /**put the NULL character at the end of the temp string**/
winner = strcmp(tempWord,hangmanWord); /**upon True comparison it will return 0**/
if(winner == 0) /**if the player guessed the whole word right then he/she is the WINNER**/
{
printf("\n\n\t \t YAHOO!!!!! You are the WINNER !!!!!");
printf("\n\n\t The Word was %s ",hangmanWord);
printf("\n\n\n\n\t\tEASY HUH???\n\n");
getchar();
return 0;
}//end of inner if
}//end of outer if
}//end of for loop
}//end of else
}// end of if(matchFound != 2) condition
printf("\n\n\t");
for(i = 0 ; i < length ; i++)
{
printf(" ");
printf("%c",hangmanOutput[i]); /**Show the original Word With blanks and right Input alphabet**/
}
getchar();
}//end of while loop
if(wrongTry <= 0) /**if the player can not guess the whole word in 5 chaces**/
{
printf("\n\n\t The Word was %s ",hangmanWord);
printf("\n\n\t The man is dead you IDIOT!!!!!");
printf("\n\n\t Better luck next!!!");
}
getchar();
return 0;
}//end of main();
void showHangman(int choice) /**This function show the hangman after each wrong try**/
{
switch(choice)
{
case 0:
system("cls");
printf("\n\t||===== ");
printf("\n\t|| | ");
printf("\n\t|| %cO/",'\\');
printf("\n\t|| | ");
printf("\n\t|| / %c",'\\');
printf("\n\t|| ");
break;
case 1:
system("cls");
printf("\n\t||===== ");
printf("\n\t|| | ");
printf("\n\t|| %cO/",'\\');
printf("\n\t|| | ");
printf("\n\t|| %c",'\\');
printf("\n\t|| ");
break;
case 2:
system("cls");
printf("\n\t||===== ");
printf("\n\t|| | ");
printf("\n\t|| %cO/",'\\');
printf("\n\t|| | ");
printf("\n\t|| ");
printf("\n\t|| ");
break;
case 3:
system("cls");
printf("\n\t||===== ");
printf("\n\t|| | ");
printf("\n\t|| %cO/",'\\');
printf("\n\t|| ");
printf("\n\t|| ");
printf("\n\t|| ");
break;
case 4:
system("cls");
printf("\n\t||===== ");
printf("\n\t|| | ");
printf("\n\t|| %cO ",'\\');
printf("\n\t|| ");
printf("\n\t|| ");
printf("\n\t|| ");
break;
case 5:
system("cls");
printf("\n\t||===== ");
printf("\n\t|| | ");
printf("\n\t|| O ");
printf("\n\t|| ");
printf("\n\t|| ");
printf("\n\t|| ");
break;
}//end of switch-case
return;
}