forked from VITAL-Club/hangman-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.c
226 lines (198 loc) · 5.36 KB
/
hangman.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
// Here's my iteration of the Hangman code. My approach to this is to
// count the amount of correct letters contained in the word.
// If you feel this code needs to be corrected, don't hesitate
// to do so.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#define HANGED 6
#ifdef WIN32
#define CLS system("cls")
#else
#define CLS system("clear")
#endif
void displayString(char* current, int stringLength)
{
int i;
for (i = 0; i < stringLength; i++)
{
// If there is no element, make the thing blank
if (current[i] == 0)
{
printf("_");
}
//Otherwise, place the letter into the string
else
{
printf("%c", current[i]);
}
// Include a space between each letter
printf(" ");
}
// Have a new line for the string
printf("\n");
}
// Displaying the hanging body
void displayBody(int correctLetters)
{
printf("\t\t\t\t\t\tBody: ");
switch(correctLetters)
{
case 1:
printf("\t\t O\n");
break;
case 2:
printf("\t\t O\n");
printf("\t\t\t\t\t\t\t\t |\n");
break;
case 3:
printf("\t\t O\n");
printf("\t\t\t\t\t\t\t\t /|\n");
break;
case 4:
printf("\t\t O\n");
printf("\t\t\t\t\t\t\t\t /|\\\n");
break;
case 5:
printf("\t\t O\n");
printf("\t\t\t\t\t\t\t\t /|\\\n");
printf("\t\t\t\t\t\t\t\t /\n");
break;
case 6:
printf("\t\t O\n");
printf("\t\t\t\t\t\t\t\t /|\\\n");
printf("\t\t\t\t\t\t\t\t /\\\n");
break;
default:
return;
}
}
// The searching prototype that Frank made. I made it into an int
// function to count the variable incase it was found once.
int checkChar(const char* answer, char* current, char guess)
{
int found = 0;
int i;
int length = strlen(answer);
for (i = 0; i < length; i++)
{
if (answer[i] == guess)
{
found++;
current[i] = guess;
}
}
return found;
}
// Printing the wrong character array
void wrongLetterDisplay(char *wrong)
{
int i;
int length = strlen(wrong);
for (i = 0; i < length; i++)
{
printf("%c", wrong[i]);
printf(" ");
}
printf("\n");
}
// Inserting the wrong letter into the wrong letter array.
void wrongLetterFunction(char *wrong, char input)
{
int i;
for (i = 0; i < HANGED; i++)
{
if (input == wrong[i])
break;
else
{
wrong[strlen(wrong)] = input;
break;
}
}
}
// Make a boolean function to make sure that the
// letter guess is not contained in the wrong letter array.
bool checkWrongLetter(char *wrong, char input)
{
bool check = false;
int i = 0;
for (i = 0; i < HANGED; i++)
{
if (wrong[i] == input)
{
check = true;
}
}
return check;
}
int main(void)
{
// This is the
char *stringGuess = "guess";
char *stringRight;
char input, letter;
// "Alphabet" is the wrong array.
char *stringWrong;
// i is the for loop, guessLen is for the length of the actual word
// alphabetLetters is the length of the incorrect array
// correctGuess is to equal the guess length of the word.
// correctLetters is the amount of times a correct letter is found,
// which will be added to correctGuess after going through the word.
int i, guessLen, alphabetLetters = 0, correctGuess = 0, correctLetters = 0;
int wrongGuess = 0, lives = 6;
printf("Welcome to Hangman\n");
guessLen = strlen(stringGuess);
// Dynamically Allocate the word to fit the thing itself.
stringRight = malloc(sizeof(char) * (guessLen + 1));
stringWrong = calloc(sizeof(char), HANGED + 1);
while (correctLetters < guessLen)
{
CLS;
// display the wrong letter array after each input
wrongLetterDisplay(stringWrong);
// Display the string
displayString(stringRight, guessLen);
printf("Please input a character: ");
input = tolower(getchar());
// This is to remove the extra newline from scanf
// 10 is a newline.
while (getchar() != 10);
// Increment letter if it a letter is found;
alphabetLetters = checkChar(stringGuess, stringRight, input);
// If there are no letters found from the guessed character,
// we increment the wrongGuess variable.
if (alphabetLetters != 0)
{
correctLetters += alphabetLetters;
}
else
{
printf("Sorry, Wrong Letter. Please try again.\n");
// Checking to see if the input is already in the wrong character array.
// If not, we increment the wrongGuess variable and add the character into the array.
if (!checkWrongLetter(stringWrong, input))
{
wrongGuess++;
wrongLetterFunction(stringWrong, input);
}
displayBody(wrongGuess);
// If the amount of lives and the amount of wrong guesses equal
// zero, we end the loop and immediately give the GAME OVER screen
if (lives - wrongGuess != 0)
printf("Lives left: %d\n", lives - wrongGuess);
else
break;
getchar();
while(getchar() != 10);
}
}
displayString(stringRight, guessLen);
// Free memory from the guessed string
printf("GAME OVER\n");
// Free both the correct / wrong strings.
free(stringRight);
free(stringWrong);
return 0;
}