-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
330 lines (286 loc) · 11.2 KB
/
main.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
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
#include "generic_vector.h"
#include "my_string.h"
#include "associativearray.h"
#include "status.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
GENERIC_VECTOR GetWholeDictionary(void);
#define MAX_LENGTH 30
#define MAX_LETTERS 26
Boolean Evil_Hangman(int word_length, int guess_amount, Boolean ShowTotalRemaining, GENERIC_VECTOR hVectorOfWordLenth);
int getWordLength(void);
int getGuessAmount(void);
char getCurrentGuess(MY_STRING guessList);
Boolean getCheatsChoice(void);
Boolean getReplay(void);
void printHangman(void);
GENERIC_VECTOR CheckIfGuessInWord(MY_STRING* phGuessList, GENERIC_VECTOR hVectorOfWordList, MY_STRING hCurrentKey, char currentGuess, Boolean showTotalRemaining);
void ClearKeyboardBuffer(void);
int main(int argc, char *argv[]) {
/* GetWholeDictionary */
GENERIC_VECTOR hVectorOfWholeDicitionary[MAX_LENGTH] = {NULL};
int i = 0, j;
FILE *fp;
for (i = 0; i < MAX_LENGTH; i++) {
hVectorOfWholeDicitionary[i] =
generic_vector_init_default(my_string_init_copy, my_string_destroy);
if (hVectorOfWholeDicitionary[i] == NULL) {
printf("Failed to allocate space for generic vector.\n");
exit(1);
}
}
MY_STRING hMyString = my_string_init_default();
if (hMyString == NULL) {
printf("Failed to get item at my_string_init_default for hMyString.\n");
exit(1);
}
fp = fopen("dictionary.txt", "r");
if (fp == NULL) {
printf("Failed to open dictionary.txt\n");
exit(1);
}
while (my_string_extraction(hMyString, fp)) {
j = my_string_get_size(hMyString);
if (j >= 0 && j < MAX_LENGTH)
generic_vector_push_back(hVectorOfWholeDicitionary[j], hMyString);
}
/* for(i = 0; i < MAX_LENGTH; i++) */
/* printf("%d:%d\n", i, generic_vector_get_size(hVectorOfWholeDicitionary[i])); */
fclose(fp);
my_string_destroy(&hMyString);
/* End of GetWhole Dictionary */
Boolean KeepGoing = TRUE;
do {
printHangman();
// prompt user for word length
int word_len = 0;
word_len = getWordLength();
// prompt user for number of guesses
int guesses = 0;
guesses = getGuessAmount();
// prompt user for total numbers of remaining words
Boolean ShowTotalRemaining = FALSE;
ShowTotalRemaining = getCheatsChoice();
// algorithm
//
// a) selecting the vector of all words that matches word len
Evil_Hangman(word_len, guesses, ShowTotalRemaining,
hVectorOfWholeDicitionary[word_len]);
KeepGoing = getReplay();
} while (KeepGoing);
/* Clean up */
for (i = 0; i < MAX_LENGTH; i++) {
generic_vector_destroy(&hVectorOfWholeDicitionary[i]);
}
return 0;
}
Boolean Evil_Hangman(int word_length, int guess_amount, Boolean ShowTotalRemaining, GENERIC_VECTOR hVectorOfWordLength){
//algorithm
//
//
int i, j;
GENERIC_VECTOR hNewWordFamily = NULL;
Boolean TheyWon= FALSE;
MY_STRING hMyString = NULL, hCurrentKey = NULL, hNewKey = NULL, hCurrentWord = NULL, guessedLetters = NULL;
char currentGuess;
//a) selecting the vector of all words that matches word len
guessedLetters = my_string_init_default();
if (guessedLetters == NULL) {
printf("Failed to get item at my_string_init_default for hMyString.\n");
exit(1);
}
hMyString = my_string_init_default();
if (hMyString == NULL) {
printf("Failed to get item at my_string_init_default for hMyString.\n");
exit(1);
}
hCurrentKey = my_string_init_default();
if (hCurrentKey == NULL) {
printf("Failed to get item at my_string_init_default for hMyString.\n");
exit(1);
}
hNewWordFamily = generic_vector_init_default(my_string_init_copy, my_string_destroy);
if (hNewWordFamily == NULL) {
printf("Failed to generic_vector_init_default for hNewWordFamily.\n");
exit(1);
}
for(i = 0; i < generic_vector_get_size(hVectorOfWordLength); i++){
generic_vector_push_back(hNewWordFamily, (MY_STRING)*generic_vector_at(hVectorOfWordLength, i));
}
for(i = 0; i < word_length; i++)
my_string_push_back(hCurrentKey, '-');
for (i = guess_amount; i > 0; i--) {
// b) print guesses remaining, guessed letters, current blanked-out word
printf("Remaining guesses: %d\n", i);
printf("Guessed letters: ");
if (my_string_get_size(guessedLetters) == 0)
printf("None yet!\n");
else {
my_string_insertion(guessedLetters, stdout);
printf("\n");
}
printf("Current word:%s", my_string_c_str(hCurrentKey));
if (ShowTotalRemaining) { // use search to print every key and the size of
// the vector
generic_vector_get_size(hVectorOfWordLength);
}
printf("\n");
// c)prompt user to guess letter
//
currentGuess = getCurrentGuess(guessedLetters);
my_string_push_back(guessedLetters, currentGuess);
my_string_push_back(guessedLetters, '|');
hNewWordFamily =
CheckIfGuessInWord(&guessedLetters, hNewWordFamily, hCurrentKey,
currentGuess, ShowTotalRemaining);
if (my_string_compare((MY_STRING)*generic_vector_at(hNewWordFamily, 0),
hCurrentKey) == 0) {
printf("You Won!\n Here's the Word:");
printf("%s\n",
my_string_c_str((MY_STRING)*generic_vector_at(hNewWordFamily, 0)));
TheyWon = TRUE;
break;
}
}
if (!TheyWon) {
printf("You lost! Not everyone can be a winner, specially you ;D\n");
printf("The word was %s!\n",
my_string_c_str((MY_STRING)*generic_vector_at(hNewWordFamily, 0)));
TheyWon = FALSE;
}
/* Clean up */
generic_vector_destroy(&hNewWordFamily);// takes care of max
my_string_destroy(&guessedLetters);
my_string_destroy(&hMyString);
my_string_destroy(&hCurrentKey);
return TheyWon;
}
GENERIC_VECTOR CheckIfGuessInWord(MY_STRING* phGuessList, GENERIC_VECTOR hVectorOfWordList, MY_STRING hCurrentKey, char currentGuess, Boolean showTotalRemaining){
int i, guessListSize = 0;
ASSOCIATIVE_ARR hArr = NULL;
MY_STRING hCurrentWord = NULL, hNewKey = NULL, *pTemp = NULL, hMyString = NULL, hGuessList = *phGuessList;
GENERIC_VECTOR hMaxVector = NULL;
hNewKey = my_string_init_default();
if (hNewKey == NULL) {
printf("Failed to get item at my_string_init_default for hMyString.\n");
exit(1);
}
hArr = associray_init_default();
if (hArr == NULL) {
printf("Failed to allocate space for associative array.\n");
exit(1);
}
/* // adds all the words of same length under the same blank key */
//Partitions all the current words
for (i = 0; i < generic_vector_get_size(hVectorOfWordList); i++) {
pTemp = (MY_STRING*) generic_vector_at(hVectorOfWordList, i);
if (pTemp == NULL) {
printf(
"Failed at CheckIfGuessInWord while printing all partions of the current words came back null\n");
exit(1);
}
hCurrentWord = *pTemp;
get_word_key_value(hCurrentKey, hNewKey,(MY_STRING) *generic_vector_at(hVectorOfWordList, i), currentGuess);
associray_insert(hArr, (MY_STRING) *generic_vector_at(hVectorOfWordList, i), hNewKey);
while(!my_string_empty(hNewKey)){// clear the string
my_string_pop_back(hNewKey);
}
}
// Choose max size
hMaxVector = associray_init_find_max(hArr, showTotalRemaining);
if(hMaxVector == NULL){
printf("Failed to find_max\n");
exit(1);
}
generic_vector_destroy(&hVectorOfWordList);
hVectorOfWordList = hMaxVector;
while(!my_string_empty(hNewKey)){// clear the string
my_string_pop_back(hNewKey);
}
get_word_key_value(hCurrentKey, hNewKey, (MY_STRING)*generic_vector_at(hVectorOfWordList, 0), currentGuess);
if(my_string_compare(hCurrentKey, hNewKey) == 0){
printf("\n\nIncorrect Guess. The word doesn't have any character %c in it \n", currentGuess);
}else
my_string_assignment(hCurrentKey, hNewKey);
associray_destroy(&hArr);
my_string_destroy(&hNewKey);
return hVectorOfWordList;
}
char getCurrentGuess(MY_STRING guessList) { // change comparison to a my_string object
int i;
int noc = 0;
Boolean HasBeenUsed = FALSE;
char singleGuess;
do{
noc = 9;
printf("Make a guess using a letter you havent used before:");
noc = scanf(" %c", &singleGuess);
ClearKeyboardBuffer();
if (noc == 1 && isalpha(singleGuess)) {
HasBeenUsed = HasItBeenUsed(guessList, singleGuess);
} else {
printf("You have guessed this letter already or invalid character, Try again!\n");
}
} while ((noc != 1 || !isalpha(singleGuess)) || HasBeenUsed == TRUE);
return singleGuess;
}
int getWordLength(void){
int wordLen = -1;
do{
printf("What word length would you like to play with? ");
scanf("%d", &wordLen);
ClearKeyboardBuffer();
}while((wordLen < 2 || wordLen > 29) || wordLen == 23 || wordLen == 25 || wordLen == 26 || wordLen == 27);
return wordLen;
}
int getGuessAmount(void){
int amount = -1;
do{
printf("How many guesses you need? ");
scanf("%d", &amount);
ClearKeyboardBuffer();
}while(amount < 0);
return amount;
}
Boolean getCheatsChoice(void){
int choice = -1;
do{
printf("Do you want to be a cheater and see remaining choices?\n Choose between yes (1) or no (0): ");
scanf("%d", &choice);
ClearKeyboardBuffer();
}while(choice != 0 && choice != 1);
return choice;
}
Boolean getReplay(void){
int choice = -1;
do{
printf("Wanna play again?\n Choose between yes (1) or no (0): ");
scanf("%d", &choice);
ClearKeyboardBuffer();
}while(choice != 0 && choice != 1);
return choice;
}
void printHangman(void){
printf("\n");
printf(" .----------------. .----------------. .-----------------. .----------------. .----------------. .----------------. .-----------------.\n");
printf("| .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. |\n");
printf("| | ____ ____ | || | __ | || | ____ _____ | || | ______ | || | ____ ____ | || | __ | || | ____ _____ | |\n");
printf("| | |_ || _| | || | / \\ | || ||_ \\|_ _| | || | .' ___ | | || ||_ \\ / _|| || | / \\ | || ||_ \\|_ _| | |\n");
printf("| | | |__| | | || | / /\\ \\ | || | | \\ | | | || | / .' \\_| | || | | \\/ | | || | / /\\ \\ | || | | \\ | | | |\n");
printf("| | | __ | | || | / ____ \\ | || | | |\\ \\| | | || | | | ____ | || | | |\\ /| | | || | / ____ \\ | || | | |\\ \\| | | |\n");
printf("| | _| | | |_ | || | _/ / \\ \\_ | || | _| |_\\ |_ | || | \\ `.___] _| | || | _| |_\\/_| |_ | || | _/ / \\ \\_ | || | _| |_\\ |_ | |\n");
printf("| | |____||____| | || ||____| |____|| || ||_____|\\____| | || | `._____.' | || ||_____||_____|| || ||____| |____|| || ||_____|\\____| | |\n");
printf("| | | || | | || | | || | | || | | || | | || | | |\n");
printf("| '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' |\n");
printf(" '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' \n");
printf("\n");
}
void ClearKeyboardBuffer(void){
char c = ' ';
char noc= 10;
noc = scanf("%c", &c);
while(noc !=1 && c != '\n'){
noc = scanf("%c", &c);
}
}