-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.cpp
158 lines (135 loc) · 3.31 KB
/
display.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "display.h"
#include <cctype>
#include <array>
std::string SPACE = " ";
void print_incorrect(char letter)
{
std::cout << "\033[38;5;240m" << (char)toupper(letter) << "\033[0m";
std::cout << SPACE;
}
void print_yellow(char letter)
{
std::cout << "\033[38;5;220m" << (char)toupper(letter) << "\033[0m";
std::cout << SPACE;
}
void print_green(char letter)
{
std::cout << "\033[38;5;40m" << (char)toupper(letter) << "\033[0m";
std::cout << SPACE;
}
void print_empty(char letter)
{
std::cout << "\033[38;5;15m" << (char)toupper(letter) << "\033[0m";
std::cout << SPACE;
}
void Display::print_keyboard() const
{
for (const auto &i : _keyboard)
{
format_word(i);
if (i == 'p')
{
std::cout << "\n ";
}
else if (i == 'l')
{
std::cout << "\n ";
}
}
std::cout << "\n"
<< std::endl;
}
Display::Display()
{
for (const auto &l : _keyboard)
{
_game_state[l] = WordScore::Score::Empty;
}
this->refresh_display();
}
void Display::refresh_display() const
{
std::cout << "\033[0m\033[2J" << (SPACE + SPACE) << "\033[38;5;10mAdversarial Wordle\033[0m\n";
std::cout << "Type \"help\" for rules on how to play!\n\n";
for (size_t i = 0; i < _guesses.size(); ++i)
{
std::cout << "> ";
this->format_word(_guesses[i], _guesses_score[i]);
std::cout << "\n";
}
std::cout << std::endl;
print_keyboard();
}
void Display::format_word(const std::string &word, const WordScore &score) const
{
for (size_t j = 0; j < word.size(); j++)
{
switch (score.get_score(j))
{
case WordScore::Score::Yellow:
print_yellow(word[j]);
break;
case WordScore::Score::Green:
print_green(word[j]);
break;
case WordScore::Score::Incorrect:
print_incorrect(word[j]);
break;
case WordScore::Score::Empty:
print_empty(word[j]);
break;
default:
print_empty(word[j]);
break;
}
}
std::cout << "\n";
}
void Display::format_word(const char &letter) const
{
auto ptr = _game_state.find(letter);
switch (ptr->second)
{
case WordScore::Score::Yellow:
print_yellow(letter);
break;
case WordScore::Score::Green:
print_green(letter);
break;
case WordScore::Score::Incorrect:
print_incorrect(letter);
break;
case WordScore::Score::Empty:
print_empty(letter);
break;
default:
print_empty(letter);
break;
}
}
void Display::update_display() const
{
this->refresh_display();
}
void Display::update_display(std::string err_msg) const
{
this->refresh_display();
std::cout << err_msg << "\n";
}
void Display::add_guess(const std::string user_guess, const WordScore score)
{
_guesses.emplace_back(user_guess);
for (size_t i = 0; i < user_guess.size(); ++i)
{
_game_state[user_guess[i]] = score.get_score(i);
}
_guesses_score.emplace_back(score);
}
std::string Display::get_user_input()
{
std::cout << "Guess: > \033[38;5;45m";
std::string user_input;
std::getline(std::cin, user_input);
std::cout << "\033[0m";
return user_input;
}