-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterface.cpp
More file actions
107 lines (84 loc) · 2.72 KB
/
Interface.cpp
File metadata and controls
107 lines (84 loc) · 2.72 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
#include "Interface.h"
#define DISPLAY_WIDTH 24
// TODO: See if terminal can handle colors.
// TODO: See if terminal can handle CHANGING colors.
// TODO: Change colors to match web version.
Interface::Interface( Grid *grid ) {
playGrid = grid;
initscr(); // Start ncurses mode
noecho(); // Silence user input
curs_set(0); // Hide the cursor
keypad( stdscr, TRUE ); // Converts arrow key input to usable chars.
}
void Interface::printEndMessage( bool gameOver ) {
int currentLine = START_LINE + 3;
std::string lines[] = {
" You win! ",
" ",
" Press r for new game ",
" or q to quit. "
};
if ( gameOver ) {
lines[0] = " Game over! ";
}
boardStartX = getmaxx(stdscr)/2 - DISPLAY_WIDTH/2;
move( currentLine++, boardStartX );
addch(ACS_ULCORNER);
for (int i = 0; i < DISPLAY_WIDTH-2; i++) {
addch(ACS_HLINE);
}
addch(ACS_URCORNER);
for (int i = 0; i < 4; i++) {
move( currentLine, boardStartX );
addch(ACS_VLINE);
mvprintw( currentLine++, boardStartX + 1, lines[i].c_str() );
addch(ACS_VLINE);
}
move( currentLine, boardStartX );
addch(ACS_LLCORNER);
for (int i = 0; i < DISPLAY_WIDTH-2; i++) {
addch(ACS_HLINE);
}
addch(ACS_LRCORNER);
refresh();
}
void Interface::printBoard( int highScore ) {
clear();
boardStartX = getmaxx(stdscr)/2 - DISPLAY_WIDTH/2;
mvprintw( 0, boardStartX + 10, "Score: %6i", playGrid->getScore() );
mvprintw( 1, boardStartX + 1, "2048 Best: %6i", highScore );
for (int col = 0; col < 4; col++) {
for (int row = 0; row < 4; row++) {
Tile currentTile = playGrid->tileAt(col, row);
drawTile( ¤tTile );
}
}
refresh();
}
void Interface::drawTile( Tile *tile ) {
int squareStartY = START_LINE + tile->y*3;
int squareStartX = boardStartX + tile->x*6;
move(squareStartY, squareStartX);
addch(ACS_ULCORNER);
addch(ACS_HLINE);
addch(ACS_HLINE);
addch(ACS_HLINE);
addch(ACS_HLINE);
addch(ACS_URCORNER);
mvaddch(squareStartY +1, squareStartX, ACS_VLINE);
mvprintw(squareStartY +1, squareStartX +1, "%s", tile->toString().c_str());
mvaddch(squareStartY +1, squareStartX +5, ACS_VLINE);
move(squareStartY +2, squareStartX);
addch(ACS_LLCORNER);
addch(ACS_HLINE);
addch(ACS_HLINE);
addch(ACS_HLINE);
addch(ACS_HLINE);
addch(ACS_LRCORNER);
}
void Interface::close() {
endwin(); // End ncurses mode
}
bool Interface::inputIsDirectional( int uInput ) {
return ( uInput == KEY_RIGHT || uInput == KEY_UP || uInput == KEY_LEFT || uInput == KEY_DOWN );
}