-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.c
232 lines (196 loc) · 5.02 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
227
228
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define DASH '_'
typedef struct Game
{
int gameOver; /* If the whole word has been guessed */
int numRight; /* Number of letters correctly guessed */
char randomWord[80]; /* holds the word */
int totalGuesses; /* hold the total number of guesses made correct & incorrect */
char lettersGuessed[80];
}Game;
int selectWord(char letters []);
int compareChar(char input, char * originalWord, char guesses []);
int gameOver(int numRight, int wordLength, char letters []);
void printWelcomeMessages(int wordLength);
void printChar(char guesses [], int length);
void newGame(Game newGame, char letters []);
void printLettersGuessed( Game * game, int totalGuesses );
int contains( char * word, char * character );
int main(int argc, char * argv[])
{
Game game = {FALSE, 0};
char guesses[80];
int wordLength;
int dashes;
int incorrectGuesses;
int maxTries;
while(game.gameOver == FALSE)
{
char * word;
char * ptr;
wordLength = 0;
dashes = 0;
incorrectGuesses = 0;
maxTries = 15;
wordLength = selectWord(game.randomWord); /* Selecting random word */
word = game.randomWord; /* Assigning word to the randomWord */
ptr = strchr(word,'\n'); /* Getting rid of the new line */
*ptr = '\0';
for (dashes = 0; dashes < wordLength; dashes ++) /* For loop to print out the dashes */
{
guesses[dashes] = DASH;
}
printWelcomeMessages( wordLength );
char input;
while(game.gameOver == FALSE)
{
printf("Number of guesses: %i\n",incorrectGuesses);
printf("Number of tries left: %i\n",(maxTries - incorrectGuesses));
printLettersGuessed( &game, game.totalGuesses );
printf( "Guess a letter: ");
scanf( " %c", &input );
if (input == '\n' || input == ' ')
{
input = getchar();
}
game.lettersGuessed[game.totalGuesses] = input;
game.totalGuesses++;
if (compareChar(input,word,guesses))
{
printf("\n\nYou guessed '%c' correctly\n\n",input);
}
else
{
printf("\n\nSorry, '%c' is not a letter in the word\n\n",input);
incorrectGuesses++;
}
printChar(guesses,wordLength);
if (!gameOver(game.numRight,wordLength,guesses))
{
system("clear");
printf("Congratulations! You have correctly guessed '%s'!\n\n",guesses);
break;
}
if (incorrectGuesses == maxTries)
{
system("clear");
printf("GAME OVER!\nYou have failed to guess '%s'\n\n",word);
break;
}
}
newGame(game,guesses);
}
return 0;
}
void printLettersGuessed( Game * game, int totalGuesses )
{
if( totalGuesses == 0 ) { return; }
printf("You have guessed the following letters: " );
char * word = game -> randomWord;
for( int i = 0; i < totalGuesses; i++ )
{
char * character = &(game -> lettersGuessed[i]);
if( contains( word, character ) )
{
printf( "\x1b[32m %s \x1b[0m", character ); //print in green -- is correct
}
else
{
printf( "\x1b[31m %s \x1b[0m", character ); //print in green -- not correct
}
}
printf( "\n" );
}
void newGame(Game newGame, char letters [])
{
memset(&newGame,0,sizeof(Game));
memset(letters,' ',80);
newGame.numRight = 0;
newGame.gameOver = FALSE;
newGame.totalGuesses = 0;
printf("Starting a new game...\n");
}
int gameOver(int numRight, int wordLength, char letters [])
{
int index;
numRight = 0;
for (index = 0; index < wordLength; index++)
{
if (letters[index] != DASH)
numRight++;
}
return (wordLength - numRight);
}
int contains( char * word, char * character )
{
char * result = (char * ) strstr( word, character );
if( result == NULL )
{
return 0;
}
return 1;
}
void printChar(char guesses [], int length)
{
int i = 0;
for ( i = 0; i < length; printf("%c ",guesses[i]), i++);
puts("\n");
}
int compareChar(char input, char * originalWord, char guesses [])
{
char * tempPtr = originalWord;
int index = 0;
int guess = FALSE;
while (*tempPtr != '\n')
{
if (input == *tempPtr)
{
guess = TRUE;
guesses[index] = *tempPtr;
}
tempPtr++;
index++;
}
return guess;
}
void printWelcomeMessages(int wordLength)
{
int dashes;
printf("WELCOME TO HANGMAN\n");
printf("\nSelecting a random word.");
printf("\nYour word is %d characters long...Good Luck!", wordLength);
puts("\n");
for (dashes = 0; dashes < wordLength; dashes++)
{
printf("_ ");
}
puts("\n");
}
int selectWord( char letters[])
{
FILE * fp = fopen("./words", "r" );
char c;
int index = 0;
int lineCount = 235886; //number of words in local dictionary
srand(time(NULL));
int randomLine = rand() % lineCount; //random line
while( index < randomLine )
{
index++;
(void)fgets( letters, 80, fp );
}
index = 0;
do {
c = fgetc(fp);
letters[index] = tolower(c);
index++;
} while (c != '\n');
fclose(fp);
return (index - 1);
}