Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
hangman.c File Reference

C implementation of Hangman Game More...

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
Include dependency graph for hangman.c:

Data Structures

struct  game_instance
 for main() - tolower() for main(), new_word(), new_guess(), won() - I/O operations for all functions - exit(), rand() and file functions for main() - for string operations strlen, strchr, strcpy for new_game() - used with srand() for declaring new game instance More...
 

Functions

struct game_instance new_game ()
 creates a new game - generates a random word and stores in global variable current_word
 
int new_guess (char new_guess, const char guesses[], int size)
 checks if letter has been guessed before
 
int in_word (char letter, const char word[], unsigned int size)
 checks if letter is in current word
 
void picture (int score)
 
void won (const char word[], int score)
 checks if player has won or lost
 
int main ()
 Main Function.
 

Detailed Description

C implementation of Hangman Game

Simple, readable version of hangman. Changed graphic to duck instead of traditional stick figure (same number of guesses).

Author
AtlantaEmrys2002

Function Documentation

◆ in_word()

int in_word ( char  letter,
const char  word[],
unsigned int  size 
)

checks if letter is in current word

Parameters
letterletter guessed by player
wordcurrent word
sizelength of word
Returns
1 if letter is in word
-1 if letter is not in word
119 {
120
121 for (int i = 0; i < size; i++) {
122 if ((word[i]) == letter) {
123 return 1;
124 }
125 }
126
127 return -1;
128}

◆ main()

int main ( void  )

Main Function.

Returns
0 on exit
41 {
42
43 struct game_instance game = new_game(); // new game created
44 char guess; // current letter guessed by player
45
46 // main loop - asks player for guesses
47 while ((strchr(game.hidden, '_') != NULL) && game.incorrect <= 12) {
48 do {
49 printf("\n****************************\n");
50 printf("Your word: ");
51
52 for (int i = 0; i < game.size; i++) {
53 printf("%c ", game.hidden[i]);
54 }
55
56 if (game.guesses_size > 0) {
57 printf("\nSo far, you have guessed: ");
58 for (int i = 0; i < game.guesses_size; i++) {
59 printf("%c ", game.guesses[i]);
60 }
61 }
62
63 printf("\nYou have %d guesses left.", (12 - game.incorrect));
64 printf("\nPlease enter a letter: ");
65 scanf(" %c", &guess);
66 guess = tolower(guess);
67
68 } while (new_guess(guess, game.guesses, game.guesses_size) != -1);
69
70 game.guesses[game.guesses_size] = guess; // adds new letter to guesses array
71 game.guesses_size++; // updates size of guesses array
72
73 if (in_word(guess, game.current_word, game.size) == 1) {
74 printf("That letter is in the word!");
75 for (int i = 0; i < game.size; i++) {
76 if ((game.current_word[i]) == guess) {
77 game.hidden[i] = guess;
78 }
79 }
80 } else {
81 printf("That letter is not in the word.\n");
82 (game.incorrect)++;
83 }
84 picture(game.incorrect);
85 }
86
87 won(game.current_word, game.incorrect);
88 return 0;
89}
void won(const char word[], int score)
checks if player has won or lost
Definition: hangman.c:192
int in_word(char, const char word[], unsigned int size)
checks if letter is in current word
Definition: hangman.c:119
int new_guess(char, const char guesses[], int size)
checks if letter has been guessed before
Definition: hangman.c:99
struct game_instance new_game(void)
creates a new game - generates a random word and stores in global variable current_word
Definition: hangman.c:134
for main() - tolower() for main(), new_word(), new_guess(), won() - I/O operations for all functions ...
Definition: hangman.c:19
char guesses[25]
previous guesses
Definition: hangman.c:25
int incorrect
number of incorrect guesses
Definition: hangman.c:24
char hidden[30]
hidden version of word that is displayed to player
Definition: hangman.c:22
char current_word[30]
word to be guessed by player
Definition: hangman.c:21
int size
size of word
Definition: hangman.c:23
int guesses_size
size of guesses array
Definition: hangman.c:26
Here is the call graph for this function:

◆ new_game()

struct game_instance new_game ( void  )

creates a new game - generates a random word and stores in global variable current_word

Returns
current_game - a new game instance containing randomly selected word, its length and hidden version of word
134 {
135
136 char word[30]; // used throughout function
137
138 FILE *fptr;
139 fptr = fopen("games/words.txt", "r");
140
141 if (fptr == NULL){
142 fprintf(stderr, "File not found.\n");
143 exit(EXIT_FAILURE);
144 }
145
146 // counts number of words in file - assumes each word on new line
147 int line_number = 0;
148 while (fgets(word, 30, fptr) != NULL) {
149 line_number++;
150 }
151
152 rewind(fptr);
153
154 // generates random number
155 int random_num;
156 srand(time(NULL));
157 random_num = rand() % line_number;
158
159 // selects randomly generated word
160 int s = 0;
161 while (s <= random_num){
162 fgets(word, 30, fptr);
163 s++;
164 }
165
166 // formats string correctly
167 if (strchr(word, '\n') != NULL){
168 word[strlen(word) - 1] = '\0';
169 }
170
171 fclose(fptr);
172
173 // creates new game instance
174 struct game_instance current_game;
175 strcpy(current_game.current_word, word);
176 current_game.size = strlen(word);
177 for (int i = 0; i < (strlen(word)); i++) {
178 current_game.hidden[i] = '_';
179 }
180 current_game.incorrect = 0;
181 current_game.guesses_size = 0;
182
183 return current_game;
184}

◆ new_guess()

int new_guess ( char  new_guess,
const char  guesses[],
int  size 
)

checks if letter has been guessed before

Parameters
new_guessletter that has been guessed by player
guessesarray of player's previous guesses
sizesize of guesses[] array
Returns
1 if letter has been guessed before
-1 if letter has not been guessed before
99 {
100
101 for (int j = 0; j < size; j++) {
102 if (guesses[j] == new_guess) {
103 printf("\nYou have already guessed that letter.");
104 return 1;
105 }
106 }
107
108 return -1;
109}
Here is the call graph for this function:

◆ picture()

void picture ( int  score)
206 {
207
208 switch(score) {
209
210 case 12:
211 printf("\n _\n"
212 " __( ' )> \n"
213 " \\_ < _ ) ");
214 break;
215
216 case 11:
217 printf("\n _\n"
218 " __( ' )\n"
219 " \\_ < _ ) ");
220 break;
221
222 case 10:
223 printf("\n _\n"
224 " __( )\n"
225 " \\_ < _ ) ");
226 break;
227
228 case 9:
229 printf("\n \n"
230 " __( )\n"
231 " \\_ < _ ) ");
232 break;
233
234 case 8:
235 printf("\n \n"
236 " __( \n"
237 " \\_ < _ ) ");
238 break;
239
240 case 7:
241 printf("\n \n"
242 " __ \n"
243 " \\_ < _ ) ");
244 break;
245
246 case 6:
247 printf("\n \n"
248 " _ \n"
249 " \\_ < _ ) ");
250 break;
251
252 case 5:
253 printf("\n \n"
254 " _ \n"
255 " _ < _ ) ");
256 break;
257
258 case 4:
259 printf("\n \n"
260 " \n"
261 " _ < _ ) ");
262 break;
263
264 case 3:
265 printf("\n \n"
266 " \n"
267 " < _ ) ");
268 break;
269
270 case 2:
271 printf("\n \n"
272 " \n"
273 " _ ) ");
274 break;
275
276 case 1:
277 printf("\n \n"
278 " \n"
279 " ) ");
280 break;
281
282 case 0:
283 break;
284
285 default:
286 printf("\n _\n"
287 " __( ' )> QUACK!\n"
288 " \\_ < _ ) ");
289 break;
290 }
291}

◆ won()

void won ( const char  word[],
int  score 
)

checks if player has won or lost

Parameters
wordthe word player has attempted to guess
scorehow many incorrect guesses player has made
Returns
void
192 {
193 if (score > 12) {
194 printf("\nYou lost! The word was: %s.\n", word);
195 }
196 else {
197 printf("\nYou won! You had %d guesses left.\n", (12 - score));
198 }
199}