forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangman-game.c
More file actions
132 lines (110 loc) · 3.17 KB
/
Hangman-game.c
File metadata and controls
132 lines (110 loc) · 3.17 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
// C program to implement hangman game
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_WORD_LENGTH 50
#define MAX_TRIES 6
// Struct to hold a word and its hint
struct WordWithHint {
char word[MAX_WORD_LENGTH];
char hint[MAX_WORD_LENGTH];
};
// Function to display the current state of the word
void displayWord(const char word[], const bool guessed[]);
// Function to draw the hangman
void drawHangman(int tries);
// driver code
int main()
{
// Seed the random number generator with the current
// time
srand(time(NULL));
// Array of words with hints
struct WordWithHint wordList[] = {
{ "geeksforgeeks", "Computer coding" },
{ "elephant", "A large mammal with a trunk" },
{ "pizza", "A popular Italian dish" },
{ "beach", "Sandy shore by the sea" },
// Add more words and hints here
};
// Select a random word from the list
int wordIndex = rand() % 4;
const char* secretWord = wordList[wordIndex].word;
const char* hint = wordList[wordIndex].hint;
int wordLength = strlen(secretWord);
char guessedWord[MAX_WORD_LENGTH] = { 0 };
bool guessedLetters[26] = { false };
printf("Welcome to Hangman!\n");
printf("Hint: %s\n", hint);
int tries = 0;
while (tries < MAX_TRIES) {
printf("\n");
displayWord(guessedWord, guessedLetters);
drawHangman(tries);
char guess;
printf("Enter a letter: ");
scanf(" %c", &guess);
guess = tolower(guess);
if (guessedLetters[guess - 'a']) {
printf("You've already guessed that letter. "
"Try again.\n");
continue;
}
guessedLetters[guess - 'a'] = true;
bool found = false;
for (int i = 0; i < wordLength; i++) {
if (secretWord[i] == guess) {
found = true;
guessedWord[i] = guess;
}
}
if (found) {
printf("Good guess!\n");
}
else {
printf("Sorry, the letter '%c' is not in the "
"word.\n",
guess);
tries++;
}
if (strcmp(secretWord, guessedWord) == 0) {
printf("\nCongratulations! You've guessed the "
"word: %s\n",
secretWord);
break;
}
}
if (tries >= MAX_TRIES) {
printf("\nSorry, you've run out of tries. The word "
"was: %s\n",
secretWord);
}
return 0;
}
void displayWord(const char word[], const bool guessed[])
{
printf("Word: ");
for (int i = 0; word[i] != '\0'; i++) {
if (guessed[word[i] - 'a']) {
printf("%c ", word[i]);
}
else {
printf("_ ");
}
}
printf("\n");
}
void drawHangman(int tries)
{
const char* hangmanParts[]
= { " _________", " | |",
" | O", " | /|\\",
" | / \\", " |" };
printf("\n");
for (int i = 0; i <= tries; i++) {
printf("%s\n", hangmanParts[i]);
}
}