Repository navigation
Expand file tree
/
Copy pathConsoleRenderer.cpp
More file actions
717 lines (649 loc) · 31.2 KB
/
Copy pathConsoleRenderer.cpp
File metadata and controls
717 lines (649 loc) · 31.2 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
#include <iostream>
#include <iomanip>
#include <typeinfo>
#include <fstream>
#include <sstream>
#include <string>
#include "ConsoleRenderer.h"
#include "Logger.h"
#define SHOW_AI_TILES false /* show AI tiles */
#define DFT "\033[0m" /* reset */
#define RED_BG "\033[41m" /* red background */
#define GREEN_BG "\033[42m" /* green background */
#define YELLOW_BG "\033[43m" /* yellow background */
#define CYAN_BG "\033[46m" /* cyan background */
#define WHITE_BG "\033[47m" /* white background */
#define LIGHT_RED_BG "\033[101m" /* light red background */
#define DARK_GRAY_BG "\033[100m" /* dark gray background */
#define RED "\033[31m" /* red */
#define BLUE "\033[34m" /* blue */
#define BLACK "\033[30m" /* black */
#define WHITE "\033[37m" /* white */
#define CLEAR "\033c"
#define HUMAN_ARROW "\033[31m" /* red */
#define HUMAN_RED "\033[31m" /* red */
#define AI_ARROW "\033[32m" /* green */
#define AI_GREEN "\033[32m" /* green */
#define HUMAN_BG "\033[41m" /* red background */
#define AI_BG "\033[42m" /* green background */
#define HUMAN_CURSOR_SHOW "\033[34m" /* blue */
#define HUMAN_CURSOR_SELECT "\033[31m" /* red */
#define BB_NAME_WIDTH 12
#define BB_MSG_WIDTH 40
#define LAND_WIDTH 5
#define LAND_HEIGHT 2
#define MENU_WIDTH 20
#define SPACE_WIDTH 10
#define LOGO_LOC_X() (3)
#define LOGO_LOC_Y(map_width) (MENU_WIDTH + SPACE_WIDTH + map_width/2 - 38)
#define BULLETIN_BOARD_LOC_X() (11)
#define BULLETIN_BOARD_LOC_Y(map_width) (MENU_WIDTH + SPACE_WIDTH + map_width + SPACE_WIDTH)
#define MAP_LOC_X() (13)
#define MAP_LOC_Y() (MENU_WIDTH + SPACE_WIDTH)
#define MENU_LOC_X() (11)
#define MENU_LOC_Y() (0)
#define WELCOME_LOC_X() (9)
#define WELCOME_LOC_Y(map_width) (MENU_WIDTH + SPACE_WIDTH + map_width/2 - 38)
#define HELP_LOC_X(map_height) (11 + map_height + 3)
#define HELP_LOC_Y() (MENU_WIDTH + SPACE_WIDTH)
#define USER_INPUT_LOC_X(map_height) (11 + map_height + 2)
#define USER_INPUT_LOC_Y() (0)
#define INFO_LOC_X(map_height) (11 + map_height + 2)
#define INFO_LOC_Y() (MENU_WIDTH + SPACE_WIDTH)
#define WIN_MESSAGE_LOC_X() (13)
#define WIN_MESSAGE_LOC_Y(map_width) (MENU_WIDTH + SPACE_WIDTH + map_width/2 - 20)
/// @brief moves the cursor to the assigned position
/// @param row row position for the cursor to move to
/// @param col column position for the cursor to move to
void moveCursor(int row, int col){
std::cout << "\033[" << row << ";" << col << "H";
}
/// @brief return the type of the target tile (capital, city, mountain, quagmire,none)
/// @param tile the target tile to get the symbol
/// @return a character representing the type of the tile (capital, city, mountain, quagmire,none)
char getTileSymbol(const Tile& tile) {
if (tile.isMountain()) return '^';
if (tile.isCapital()) return 'C';
if (tile.isCity()) return 'c';
//if (tile.isOriginalCapital) return '~';//if (tile.isQuagmire) return '~';
return '.';
}
/// @brief assign the rendering task to specific ConsoleRenderer functions
/// @param task the rendering task
void ConsoleRenderer::render(const RenderTask& task) {
if (const auto* t = dynamic_cast<const SelectTile*>(&task)) {
LOG_INFO("renderSelectTile: Received a rendering task.");
renderSelectTile(*t);
} else if (const auto* t = dynamic_cast<const Arrow*>(&task)) {
LOG_INFO("renderArrow: Received a rendering task.");
renderArrow(*t);
} else if (const auto* t = dynamic_cast<const MoveCursor*>(&task)) {
LOG_INFO("renderMoveCursor: Received a rendering task.");
renderMoveCursor(*t);
} else if (const auto* t = dynamic_cast<const RefreshMapInterface*>(&task)) {
LOG_INFO("renderRefreshMapInterface: Received a rendering task.");
renderRefreshMapInterface(*t);
} else if (const auto* t = dynamic_cast<const UpdateTile*>(&task)) {
LOG_INFO("renderUpdateTile: Received a rendering task.");
renderUpdateTile(*t);
} else if (const auto* t = dynamic_cast<const ShowMsg*>(&task)) {
LOG_INFO("renderShowMsg: Received a rendering task.");
renderShowMsg(*t);
} else if (const auto* t = dynamic_cast<const BulletinBoardInterface*>(&task)) {
LOG_INFO("renderBulletinBoardInterface: Received a rendering task.");
renderBulletinBoardInterface(*t);
} else if (const auto* t = dynamic_cast<const InitGameInterface*>(&task)) {
LOG_INFO("renderInitGameInterface: Received a rendering task.");
renderInitGameInterface(*t);
} else if (const auto* t = dynamic_cast<const StartGameInterface*>(&task)) {
LOG_INFO("renderStartGameInterface: Received a rendering task.");
renderStartGameInterface(*t);
} else if (const auto* t = dynamic_cast<const HelpInterface*>(&task)) {
LOG_INFO("renderHelpInterface: Received a rendering task.");
renderHelpInterface(*t);
} /*else if (const auto* t = dynamic_cast<const MenuInterface*>(&task)){
LOG_INFO("renderMenuInterface: Received a rendering task.");
renderMenuInterface(*t);
} */else if (const auto* t = dynamic_cast<const ConfirmExitInterface*>(&task)){
LOG_INFO("renderConfirmExitInterface: Received a rendering task.");
renderConfirmExitInterface(*t);
} else if (const auto* t = dynamic_cast<const ConfirmResetInterface*>(&task)){
LOG_INFO("renderConfirmResetInterface: Received a rendering task.");
renderConfirmResetInterface(*t);
} /*else if (const auto* t = dynamic_cast<const DifficultyInterface*>(&task)) {
renderDifficultyInterface(*t);
}*/ else if (const auto* t = dynamic_cast<const GameClearedInterface*>(&task)){
renderGameClearedInterface(*t);
} else if (const auto* t = dynamic_cast<const EndGameInterface*>(&task)){
LOG_INFO("renderEndGameInterface: Received a rendering task.");
renderEndGameInterface(*t);
} else if (const auto* t = dynamic_cast<const UpdateTurnsInterface*>(&task)){
LOG_INFO("renderUpdateTurnsInterface: Received a rendering task.");
renderUpdateTurnsInterface(*t);
}
// finally, move cursor to user input location
std::cout << DFT;
moveCursor(USER_INPUT_LOC_X(map_height), USER_INPUT_LOC_Y());
std::cout.flush();
}
/// @brief highlight the selected tile into yellow, or turn the tile back to red if it is not selected
/// Given the rules of generals.io, only the player's tiles can be highlighted.
/// @param task the SelectTile rendering task
void ConsoleRenderer::renderSelectTile(const SelectTile& task) {
std::string cursor_color;
bSelected_ = task.select;
if (task.select){cursor_color = HUMAN_CURSOR_SELECT;}
else {cursor_color = HUMAN_CURSOR_SHOW;}
// show new cursor
int row = LAND_HEIGHT * static_cast<int>(user_cursor_y) + MAP_LOC_X();
int col = static_cast<int>(user_cursor_x) * LAND_WIDTH + MAP_LOC_Y();
moveCursor(row-1, col-1); // left top corner
std::cout << cursor_color << "┌" << DFT;
moveCursor(row-1, col+LAND_WIDTH-1); // right top corner
std::cout << cursor_color << "┐" << DFT;
moveCursor(row+1, col-1); // left bottom corner
std::cout << cursor_color << "└" << DFT;
moveCursor(row+1, col+LAND_WIDTH-1); // right bottom corner
std::cout << cursor_color << "┘" << DFT;
}
/// @brief draw an arrow between adjacent tiles, or erase the arrow between adjacent tiles
/// Given the rules of generals.io, only arrows on the player's side is generated.
/// @param task the Arrow rendering task
void ConsoleRenderer::renderArrow(const Arrow& task) {
int x0 = task.from.first;
int y0 = task.from.second;
int x1 = task.to.first;
int y1 = task.to.second;
LOG_INFOF("renderArrow: from (%d,%d) to (%d,%d), draw=%d, isHuman=%d",
x0, y0, x1, y1, task.draw, task.isHuman);
//Tile tile = task.snapshot[y0][x0];
std::string arrow_color;
if (task.isHuman){arrow_color = HUMAN_ARROW;}
else {arrow_color = AI_ARROW;}
if ((x0 == x1) && (y0 - y1 == 1)){
int row = LAND_HEIGHT * y0 + MAP_LOC_X() - 1;
int col = x0 * LAND_WIDTH + MAP_LOC_Y() + 1;
moveCursor(row,col);
if (task.draw){std::cout << arrow_color << "^";}
else {std::cout << " ";}
}
else if ((x0 == x1) && (y0 - y1 == -1)){
int row = LAND_HEIGHT * y0 + MAP_LOC_X() + 1;
int col = x0 * LAND_WIDTH + MAP_LOC_Y() + 1;
moveCursor(row,col);
if (task.draw){std::cout << arrow_color << "v";}
else {std::cout << " ";}
}
else if ((x0 - x1 == 1) && (y0 == y1)){
int row = LAND_HEIGHT * y0 + MAP_LOC_X();
int col = x0 * LAND_WIDTH + MAP_LOC_Y() - 1;
moveCursor(row,col);
if (task.draw){std::cout << arrow_color << "<";}
else {std::cout << " ";}
}
else if ((x0 - x1 == -1) && (y0 == y1)){
int row = LAND_HEIGHT * y0 + MAP_LOC_X();
int col = x0 * LAND_WIDTH + MAP_LOC_Y() + LAND_WIDTH - 1;//3;
moveCursor(row,col);
if (task.draw){std::cout << arrow_color << ">";}
else {std::cout << " ";}
}
// update user cursor position
if (task.isHuman && task.draw) { // only human drawing
updateHumanCursor(task.to, true, task.snapshot);
}
/*std::cout << DFT;
moveCursor(USER_INPUT_LOC_X(map_height), USER_INPUT_LOC_Y());
std::cout.flush();*/
}
/// @brief MoveCursor the cursor to a new position
/// @param task the MoveCursor rendering task
void ConsoleRenderer::renderMoveCursor(const MoveCursor& task) {
LOG_INFOF("renderMoveCursor: to (%d,%d)", task.to.first, task.to.second);
// update user cursor position
updateHumanCursor(task.to, false, task.snapshot);
/*std::cout << DFT;
moveCursor(USER_INPUT_LOC_X(map_height), USER_INPUT_LOC_Y());
std::cout.flush();*/
}
/// @brief update the whole game map
/// @param task the RefreshMap rendering task
void ConsoleRenderer::renderRefreshMapInterface(const RefreshMapInterface& task) {
renderRefreshMap(task.snapshot);
/*std::cout << DFT;
moveCursor(USER_INPUT_LOC_X(map_height), USER_INPUT_LOC_Y());
std::cout.flush();*/
}
void ConsoleRenderer::renderRefreshMap(const std::vector<std::vector<Tile>>& snapshot, bool bInit) {
moveCursor(MAP_LOC_X()-2,MAP_LOC_Y()-2);
// dynamic MAP header: left and right '=' counts symmetric
int left_eq = 0;
int right_eq = 0;
if (map_width > 1) {
left_eq = (map_width-1) / 2; // left side count as requested
right_eq = (map_width-1) - left_eq;
}
std::string header = std::string(left_eq, '=') + " MAP " + std::string(right_eq, '=');
std::cout << header;
for (size_t y = 0; y < snapshot.size(); y++) {
for (size_t x = 0; x < snapshot[y].size(); x++) {
int row = LAND_HEIGHT * static_cast<int>(y) + MAP_LOC_X();
int col = static_cast<int>(x) * LAND_WIDTH + MAP_LOC_Y();
Tile tile = snapshot[y][x];
// Decide whether to render this tile: always show mountains; otherwise show only if owner == 0 (human)
// or adjacent (up/down/left/right) to a human-owned tile
bool show = false;
int h = static_cast<int>(snapshot.size());
int w = h > 0 ? static_cast<int>(snapshot[0].size()) : 0;
if (tile.isMountain()) show = true;
else if (tile.owner == 0) show = true;
else if (tile.owner == 1 && SHOW_AI_TILES) show = true;
else {
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
for (int i = 0; i < 4 && !show; ++i) {
int nx = static_cast<int>(x) + dx[i];
int ny = static_cast<int>(y) + dy[i];
if (nx >= 0 && nx < w && ny >= 0 && ny < h) {
if (snapshot[ny][nx].owner == 0) show = true;
}
}
}
moveCursor(row, col);
if (!show) {
std::cout << WHITE_BG << ' ' << std::string(LAND_WIDTH-2, ' ');
} else {
if (tile.isMountain()) {
// Mountains: dark gray background with white text
std::cout << DARK_GRAY_BG << WHITE << getTileSymbol(tile) << std::string(LAND_WIDTH-2, ' ');
}
else if (bInit) {
std::cout << WHITE_BG << BLACK << std::string(LAND_WIDTH-1, ' ');
}
else if (tile.army){
// format army count: cap display at "99+" when >= 1000, right-align into LAND_WIDTH-2
std::string armyStr = (tile.army >= 1000) ? std::string("99+") : std::to_string(tile.army);
if ((int)armyStr.size() < LAND_WIDTH-2) armyStr = std::string(LAND_WIDTH-2 - armyStr.size(), ' ') + armyStr;
if (tile.owner == 0) { // human
std::cout << HUMAN_BG << BLACK << getTileSymbol(tile) << armyStr;
}
else if (tile.owner == 1){ // ai
std::cout << AI_BG << BLACK << getTileSymbol(tile) << armyStr;
}
else { // neutral
std::cout << WHITE_BG << BLACK << getTileSymbol(tile) << armyStr;
}
} else {
// Visible plain land: light red background; others stay white background
char sym = getTileSymbol(tile);
if (sym == '.') {
std::cout << LIGHT_RED_BG << BLACK << sym << std::string(LAND_WIDTH-2, ' ');
} else {
std::cout << WHITE_BG << BLACK << sym << std::string(LAND_WIDTH-2, ' ');
}
}
}
std::cout << DFT;
}
}
// render human cursor
updateHumanCursor({user_cursor_x, user_cursor_y}, bSelected_, snapshot);
}
/// @brief update a certain file
/// @param task the UpdateTile rendering task
void ConsoleRenderer::renderUpdateTile(const UpdateTile& task) {
int x = task.coords.first;
int y = task.coords.second;
int row = LAND_HEIGHT * y + MAP_LOC_X();
int col = x * LAND_WIDTH + MAP_LOC_Y();
// Decide whether to render this tile: always show mountains; otherwise show only if owner == 0 (human)
// or adjacent (up/down/left/right) to a human-owned tile
bool show = false;
auto& snap = task.snapshot;
int h = static_cast<int>(snap.size());
int w = h > 0 ? static_cast<int>(snap[0].size()) : 0;
if (task.tile.isMountain()) show = true;
else if (task.tile.owner == 0) show = true;
else {
// check neighbors
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
for (int i = 0; i < 4 && !show; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx < w && ny >= 0 && ny < h) {
if (snap[ny][nx].owner == 0) show = true;
}
}
}
moveCursor(row, col);
if (!show) {
// render empty cell (preserve layout)
std::cout << WHITE_BG << ' ' << std::string(LAND_WIDTH-2, ' ');
} else {
if (task.tile.isMountain()){
// Mountains: dark gray background with white text
std::cout << DARK_GRAY_BG << WHITE << getTileSymbol(task.tile) << std::string(LAND_WIDTH-2, ' ');
}
else if (task.tile.army){
// format army count: cap display at "99+" when >= 1000, right-align into LAND_WIDTH-2
std::string armyStr = (task.tile.army >= 1000) ? std::string("99+") : std::to_string(task.tile.army);
if ((int)armyStr.size() < LAND_WIDTH-2) armyStr = std::string(LAND_WIDTH-2 - armyStr.size(), ' ') + armyStr;
if (task.tile.owner == 1) {std::cout << RED_BG << BLACK << getTileSymbol(task.tile) << armyStr;}
else if (task.tile.owner == 0){std::cout << GREEN_BG << BLACK << getTileSymbol(task.tile) << armyStr; }
else {std::cout << WHITE_BG << BLACK << getTileSymbol(task.tile) << armyStr; }
} else {
// Visible plain land: light red background; others stay white background
char sym = getTileSymbol(task.tile);
if (sym == '.') {
std::cout << LIGHT_RED_BG << BLACK << sym << std::string(LAND_WIDTH-2, ' ');
} else {
std::cout << WHITE_BG << BLACK << sym << std::string(LAND_WIDTH-2, ' ');
}
}
}
/*std::cout <<DFT;
moveCursor(USER_INPUT_LOC_X(map_height), USER_INPUT_LOC_Y());
std::cout.flush();*/
}
void ConsoleRenderer::renderGameLogo (int map_width) {
moveCursor(LOGO_LOC_X(),LOGO_LOC_Y(map_width));
std::cout << " ██████╗ ███████╗███╗ ██╗███████╗██████╗ █████╗ ██╗ ███████╗ █████╗ ██╗";
moveCursor(LOGO_LOC_X()+1,LOGO_LOC_Y(map_width));
std::cout << "██╔════╝ ██╔════╝████╗ ██║██╔════╝██╔══██╗██╔══██╗██║ ██╔════╝ ██╔══██╗██║";
moveCursor(LOGO_LOC_X()+2,LOGO_LOC_Y(map_width));
std::cout << "██║ ███╗█████╗ ██╔██╗ ██║█████╗ ██████╔╝███████║██║ ███████╗ ███████║██║";
moveCursor(LOGO_LOC_X()+3,LOGO_LOC_Y(map_width));
std::cout << "██║ ██║██╔══╝ ██║╚██╗██║██╔══╝ ██╔══██╗██╔══██║██║ ╚════██║ ██╔══██║██║";
moveCursor(LOGO_LOC_X()+4,LOGO_LOC_Y(map_width));
std::cout << "╚██████╔╝███████╗██║ ╚████║███████╗██║ ██║██║ ██║███████╗███████║██╗██║ ██║██║";
moveCursor(LOGO_LOC_X()+5,LOGO_LOC_Y(map_width));
std::cout << " ╚═════╝ ╚══════╝╚═╝ ╚═══╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝╚═╝";
std::cout << DFT;
}
/// @brief shows the init game page (first page)
/// @param task the InitGameInterface rendering task
void ConsoleRenderer::renderInitGameInterface (const InitGameInterface& task){
bSelected_ = false;
level_ = task.level;
username_ = task.username;
map_width = static_cast<int>(task.snapshot[0].size()) * LAND_WIDTH;
map_height = static_cast<int>(task.snapshot.size()) * LAND_HEIGHT;
user_cursor_x = task.user_cursor_x;
user_cursor_y = task.user_cursor_y;
std::cout << CLEAR;
renderGameLogo(map_width);
moveCursor(WELCOME_LOC_X(),WELCOME_LOC_Y(map_width));
std::cout << BLUE << " **************Welcome to GENERALS.AI, a human vs AI board game***************** " << DFT;
//std::vector<std::vector<Tile>> snapToRender;
//int tiles_w = task.snapshot[0].size();
//int tiles_h = task.snapshot.size();
//snapToRender.assign(tiles_h, std::vector<Tile>(tiles_w));
renderRefreshMap(task.snapshot, true);
std::string s = "";
renderBulletinBoard(0, 0, 0, 0, 0, s);
renderMenu();
renderHelp();
/*std::cout << DFT;
moveCursor(USER_INPUT_LOC_X(map_height), USER_INPUT_LOC_Y());
std::cout.flush();*/
}
/// @brief shows the start game page (first page)
/// @param task the StartGameInterface rendering task
void ConsoleRenderer::renderStartGameInterface (const StartGameInterface& task){
bSelected_ = false;
level_ = task.level;
username_ = task.username;
map_width = static_cast<int>(task.snapshot[0].size()) * LAND_WIDTH;
map_height = static_cast<int>(task.snapshot.size()) * LAND_HEIGHT;
user_cursor_x = task.user_cursor_x;
user_cursor_y = task.user_cursor_y;
std::cout << CLEAR;
renderGameLogo(map_width);
moveCursor(WELCOME_LOC_X(),WELCOME_LOC_Y(map_width));
std::cout << BLUE << " *********** Welcome to GENERALS.AI, a human vs AI board game ************** " << DFT;
renderRefreshMap(task.snapshot);
std::string s = "";
renderBulletinBoard(0, 0, 0, 0, 0, s);
renderMenu();
renderHelp();
/*std::cout << DFT;
moveCursor(USER_INPUT_LOC_X(map_height), USER_INPUT_LOC_Y());
std::cout.flush();*/
}
/// @brief update the human cursor on the map
/// @param snapshot the current game map snapshot
void ConsoleRenderer::updateHumanCursor (std::pair<int, int> to, bool bSelected, const std::vector<std::vector<Tile>>& snapshot){
Tile tile = snapshot[user_cursor_y][user_cursor_x];
int x1 = static_cast<int>(to.first);
int y1 = static_cast<int>(to.second);
LOG_DEBUGF("user_cursor_x-%d,user_cursor_y-%d, tile.owner-%d, army-%d, landType-%d, to.x-%d, to.y-%d",
user_cursor_x, user_cursor_y, tile.owner, tile.army, static_cast<int>(tile.landType), x1, y1);
// erase cur cursor
int row = LAND_HEIGHT * static_cast<int>(user_cursor_y) + MAP_LOC_X();
int col = static_cast<int>(user_cursor_x) * LAND_WIDTH + MAP_LOC_Y();
moveCursor(row-1, col-1); // left top corner
std::cout << " " ;
moveCursor(row-1, col+LAND_WIDTH-1); // right top corner
std::cout << " " ;
moveCursor(row+1, col-1); // left bottom corner
std::cout << " " ;
moveCursor(row+1, col+LAND_WIDTH-1); // right bottom corner
std::cout << " " ;
// update user cursor
user_cursor_x = x1;
user_cursor_y = y1;
// show new cursor
std::string cursor_color;
if (bSelected){cursor_color = HUMAN_CURSOR_SELECT;}
else {cursor_color = HUMAN_CURSOR_SHOW;}
row = LAND_HEIGHT * static_cast<int>(user_cursor_y) + MAP_LOC_X();
col = static_cast<int>(user_cursor_x) * LAND_WIDTH + MAP_LOC_Y();
moveCursor(row-1, col-1); // left top corner
std::cout << cursor_color << "┌" << DFT;
moveCursor(row-1, col+LAND_WIDTH-1); // right top corner
std::cout << cursor_color << "┐" << DFT;
moveCursor(row+1, col-1); // left bottom corner
std::cout << cursor_color << "└" << DFT;
moveCursor(row+1, col+LAND_WIDTH-1); // right bottom corner
std::cout << cursor_color << "┘" << DFT;
std::cout << DFT;
}
/// @brief shows the help page (first page)
/// @param task the HelpInterface rendering task
void ConsoleRenderer::renderHelpInterface (const HelpInterface& task){
(void)task;
//std::cout << CLEAR;
//renderGameLogo ();
renderHelp ();
/*std::cout << DFT;
moveCursor(USER_INPUT_LOC_X(map_height), USER_INPUT_LOC_Y());
std::cout.flush();*/
}
void ConsoleRenderer::renderHelp (){
moveCursor(HELP_LOC_X(map_height),HELP_LOC_Y());
std::cout << /*WHITE_BG <<*/ DFT << "A/a - Left, D/d - Right, W/w - Up, S/s - Down, Space - Select/Unselect Tile";
//moveCursor(HELP_LOC_X(map_height)+1,HELP_LOC_Y());
//std::cout << /*WHITE_BG <<*/ DFT << "Space - Toggle Army Movement: On / Off";
}
/// @brief shows the Bulletin Board page
/// @param task the BulletinBoardInterface rendering task
void ConsoleRenderer::renderBulletinBoardInterface (const BulletinBoardInterface& task){
//std::cout << CLEAR;
//renderGameLogo();
renderBulletinBoard(task.turns, task.user_land, task.user_army, task.ai_land, task.ai_army, task.msg);
/*std::cout << DFT;
moveCursor(USER_INPUT_LOC_X(map_height), USER_INPUT_LOC_Y());
std::cout.flush();*/
}
void ConsoleRenderer::renderBulletinBoard (int turns,
int user_land, int user_army,
int ai_land, int ai_army,
const std::string& msg) {
moveCursor(BULLETIN_BOARD_LOC_X(), BULLETIN_BOARD_LOC_Y(map_width));
std::cout << "======== BULLETIN BOARD ========";
moveCursor(BULLETIN_BOARD_LOC_X()+1, BULLETIN_BOARD_LOC_Y(map_width));
std::cout << DFT << "TURNS: " << RED << turns << DFT;
moveCursor(BULLETIN_BOARD_LOC_X()+2, BULLETIN_BOARD_LOC_Y(map_width));
std::cout << DFT << "LEVEL: " << RED << level_ << DFT;
moveCursor(BULLETIN_BOARD_LOC_X()+4, BULLETIN_BOARD_LOC_Y(map_width));
std::cout <<"PLAYER" << std::setw(12) << "LAND" << std::setw(12) << "ARMY" << DFT;
// print player name padded/truncated to BB_NAME_WIDTH
{
moveCursor(BULLETIN_BOARD_LOC_X()+5, BULLETIN_BOARD_LOC_Y(map_width));
std::string out = username_.substr(0, BB_NAME_WIDTH);
if ((int)out.size() < BB_NAME_WIDTH) out += std::string(BB_NAME_WIDTH - out.size(), ' ');
std::cout << HUMAN_RED << out;
}
moveCursor(BULLETIN_BOARD_LOC_X()+5, BULLETIN_BOARD_LOC_Y(map_width)+14);
std::cout << HUMAN_RED << user_land;
moveCursor(BULLETIN_BOARD_LOC_X()+5, BULLETIN_BOARD_LOC_Y(map_width)+26);
std::cout << HUMAN_RED << user_army << DFT;
moveCursor(BULLETIN_BOARD_LOC_X()+6, BULLETIN_BOARD_LOC_Y(map_width));
std::cout << AI_GREEN << "AI";
moveCursor(BULLETIN_BOARD_LOC_X()+6, BULLETIN_BOARD_LOC_Y(map_width)+14);
std::cout << AI_GREEN << ai_land;
moveCursor(BULLETIN_BOARD_LOC_X()+6, BULLETIN_BOARD_LOC_Y(map_width)+26);
std::cout << AI_GREEN << ai_army << DFT;
if(msg!=""){
// print a fixed-width message area: label on the left, message content padded/truncated
moveCursor(BULLETIN_BOARD_LOC_X()+8, BULLETIN_BOARD_LOC_Y(map_width));
std::cout << "Messages: " << DFT;
moveCursor(BULLETIN_BOARD_LOC_X()+9, BULLETIN_BOARD_LOC_Y(map_width));
std::string out = msg.substr(0, BB_MSG_WIDTH);
if ((int)out.size() < BB_MSG_WIDTH) out += std::string(BB_MSG_WIDTH - out.size(), ' ');
std::cout << out << DFT;
}
}
/// @brief shows the message of the game
/// @param task the ShowMsg rendering task
void ConsoleRenderer::renderShowMsg(const ShowMsg& task) {
moveCursor(BULLETIN_BOARD_LOC_X()+8, BULLETIN_BOARD_LOC_Y(map_width));
std::cout << "Messages: " << DFT;
{
moveCursor(BULLETIN_BOARD_LOC_X()+9, BULLETIN_BOARD_LOC_Y(map_width));
std::string out = task.msg.substr(0, BB_MSG_WIDTH);
if ((int)out.size() < BB_MSG_WIDTH) out += std::string(BB_MSG_WIDTH - out.size(), ' ');
std::cout << out << DFT;
}
/*std::cout << DFT;
moveCursor(USER_INPUT_LOC_X(map_height), USER_INPUT_LOC_Y());
std::cout.flush();*/
}
/*
/// @brief shows the Menu page
/// @param task the MenuInterface task
void ConsoleRenderer::renderMenuInterface(const MenuInterface& task){
(void)task;
std::string name;
std::cout << CLEAR;
renderGameLogo ();
renderMenu();
std::cout << DFT;
moveCursor(USER_INPUT_LOC_X(map_height), USER_INPUT_LOC_Y());
std::cout.flush();
}*/
void ConsoleRenderer::renderMenu(){
moveCursor(MENU_LOC_X(),MENU_LOC_Y());
std::cout << "======= MENU =======";
moveCursor(MENU_LOC_X()+1,MENU_LOC_Y());
std::cout << " G/g: Start";
moveCursor(MENU_LOC_X()+2,MENU_LOC_Y());
std::cout << " R/r: Restart";
moveCursor(MENU_LOC_X()+3,MENU_LOC_Y());
std::cout << " P/p: Pause/Resume";
moveCursor(MENU_LOC_X()+4,MENU_LOC_Y());
std::cout << " Q/q: Quit";
moveCursor(MENU_LOC_X()+5,MENU_LOC_Y());
std::cout << "======= Legends =======";
moveCursor(MENU_LOC_X()+6,MENU_LOC_Y());
std::cout << " Red : Your Army";
moveCursor(MENU_LOC_X()+7,MENU_LOC_Y());
std::cout << " Green : AI's Army";
moveCursor(MENU_LOC_X()+8,MENU_LOC_Y());
std::cout << " ^ : Mountains";
moveCursor(MENU_LOC_X()+9,MENU_LOC_Y());
std::cout << " C : Capital";
moveCursor(MENU_LOC_X()+10,MENU_LOC_Y());
std::cout << " c : City";
moveCursor(MENU_LOC_X()+11,MENU_LOC_Y());
std::cout << " . : Plain Lands Visible" << DFT;
}
/// @brief shows the ConfirmExit page
/// @param task the ConfirmExitInterface rendering task
void ConsoleRenderer::renderConfirmExitInterface (const ConfirmExitInterface& task){
(void)task;
moveCursor(INFO_LOC_X(map_height), INFO_LOC_Y());
std::cout << WHITE_BG << RED << " Are you sure you want to quit? (Y/N) " << DFT;
/*std::cout << DFT;
moveCursor(USER_INPUT_LOC_X(map_height), USER_INPUT_LOC_Y());
std::cout.flush();*/
}
/// @brief shows the ConfirmReset page
/// @param task the ConfirmResetInterface rendering task
void ConsoleRenderer::renderConfirmResetInterface (const ConfirmResetInterface& task){
(void)task;
moveCursor(INFO_LOC_X(map_height), INFO_LOC_Y());
std::cout << WHITE_BG << RED << " Are you sure you want to restart? (Y/N) " << DFT;
/*std::cout << DFT;
moveCursor(USER_INPUT_LOC_X(map_height), USER_INPUT_LOC_Y());
std::cout.flush();*/
}
/// @brief shows the GameCleared page
/// @param task the GameClearedInterface rendering task
void ConsoleRenderer::renderGameClearedInterface (const GameClearedInterface& task){
(void)task;
moveCursor(WIN_MESSAGE_LOC_X(), WIN_MESSAGE_LOC_Y(map_width));
std::cout << YELLOW_BG << " ----------------------------------------- ";
moveCursor(WIN_MESSAGE_LOC_X()+1, WIN_MESSAGE_LOC_Y(map_width));
std::cout << " | Good job! | ";
moveCursor(WIN_MESSAGE_LOC_X()+2, WIN_MESSAGE_LOC_Y(map_width));
std::cout << " |Congratulations on completing the game!| ";
moveCursor(WIN_MESSAGE_LOC_X()+3, WIN_MESSAGE_LOC_Y(map_width));
std::cout << YELLOW_BG << " ----------------------------------------- ";
moveCursor(WIN_MESSAGE_LOC_X()+4, WIN_MESSAGE_LOC_Y(map_width));
std::cout << " |Press Q/q to exit! | ";
moveCursor(WIN_MESSAGE_LOC_X()+5, WIN_MESSAGE_LOC_Y(map_width));
std::cout << " ----------------------------------------- ";
}
/// @brief shows the ending message when a round of game ends
/// @param task the EndGameInterface rendering task
void ConsoleRenderer::renderEndGameInterface (const EndGameInterface& task){
moveCursor(WIN_MESSAGE_LOC_X(), WIN_MESSAGE_LOC_Y(map_width));
std::cout << YELLOW_BG << " ---------------------------------------- ";
moveCursor(WIN_MESSAGE_LOC_X()+1, WIN_MESSAGE_LOC_Y(map_width));
std::cout << " | Game End! | ";
moveCursor(WIN_MESSAGE_LOC_X()+2, WIN_MESSAGE_LOC_Y(map_width));
if (task.isWinning){
std::cout << " | You win! | ";
} else{
std::cout << " | You lose... | ";
}
moveCursor(WIN_MESSAGE_LOC_X()+3, WIN_MESSAGE_LOC_Y(map_width));
std::cout << YELLOW_BG << " ---------------------------------------- ";
moveCursor(WIN_MESSAGE_LOC_X()+4, WIN_MESSAGE_LOC_Y(map_width));
std::cout << " |Round count:" << std::setw(5) << task.roundCount << " | ";
moveCursor(WIN_MESSAGE_LOC_X()+5, WIN_MESSAGE_LOC_Y(map_width));
std::cout << " |Press Q/q to exit! | ";
moveCursor(WIN_MESSAGE_LOC_X()+6, WIN_MESSAGE_LOC_Y(map_width));
if (task.isWinning){
std::cout << " |Press R/r to start next level! | ";
} else {
std::cout << " |Press R/r to retry this level! | ";
}
//std::cout << " |Press R/r to start next level! | ";
moveCursor(WIN_MESSAGE_LOC_X()+7, WIN_MESSAGE_LOC_Y(map_width));
std::cout << " ---------------------------------------- ";
/*std::cout << DFT;
moveCursor(USER_INPUT_LOC_X(map_height), USER_INPUT_LOC_Y());
std::cout.flush();*/
}
/// @brief shows the UpdateTurns page
/// @param task the UpdateTurnsInterface rendering task
void ConsoleRenderer::renderUpdateTurnsInterface(const UpdateTurnsInterface& task){
renderRefreshMap(task.snapshot);
renderBulletinBoard(task.turns, task.user_land, task.user_army, task.ai_land, task.ai_army, task.msg);
/*std::cout << DFT;
moveCursor(USER_INPUT_LOC_X(map_height), USER_INPUT_LOC_Y());
std::cout.flush();*/
}