forked from ChicoState/Wordler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
61 lines (51 loc) · 1.58 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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "dictionary.h"
// function prototypes:
std::string get_hint(std::string,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{
do{
std::cin >> guess;
if(guess == "quit"){
std::cout << "Quitting the game" << std::endl;
return 1;
}
}while( guess.length() != 5 );
// capitalize guess for easy comparisons
for(int i=0; i<guess.length(); i++){
guess[i] = toupper(guess[i]);
}
guesses++;
hint = get_hint(guess,secret);
if( hint == secret ){
std::cout << "Congrats, you got it in " << guesses << " guesses!\n";
}
else{
std::cout << hint << " Guess again: ";
}
}while( hint != 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;
}