-
Notifications
You must be signed in to change notification settings - Fork 110
/
main.cpp
65 lines (54 loc) · 1.68 KB
/
main.cpp
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "dictionary.h"
// function prototypes:
std::string get_hint(std::string,std::string);
void capitalize(std::string &);
// Wordler game!
int main(){
srand(time(NULL)); //execute only once per run
dictionary word_list;
std::string guess;
std::string hint;
std::string secret;
int guesses = 0;
secret = word_list.select_word();
// REVEAL ANSWER: std::cout << secret << std::endl;
std::cout << "Welcome to Wordler -- a game that totally isn't simplified Wordle\n";
std::cout << "Guess your five-letter word:\n_____\n";
do{
// require user to enter another guess if their word isn't 5 letters long
do{
std::cin >> guess;
}while( guess.length() != 5 );
// capitalize guess for easy comparisons
capitalize(guess);
guesses++;
hint = get_hint(guess,secret);
capitalize(hint);
if( hint == secret ){
std::cout << "Congrats, you got it in " << guesses << " guesses!\n";
}
else{
std::cout << hint << " Guess again: ";
}
}while( guess != secret );
return 0;
}
// compares a guess and a secret word and reveals matching letters, but all
// non-matching letters become underscores ('_') and the hint is returned
std::string get_hint(std::string match, std::string word){
for(int i=0; i<word.length(); i++){
if( word[i] != match[i] ){
word[i] = '_';
}
}
return word;
}
// capitalizes a word (to UPPER CASE)
void capitalize(std::string & word){
for(int i=0; i<word.length(); i++){
word[i] = toupper(word[i]);
}
}