-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub_task_4_Hang_Man.c
235 lines (211 loc) · 4.76 KB
/
sub_task_4_Hang_Man.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
// Group Members
// David Henry - 1007604
// Rockell London - 1037481
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <conio.h>
// prototypes
int word_selector(); //random number to select a random word
int print_hangman(int); // prints hangman image
int word_print(int, int); //prints word with blanks and hangman image
// variables
char words[][10] = { "dog", "cow", "computer", "onion", "house",
"apple", "pizza", "soda", "chicken", "potato" };
//to hold the letter guessed
char letter[10];
int i, word_index, random_number;
int correct = 0, guessed = 0;
int word_hidden[10]; //[0,0,0,0,0,0,0,0,0,0,0]
int guess_check;
int hangman = 0;
int playing = 1;
int chances = 5;
// main
int main()
{
// clear screen at start
system("cls");
// value is generated to choose the word
word_index = word_selector();
int word_length = strlen(words[word_index]); //length of the word
// intro screen with word list
print_hangman(10);
printf("\n Press any key to begin...");
getch();
system("cls");
// print the initial screen with the blanks
word_print(hangman, word_length);
// game loop
while (playing)
{
// check to see if guesses are used up or player won
if (correct == word_length)
{
printf("\n\n YOU WIN!\n Press any key to exit..\n");
getch();
return 0;
}
if (guessed == 5)
{
printf("\n\n You lose!\n\n The word was %s !\n Press any key to exit...", words[word_index]);
getch();
return 0;
}
// ask player to guess
printf("\n\n Remaining guesses : %d \n\n %d letter word \n\n Enter a letter:", chances - guessed, word_length);
fgets(letter, 10, stdin);
// current "correct" value count is stored,a change indicates guess was correct
guess_check = correct;
// loop through letters to see if it matches player guess,skip ones that are already guessed
for (i = 0; i < word_length; i++)
{
if (word_hidden[i] == 1)
{
continue;
}
// increase correct count if letter exists in the word
if (letter[0] == words[word_index][i])
{
word_hidden[i] = 1;
correct++;
}
}
// Clear the screen after guessing
system("cls");
// check if guess was incorrect
if (correct > guess_check)
{
printf("\n Correct guess!");
}
else
{
printf("\n Incorrect Guess!");
guessed++;
hangman++;
}
// print new hangman image and correctly guessed letters
word_print(hangman, word_length);
}
return 0;
}
// function that prints the word / blanks and calls the hangman image func
int word_print(int hangman, int word_length)
{
print_hangman(hangman);
for (i = 0; i < word_length; i++)
{
if (word_hidden[i])
{
printf(" %c ", words[word_index][i]);
}
else
{
printf(" _ ");
}
}
}
// function to print hanged man
int print_hangman(int x)
{
if (x == 0)
{
printf("\n");
printf("\n +----+");
printf("\n | |");
printf("\n |");
printf("\n |");
printf("\n |");
printf("\n |");
printf("\n |");
printf("\n +----+\n\n");
}
else if (x == 1)
{
printf("\n");
printf("\n +----+");
printf("\n | |");
printf("\n | 0");
printf("\n |");
printf("\n |");
printf("\n |");
printf("\n |");
printf("\n +----+\n\n");
}
else if (x == 2)
{
printf("\n");
printf("\n +----+");
printf("\n | |");
printf("\n | 0");
printf("\n | /|");
printf("\n |");
printf("\n |");
printf("\n |");
printf("\n +----+\n\n");
}
else if (x == 3)
{
printf("\n");
printf("\n +----+");
printf("\n | |");
printf("\n | 0");
printf("\n | /|\\");
printf("\n |");
printf("\n |");
printf("\n |");
printf("\n +----+\n\n");
}
else if (x == 4)
{
printf("\n");
printf("\n +----+");
printf("\n | |");
printf("\n | 0");
printf("\n | /|\\");
printf("\n | / ");
printf("\n |");
printf("\n |");
printf("\n +----+\n\n");
}
else if (x == 5)
{
printf("\n");
printf("\n +----+");
printf("\n | |");
printf("\n | 0");
printf("\n | /|\\");
printf("\n | / \\");
printf("\n |");
printf("\n |");
printf("\n +----+\n\n");
}
else if (x == 10)
{
printf("\n ****HANG MAN *****************************************************");
printf("\n +----+");
printf("\n | |");
printf("\n | 0");
printf("\n | /|\\");
printf("\n | / \\");
printf("\n |");
printf("\n |");
printf("\n +----+\n");
printf("\n "
"*******************************************************************"
"*\n");
printf("\n Words: "
"dog,cow,computer,onion,house,apple,pizza,soda,chicken,potato\n");
printf("\n "
"*******************************************************************"
"*\n");
}
}
// generate a number between 0 and 10 - uses as the array index
int word_selector()
{
srand((unsigned) time(NULL));
random_number = rand() % 10;
return random_number;
}