-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangman.cpp
More file actions
236 lines (166 loc) · 4.64 KB
/
Hangman.cpp
File metadata and controls
236 lines (166 loc) · 4.64 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
struct wordData{
char word[11];
char clue[20];
};
void printLivesAndHangman(int nlives){
printf("Lives: ");
for(int i = 1; i <= nlives; i++) printf("\3");
if(nlives == 0) printf("You lose! :(");
printf("\n\n");
printf("+----+\n");
printf("|");
if(nlives <= 6) printf(" |");
printf("\n");
printf("|");
if(nlives <= 5) printf(" O");
printf("\n");
printf("|");
if(nlives <= 4) printf(" /");
if(nlives <= 3) printf("|");
if(nlives <= 2) printf("\\");
printf("\n");
printf("|");
if(nlives <= 1) printf(" /");
if(nlives == 0) printf(" \\");
printf("\n");
printf("|");
printf("\n\n");
}
int main(){
struct wordData data[101];
int idx = 0;
FILE *fptr;
fptr = fopen("wordlist.csv", "r");
fscanf(fptr, "Word,Clue\n");
while(!feof(fptr)){
fscanf(fptr, "%[^,],%[^\n]\n", &data[idx].word, &data[idx].clue);
idx++;
}
int nidx = idx - 1;
printf("Welcome to Hangman!!!");
sleep(3);
system("cls");
bool didntInputANumber = 1;
printf("How many times do you want to play? (Input a number)\n");
int nplay;
scanf("%d", &nplay);
getchar();
system("cls");
printf("Have fun!!!");
sleep(3);
system("cls");
srand(time(0));
int wordNumber[101];
for(int i = 0; i < nplay; i++) wordNumber[i] = rand() % (nidx + 1);
for(int i = 0; i < nplay; i++){
didntInputANumber = 0;
printf("GAME %d\n", i + 1);
bool finished = 0;
char guessedLetters[27] = "";
int len = strlen(data[wordNumber[i]].word);
char answer[11] = "A";
for(int j = 0; j < len; j++) answer[j] = '_';
int nattempt = 0;
int nlives = 7;
while(nlives > 0){
printLivesAndHangman(nlives);
printf("Clue: %s (%d letters)\n\n", data[wordNumber[i]].clue, len);
char letter[11];
for(int j = 0; j < len; j++) printf("%c ", answer[j]);
printf("\n\n");
if(strcmp(answer, data[wordNumber[i]].word) == 0) finished = 1;
int lenGuessedLetters = strlen(guessedLetters);
printf("Attempted letters:\n");
for(int j = 0; j < lenGuessedLetters; j++) printf("%c", guessedLetters[j]);
printf("\n\n");
if(finished){
printf("\n\nYOU WIN!!!");
sleep(3);
system("cls");
break;
}
if(!finished){
printf("Input the letter you wanna try:\n");
scanf("%s", &letter);
getchar();
printf("\n");
}
if(strlen(letter) > 1){
printf("Please input one letter!");
sleep(3);
system("cls");
printf("GAME %d\n", i + 1);
continue;
}
if((letter[0] < 'A' || letter[0] > 'Z') && (letter[0] < 'a' || letter[0] > 'z')){
printf("Please input a letter!");
sleep(3);
system("cls");
printf("GAME %d\n", i + 1);
continue;
}
if(letter[0] >= 'a' || letter[0] <= 'z') letter[0] = letter[0] - 32;
bool guessedSameLetter = 0;
if(lenGuessedLetters == 0) strcat(guessedLetters, letter);
else{
for(int j = 0; j < lenGuessedLetters; j++){
if(letter[0] == guessedLetters[j]){
printf("You've tried this letter!");
sleep(3);
system("cls");
guessedSameLetter = 1;
printf("GAME %d\n", i + 1);
break;
}
}
}
if(guessedSameLetter) continue;
else{
guessedLetters[nattempt] = letter[0];
nattempt++;
}
bool letterInWord = 0;
for(int j = 0; j < len; j++){
if(answer[j] == '_'){
if(data[wordNumber[i]].word[j] == letter[0]){
answer[j] = letter[0];
letterInWord = 1;
}
}
}
if(letterInWord) printf("The letter is in the word!");
else{
printf("The letter is not in the word!");
nlives--;
}
sleep(3);
system("cls");
printf("GAME %d\n", i + 1);
}
if(nlives == 0){
printLivesAndHangman(nlives);
printf("Clue: %s (%d letters)\n\n", data[wordNumber[i]].clue, len);
for(int j = 0; j < len; j++) printf("%c ", answer[j]);
printf("\n\n");
int lenGuessedLetters = strlen(guessedLetters);
printf("Attempted letters:\n");
for(int j = 0; j < lenGuessedLetters; j++) printf("%c", guessedLetters[j]);
printf("\n\n");
printf("Correct word:\n");
printf("%s\n", data[wordNumber[i]].word);
printf("\nYou lose!!!");
sleep(3);
system("cls");
continue;
}
}
if(didntInputANumber) printf("Please input a number!");
else printf("Thank you for playing!!!");
fclose(fptr);
return 0;
}