-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordGuess.c
83 lines (76 loc) · 1.83 KB
/
WordGuess.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_GUESSES 20
#define PHRASE_LENGTH 17
#define SPACE_COUNT 2
const char* secretPhrase = "today hello world";
bool guessStatus[PHRASE_LENGTH];
int guessAttempt = 0;
int charactersUnveiled = 0;
char lastGuess = '\0';
bool displayMessage() {
bool cont = true;
//printf("\nCharacters unveiled %d\n", charactersUnveiled);
if(guessAttempt > MAX_GUESSES) {
printf("\nSorry you have exhausted all attempts! The correct phrase is: ");
for(int i = 0 ; i < PHRASE_LENGTH ; i++) {
if(!guessStatus[i]) {
guessStatus[i] = true;
}
}
cont = false;
} else if((charactersUnveiled + SPACE_COUNT) < PHRASE_LENGTH) {
printf("\nPhrase is: ");
} else if((charactersUnveiled + SPACE_COUNT) == PHRASE_LENGTH) {
printf("\nCongratulations! In %d attempts you have guessed correctly the phrase: ", guessAttempt);
cont = false;
}
return cont;
}
bool displayGuess() {
bool cont = displayMessage();
for (int i = 0; i < PHRASE_LENGTH ; i++) {
if (secretPhrase[i] == 32) {
printf(" ");
} else if (guessStatus[i]) {
printf("%c", secretPhrase[i]);
} else {
printf("-");
}
}
printf("\n");
return cont;
}
void makeGuess(char c) {
for (int i = 0; i < PHRASE_LENGTH ; i++) {
if(secretPhrase[i] == 32) {
guessStatus[i] = true;
} else if (c == secretPhrase[i] && !guessStatus[i]) {
guessStatus[i] = true;
charactersUnveiled++;
}
}
lastGuess = c;
}
char attemptGuess() {
displayGuess(lastGuess);
printf("\nGuess a character: ");
char g = '\0';
scanf(" %c", &g);
printf("\n");
guessAttempt++;
return g;
}
/*int main(void) {
while (guessAttempt <= MAX_GUESSES) {
char c = attemptGuess();
makeGuess(c);
if(!displayGuess()) {
break;
}
}
return 0;
}
*/