-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordle.cpp
More file actions
103 lines (80 loc) · 1.83 KB
/
wordle.cpp
File metadata and controls
103 lines (80 loc) · 1.83 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
#include <iostream>
#include <ctime>
#include <cctype>
using namespace std;
string pickWord(string word[25]);
string getInput();
int checkGuess(string answer, string guess, int hint[5]);
void showHints(string guess, int hint[5]);
int main() {
string word[25] = {
"about", "after", "could", "early", "every",
"first", "found", "great", "group", "house",
"large", "later", "never", "often", "other",
"small", "still", "there", "think", "those",
"under", "until", "where", "while", "world"
};
string guess, answer;
int hint[5];
int correctGuess;
answer = pickWord(word);
while(true) {
guess = getInput();
correctGuess = checkGuess(answer, guess, hint);
if(correctGuess == 5) {
cout << " You got the word!";
break;
}
showHints(guess, hint);
}
return 0;
}
string pickWord(string word[25]) {
int choice;
srand(time(NULL));
choice = 1 + rand() % 25;
return word[choice];
}
string getInput() {
string input;
cout << " Guess: ";
cin >> input;
return input;
}
int checkGuess(string answer, string guess, int hint[5]) {
int correctGuess = 0;
for(int i = 0; i < guess.length(); i++) {
guess[i] = tolower(guess[i]);
if(answer[i] == guess[i]) {
hint[i] = 2;
correctGuess++;
}
else if(answer.find(guess[i]) != string::npos) {
hint[i] = 1;
answer[answer.find(guess[i])] = ' ';
}
else
hint[i] = 0;
}
return correctGuess;
}
void showHints(string guess, int hint[5]) {
int correctGuess = 0;
int correctLetter = 0;
cout << " ";
for(int i = 0; i < 5; i++) {
if(hint[i] == 2) {
cout << "[" << guess[i] << "] ";
correctGuess++;
}
else if(hint[i] == 1) {
cout << "(" << guess[i] << ") ";
correctLetter++;
}
else
cout << guess[i] << " ";
}
cout << "\n";
cout << " Correct guess: " << correctGuess << ", ";
cout << "correct letter: " << correctLetter << "\n\n";
}