-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
78 lines (60 loc) · 1.3 KB
/
main.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
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
char getLetter();
void checkLetter(char letter, const char *word, char *revealedWord);
void start(char *word);
bool isFinished(char *revealWord);
void main()
{
char word[100];
start(word);
int length = strlen(word);
char revealedWord[length + 1];
char guessedLetters[26];
for (int i = 0; i < length; i++)
{
revealedWord[i] = '_';
}
revealedWord[length] = '\0';
printf("%s\n", revealedWord);
while(!isFinished) {
char letter = getLetter();
checkLetter(letter, word, revealedWord);
printf("%s\n", revealedWord);
}
}
void start(char *word) {
printf("Welcome to HANGMAN\n");
printf("Please enter a word\n");
scanf("%99s", word);
printf("\n\n");
printf("Great, let's play hangman!\n");
}
char getLetter()
{
char letter;
printf("Please enter a letter.\n");
scanf(" %c", &letter);
return letter;
}
void checkLetter(char letter, const char *word, char *revealedWord)
{
int length = strlen(word);
for (int i = 0; i < length; i++)
{
if (word[i] == letter)
{
revealedWord[i] = letter;
}
}
}
bool isFinished(char *revealedWord) {
int length = strlen(revealedWord);
for (int i = 0; i < length; i++)
{
if (revealedWord[i] == '_')
return false;
}
return true;
}