-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_of_life_tui.cpp
215 lines (181 loc) · 6.21 KB
/
game_of_life_tui.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
#include "game_of_life_tui.h"
#define UP 'w'
#define DOWN 'r'
#define LEFT 'a'
#define RIGHT 's'
#define TOGGLE_CELL ' '
#define STEP_GAME 'f'
#define TITLE_WINDOW_HEIGHT 1
int main() {
initscr();
noecho();
cbreak();
raw();
clear();
WINDOW *titleWin = NULL, *mainWin = NULL, *borderWin = NULL;
// title window
titleWin = newwin(TITLE_WINDOW_HEIGHT, COLS, 0, 0);
// centered text
wmove(titleWin, 0, (COLS/2) - 6);
wattron(titleWin, A_BOLD);
waddstr(titleWin, "Game of Life");
wattroff(titleWin, A_BOLD);
// main display border window
borderWin = newwin(LINES - TITLE_WINDOW_HEIGHT, COLS, TITLE_WINDOW_HEIGHT, 0);
box(borderWin, 0, 0);
// main display window
mainWin = newwin(LINES - TITLE_WINDOW_HEIGHT - 2, COLS - 2, TITLE_WINDOW_HEIGHT + 1, 1);
int cursX = COLS/2, cursY = (LINES - TITLE_WINDOW_HEIGHT - 2) / 2;
wmove(mainWin, cursY, cursX);
//refresh all
refresh();
wrefresh(titleWin);
wrefresh(borderWin);
wrefresh(mainWin);
viewport view;
view.x = 0;
view.y = 0;
view.width = COLS - 2;
view.height = LINES - TITLE_WINDOW_HEIGHT - 2;
std::vector<cell> cells(0);
updateInput(mainWin, &cursY, &cursX, cells, &view);
endwin();
return 0;
}
void updateInput(WINDOW *mainWin, int *cursY, int *cursX, std::vector<cell> cells, viewport *view) {
int ch;
while ((ch = getch()) != 'q') {
int update = false;
switch (ch) {
// left
case LEFT:
(*cursX)--;
if (*cursX < view->x) {
view->x--;
renderCells(mainWin, *cursX, *cursY, cells, *view);
}
break;
// right
case RIGHT:
(*cursX)++;
if (*cursX >= view->x + view->width) {
view->x++;
renderCells(mainWin, *cursX, *cursY, cells, *view);
}
break;
// up
case UP:
(*cursY)--;
if (*cursY < view->y) {
view->y--;
renderCells(mainWin, *cursX, *cursY, cells, *view);
}
break;
// down
case DOWN:
(*cursY)++;
if (*cursY >= view->y + view->height) {
view->y++;
renderCells(mainWin, *cursX, *cursY, cells, *view);
}
break;
// toggle cell
case TOGGLE_CELL:
if (findCellByCoords(cells, *cursX, *cursY)) {
removeCellByCoords(&cells, *cursX, *cursY);
waddch(mainWin, ' ');
}
else {
cell newCell;
newCell.x = *cursX;
newCell.y = *cursY;
cells.push_back(newCell);
}
renderCells(mainWin, *cursX, *cursY, cells, *view);
break;
// step game
case STEP_GAME:
std::vector<cell> newCells = tickGoL(cells);
cells.clear();
cells = newCells;
renderCells(mainWin, *cursX, *cursY, cells, *view);
break;
}
wmove(mainWin, *cursY - view->y, *cursX - view->x);
wrefresh(mainWin);
}
}
void renderCells(WINDOW *mainWin, const int &cursY, const int &cursX, std::vector<cell> &cells, const viewport &view) {
wclear(mainWin);
for (std::vector<cell>::iterator curCell = cells.begin(); curCell != cells.end(); ++curCell) {
if (curCell->y - view.y >= 0 && curCell->y - view.y < view.height && curCell->x - view.x >= 0 && curCell->x - view.x < view.width) {
wmove(mainWin, curCell->y - view.y, curCell->x - view.x);
waddch(mainWin, '#');
}
}
wmove(mainWin, cursY - view.y, cursX - view.x);
wrefresh(mainWin);
}
void removeCellByCoords(std::vector<cell> *cells, const int x, const int y) {
std::vector<cell>::iterator cellToDelete;
for (std::vector<cell>::iterator curCell = cells->begin(); curCell != cells->end(); ++curCell) {
if (curCell->x == x && curCell->y == y) {
cellToDelete = curCell;
break;
}
}
cells->erase(cellToDelete);
}
const bool findCellByCoords(const std::vector<cell> &cells, const int x, const int y) {
for (int i = 0; i < cells.size(); ++i) {
if (cells[i].x == x && cells[i].y == y) {
return true;
}
}
return false;
}
const int findNumNeighbours(const std::vector<cell> &cells, const int x, const int y) {
int numNeighbours = 0;
for (int r = -1; r <= 1; ++r) {
for (int c = -1; c <= 1; ++c) {
//check not self
if (findCellByCoords(cells, x + c, y + r) && !(r == 0 && c == 0)) {
numNeighbours++;
}
}
}
return numNeighbours;
}
std::vector<cell> tickGoL(const std::vector<cell> &cells) {
std::vector<cell> newCells(0);
for (int i = 0; i < cells.size(); ++i) {
cell currentCell = cells[i];
int numNeighbours = 0;
for (int r = -1; r <= 1; ++r) {
for (int c = -1; c <= 1; ++c) {
//check not self
if (r == 0 && c == 0) {
continue;
}
else if (findCellByCoords(cells, currentCell.x + c, currentCell.y + r)) {
numNeighbours++;
}
else {
//check empty neighbours then birth cell if hasn't been birthed already
if (findNumNeighbours(cells, currentCell.x + c, currentCell.y + r) == 3
&& !findCellByCoords(newCells, currentCell.x + c, currentCell.y + r)) {
cell newCell;
newCell.x = currentCell.x + c;
newCell.y = currentCell.y + r;
newCells.push_back(newCell);
}
}
}
}
//keep cells alive
if (numNeighbours >= 2 && numNeighbours <= 3) {
newCells.push_back(currentCell);
}
}
return newCells;
}