-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordGuesssinggame.c
More file actions
314 lines (308 loc) · 8.09 KB
/
wordGuesssinggame.c
File metadata and controls
314 lines (308 loc) · 8.09 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
char guess[10];
int chances;
// FUNCTION
void update_dashes(char guess[], int index, char dashes[], char secret_word[]);
char get_guess(char secret_word[])
{
// guesses left
if (strlen(secret_word) > 6)
{
chances = 5;
}
else
{
chances = strlen(secret_word) - 1;
}
// DASHES
char dashes[strlen(secret_word)];
// this array is to store entered guesses to stop when user enters again same value
char guesses[strlen(secret_word)];
for (int i = 0; i < strlen(secret_word); i++)
{
// initializing all values to null to remove garbage
guesses[i] = '\0';
}
// printing dashes
for (int i = 0; i < strlen(secret_word); i++)
{
dashes[i] = '_';
printf(" %c", dashes[i]);
}
int k = 0, j = 0;
// guess checking here
while (chances > 0)
{
printf("\n%d guesses left", chances);
printf("\nEnter the letter\n");
int i, count = 0, l, count1 = 0;
gets(guess);
if (strlen(guess) != 1)
{
// if guess is of more than one word the program again prompts
printf("Your guess must be of one character.");
continue;
}
if (isdigit(guess[0]) || isupper(guess[0]) || isblank(guess[0]))
{
// if guess is a upper case or any digit so the program again prompts
printf("Your guess must be a lowercase alphabet.");
continue;
}
if (isalpha(guess[0]))
{
// storing guess into guesses
guesses[j] = guess[0];
j++;
for (int a = 0; a < strlen(guesses); a++)
{
if (guesses[a] == guess[0])
{
count1++;
}
}
// if guess is previously entered show message and again prompt
if (count1 > 1)
{
printf("You have entered this previously.");
count1 = 0;
continue;
}
// if guess in the word.
for (i = 0; i < strlen(secret_word); i++)
{
if (guess[0] == secret_word[i] && count != 1)
{
printf("Your guess is in the word!\n");
int index = i;
update_dashes(guess, index, dashes, secret_word);
count++;
}
}
// if guess not in word
if (count == 0)
{
printf("Incorrect guess.\n");
chances--;
if (chances == 0)
{
// terminating program if chances finished
printf("You lose, the word was ");
for (i = 0; i < strlen(secret_word); i++)
{
printf("%c", secret_word[i]);
}
exit(0);
}
// printing dashes again
for (i = 0; i < strlen(secret_word); i++)
{
printf(" %c", dashes[i]);
}
continue;
}
}
else
{
printf("Please enter a lowercase alphabet\n");
continue;
}
}
}
void update_dashes(char guess[], int index, char dashes[], char secret_word[])
{
int i, l = 0, count;
// replacing
for (i = 0; i < strlen(secret_word); i++)
{
if (guess[0] == secret_word[i])
{
dashes[i] = guess[0];
}
}
// replacing preview
for (i = 0; i < strlen(secret_word); i++)
{
printf(" %c", dashes[i]);
}
// To stop, if word is completed correctly (even if "chances" are left)
for (count = 0; count < strlen(secret_word); count++)
{
if (dashes[count] == secret_word[count])
{
l++;
}
}
if (l == strlen(secret_word))
{
printf("\nYou won Congrats.\n");
exit(0);
}
count = 0;
l = 0;
}
int random(int n) // Function to generate random number
{
srand(time(NULL));
int num = rand() % n;
return num;
}
int main()
{
char fruit[][20] = {"apple", "watermelon", "orange", "pear", "cherry",
"stawberry", "grape", "mango", "pomegranate", "plum",
"apricot", "grapefruit", "melon", "banana", "coconut", "peach",
"avocado", "custardapple", "guava", "pumpkin", "lychee", "sapodilla",
"dates", "fig", "cranberry", "honepoot", "jackfruit", "dragonfruit",
"kiwi", "olive", "sugarcane"};
char country[][20] = {"indonesia", "china", "america", "india",
"dubai", "turkey", "afghanistan", "australia", "bangladesh", "austria", "maldives", "singapore",
"malaysia", "germany", "france", "norway", "kuwait", "brazil", "qatar", "russia",
"zinbabwe", "portugal"};
char food[][20] = {"rice", "milk", "chocolate", "pasta", "lasagne", "chowmein", "burger", "broast", "roll", "sandwich", "steak", "kebab", "spaghetti", "salad", "seafood", "bacon", "nuggets", "mushroom", "mandi", "hotdogs", "sausage"};
char Animal[][20] = {"chicken", "duck", "deer", "cow", "buffalow", "horse",
"elephant", "goat", "lion", "zebra", "cat", "dog", "fish", "camel",
"donkey", "parrot", "butterfly", "eagle"};
char things[][20] = {"pillow", "fork", "book", "boat", "table", "panaflex", "banner", "camera", "bolt", "bucket", "bulb", "almirah", "album", "cloth", "desk", "container", "stove", "heater", "cutlery"};
char movie[][20] = {"jumanji", "braveheart", "ironman", "maverick", "avatar", "martian", "interstellar", "zootopia", "virus", "titanic", "extraction", "gravity", "inception", "matrix", "alien", "morbius", "dune", "tenet", "sherdil", "malik"};
system("cls"); // clearing screen before game
srand(time(NULL)); // to flush memory so the system generate a new number
char chr[5], chr1[5];
char secret_word[20];
int num, a = 20;
printf("\t***** Welcome to word guessing game! ***** \n");
printf("There are two modes Easy and Hard.\nIn easy mode you have to select the category from which you want the secret word to be chosen and in hard mode the secret word will be selected from any random category.\n");
while (1)
// loop using to validate user for incorrect inputs
{
printf("Enter mode (Press E for Easy) or (Press H for Hard): ");
gets(chr1);
fflush(stdin);
if (strlen(chr1) == 1 && isalpha(chr1[0]))
{
if (chr1[0] == 'e' || chr1[0] == 'E')
// EASY Mode
{
printf("Choose the category\n");
while (1)
// loop using to validate user for incorrect inputs
{
printf("Press a for Animal\n");
printf("Press f for Fruits\n");
printf("Press c for country\n");
printf("Press t for Thing\n");
printf("Press d for Food\n");
printf("Press m for Movies\n");
printf("Enter the option: ");
scanf("%s", &chr);
fflush(stdin);
if (strlen(chr) == 1 && isalpha(chr[0]))
{
// generating word of user's desired category
switch (chr[0])
{
case 'd':
case 'D':
num = random(a);
strcpy(secret_word, food[num]); // used this function instead of using for loop because the program looks lengthy
break;
case 'a':
case 'A':
num = random(a);
strcpy(secret_word, Animal[num]);
break;
case 'F':
case 'f':
num = random(a);
strcpy(secret_word, fruit[num]);
break;
case 'c':
case 'C':
num = random(a);
strcpy(secret_word, country[num]);
break;
case 't':
case 'T':
num = random(a);
strcpy(secret_word, things[num]);
break;
case 'm':
case 'M':
num = random(a);
strcpy(secret_word, movie[num]);
break;
default:
printf("Invalid option\n\n");
continue;
// break;
}
break;
}
else
{
printf("Invalid input\n\n");
continue;
}
}
}
else if (chr1[0] == 'h' || chr1[0] == 'H')
{
// HARD MODE
int num1 = 6;
// generating a number to generate a random category
num = random(num1);
// selecting categories randomnly
if (num == 0)
{
num1 = random(a);
strcpy(secret_word, food[num1]); // used this function instead of using for loop because the program looks lengthy
}
else if (num == 1)
{
num1 = random(a);
strcpy(secret_word, Animal[num1]);
}
else if (num == 2)
{
num1 = random(a);
strcpy(secret_word, fruit[num1]);
}
else if (num == 3)
{
num1 = random(a);
strcpy(secret_word, country[num1]);
}
else if (num == 4)
{
num1 = random(a);
strcpy(secret_word, things[num1]);
}
else if (num == 5)
{
num1 = random(a);
strcpy(secret_word, movie[num1]);
}
}
else
{
printf("Invalid choice\n");
continue;
}
break;
}
else
{
printf("Invalid input\n");
continue;
}
}
// Now word generated
// calling function for further game steps
get_guess(secret_word);
return 0;
}