-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbraincurses.cpp
333 lines (279 loc) · 8.58 KB
/
braincurses.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* braincurses.cpp
* Copyright © 2002–2015, Brian Derr <brian@derrclan.com>
*/
#include "braincurses.h"
#include <cstdlib>
#include "code.h"
// Maps the x-coordinates (within a WINDOW) for different values of code_length_.
const std::unordered_map<int, std::vector<int>> codePosition = {
{4, {2, 6, 10, 14}},
{5, {2, 5, 8, 11, 14}},
{6, {3, 5, 7, 9, 11, 13}}
};
Braincurses::Braincurses(int code_length, int guesses) :
code_(Code(code_length)), guesses_(guesses), initialized_(false) {
InitializeNcurses();
}
void Braincurses::InitializeNcurses() {
initscr();
if (has_colors() == FALSE) {
// TODO: throw an exception?
}
start_color();
if (COLORS < 8) {
// TODO: throw an exception?
}
use_default_colors();
// Only using the first eight color pairs.
for (int i = 0; i < 8; i++) {
if (i == 6) {
// COLOR_CYAN is hard to distinguish from COLOR_BLUE; replace it with COLOR_WHITE.
init_pair(i, i+1, -1);
} else {
init_pair(i, i, -1);
}
}
cbreak();
noecho();
intrflush(stdscr, FALSE);
curs_set(1);
windows_.emplace(kHeaderWindow, CreateWindow(3, 21, 0, 0));
windows_.emplace(kMarkerWindow, CreateWindow(17, 17, 3, 0));
windows_.emplace(kGuessWindow, CreateWindow(17, 17, 3, 21));
windows_.emplace(kCodeWindow, CreateWindow(3, 17, 0, 21));
windows_.emplace(kWatermarkWindow, CreateWindow(17, 4, 3, 17));
windows_.emplace(kInputWindow, CreateWindow(3, 60, 20, 0));
windows_.emplace(kInfoWindow, CreateWindow(20, 22, 0, 38));
initialized_ = true;
}
void Braincurses::PrepareBoard() {
mvwaddstr(windows_[kHeaderWindow], 1, 5, kGameName.c_str());
mvwaddstr(windows_[kInfoWindow], 1, 2, "Colors: ");
wattron(windows_[kInfoWindow], A_BOLD);
wattron(windows_[kInfoWindow], COLOR_PAIR(COLOR_RED));
mvwaddstr(windows_[kInfoWindow], 2, 2, "RED");
wattroff(windows_[kInfoWindow], COLOR_PAIR(COLOR_RED));
waddstr(windows_[kInfoWindow], ", ");
wattron(windows_[kInfoWindow], COLOR_PAIR(COLOR_BLUE));
waddstr(windows_[kInfoWindow], "BLUE");
wattroff(windows_[kInfoWindow], COLOR_PAIR(COLOR_BLUE));
waddstr(windows_[kInfoWindow], ", ");
mvwaddstr(windows_[kInfoWindow], 3, 2, "WHITE, ");
wattron(windows_[kInfoWindow], COLOR_PAIR(COLOR_GREEN));
waddstr(windows_[kInfoWindow], "GREEN");
wattroff(windows_[kInfoWindow], COLOR_PAIR(COLOR_GREEN));
waddstr(windows_[kInfoWindow], ", ");
wattron(windows_[kInfoWindow], COLOR_PAIR(COLOR_YELLOW));
mvwaddstr(windows_[kInfoWindow], 4, 2, "YELLOW");
wattroff(windows_[kInfoWindow], COLOR_PAIR(COLOR_YELLOW));
waddstr(windows_[kInfoWindow], ", ");
wattron(windows_[kInfoWindow], COLOR_PAIR(COLOR_MAGENTA));
waddstr(windows_[kInfoWindow], "MAGENTA");
wattroff(windows_[kInfoWindow], COLOR_PAIR(COLOR_MAGENTA));
wattroff(windows_[kInfoWindow], A_BOLD);
#ifdef DEBUG
DisplayCode(true);
#else
DisplayCode(false);
#endif
char guessLabel[3];
for (int i = 1; i <= guesses_; i++) {
snprintf(guessLabel, 3, "%2d", i);
mvwaddstr(windows_[kWatermarkWindow], 16 - i, 1, guessLabel);
}
wmove(windows_[kInputWindow], 1, 15);
wnoutrefresh(windows_[kHeaderWindow]);
wnoutrefresh(windows_[kCodeWindow]);
wnoutrefresh(windows_[kInfoWindow]);
wnoutrefresh(windows_[kWatermarkWindow]);
doupdate();
}
WINDOW* Braincurses::CreateWindow(int height, int width, int starty, int startx) {
WINDOW* window;
window = newwin(height, width, starty, startx);
box(window, 0, 0);
wrefresh(window);
return window;
}
void Braincurses::CleanUpWindow(WINDOW* window) {
werase(window);
box(window, 0, 0);
wrefresh(window);
}
void Braincurses::WipeBoard() {
for (auto kv : windows_) {
CleanUpWindow(kv.second);
}
}
std::vector<int> Braincurses::GetInput() {
std::string delStr (kInputLength, ' ');
std::vector<int> guessInput (code_.Length(), -1);
int column[6] = {2, 11, 20, 29, 38, 47};
int input;
auto local_window = windows_[kInputWindow];
keypad(local_window, TRUE);
int x = 0;
while (true) {
input = mvwgetch(local_window, 1, column[x]);
switch (input) {
case 'r':
mvwaddstr(local_window, 1, column[x], delStr.c_str());
mvwaddstr(local_window, 1, column[x], "red");
guessInput[x] = COLOR_RED;
x++;
break;
case 'g':
mvwaddstr(local_window, 1, column[x], delStr.c_str());
mvwaddstr(local_window, 1, column[x], "green");
guessInput[x] = COLOR_GREEN;
x++;
break;
case 'y':
mvwaddstr(local_window, 1, column[x], delStr.c_str());
mvwaddstr(local_window, 1, column[x], "yellow");
guessInput[x] = COLOR_YELLOW;
x++;
break;
case 'b':
mvwaddstr(local_window, 1, column[x], delStr.c_str());
mvwaddstr(local_window, 1, column[x], "blue");
guessInput[x] = COLOR_BLUE;
x++;
break;
case 'm':
mvwaddstr(local_window, 1, column[x], delStr.c_str());
mvwaddstr(local_window, 1, column[x], "magenta");
guessInput[x] = COLOR_MAGENTA;
x++;
break;
case 'w':
mvwaddstr(local_window, 1, column[x], delStr.c_str());
mvwaddstr(local_window, 1, column[x], "white");
guessInput[x] = COLOR_CYAN; // Really COLOR_WHITE
x++;
break;
case KEY_LEFT:
if (x > 0) {
x--;
wmove(local_window, 1, column[x]);
}
break;
case KEY_RIGHT:
if (x < code_.Length()) {
wmove(local_window, 1, column[x]);
x++;
}
break;
case KEY_BACKSPACE:
case KEY_DC:
if (x > 0 && x <= code_.Length()) {
x--;
mvwaddstr(local_window, 1, column[x], delStr.c_str());
guessInput[x] = -1;
}
break;
default:
break;
}
wrefresh(local_window);
if (x == code_.Length()) {
break;
}
}
CleanUpWindow(local_window);
return guessInput;
}
void Braincurses::DisplayGuess(int y, std::vector<int> guess) {
auto local_window = windows_[kGuessWindow];
y = 15 - y;
wattron(local_window, A_BOLD);
int i = 0;
for (auto x : codePosition.at(guess.size())) {
mvwaddch(local_window, y, x, 'X' | COLOR_PAIR(guess[i]));
i++;
}
wattroff(local_window, A_BOLD);
wrefresh(local_window);
}
void Braincurses::DisplayMarkers(int y, std::vector<int> correct) {
auto local_window = windows_[kMarkerWindow];
y = 15 - y;
wattron(local_window, A_BOLD);
std::vector<int> x_positions = codePosition.at(code_.Length());
for (int i = 0; i < code_.Length(); i++) {
int marker = correct[i];
int x = x_positions[i];
if (marker == 2) { // NAILED_IT
mvwaddch(local_window, y, x, 'X' | COLOR_PAIR(COLOR_RED));
continue;
} else if (marker == 1) { // ALMOST
mvwaddch(local_window, y, x, 'X' | COLOR_PAIR(COLOR_WHITE));
continue;
}
}
wattroff(local_window, A_BOLD);
wrefresh(local_window);
}
bool Braincurses::IsWinner(std::vector<int> correct) {
bool winner = true;
for (unsigned i = 0; i < correct.size(); i++) {
if (correct[i] != 2) {
winner = false;
break;
}
}
return winner;
}
bool Braincurses::GameOverPlayAgain(bool winner) {
CleanUpWindow(windows_[kInputWindow]);
if (winner) {
mvwaddstr(windows_[kInputWindow], 1, 1, "You win! Congratulations!");
} else {
mvwaddstr(windows_[kInputWindow], 1, 1, "You ran out of turns; better luck next time.");
}
wrefresh(windows_[kInputWindow]);
return PlayAgain();
}
void Braincurses::DisplayCode(bool colored) {
auto local_window = windows_[kCodeWindow];
int i = 0;
for (auto x : codePosition.at(code_.Length())) {
#ifdef DEBUG
mvwaddch(local_window, 1, x, 'X' | COLOR_PAIR(code_.Get()[i]) | A_BOLD);
#else
if (colored) {
mvwaddch(local_window, 1, x, 'X' | COLOR_PAIR(code_.Get()[i]) | A_BOLD);
} else {
mvwaddch(local_window, 1, x, 'X');
}
#endif
i++;
}
wrefresh(local_window);
}
bool Braincurses::PlayAgain() {
CleanUpWindow(windows_[kInputWindow]);
mvwaddstr(windows_[kInputWindow], 1, 1, "Would you like to play again? ([Y]/n) ");
wrefresh(windows_[kInputWindow]);
int again = tolower(wgetch(windows_[kInputWindow]));
return (again == 'y' || again == '\n' ? true : false);
}
bool Braincurses::PlayGame() {
code_.Create();
WipeBoard();
PrepareBoard();
bool winner = false;
std::vector<int> correct;
for (int i = 0; i < guesses_; i++) {
std::vector<int> guess = GetInput();
correct = code_.IsCorrect(guess.begin(), guess.end());
DisplayGuess(i, guess);
DisplayMarkers(i, correct);
if (IsWinner(correct)) {
winner = true;
break;
}
}
DisplayCode(true);
return winner;
}