-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.cpp
266 lines (209 loc) · 8.38 KB
/
widget.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
#include "widget.h"
#include <QJsonDocument>
#include <QJsonArray>
#include <QDir>
#include <QDebug>
#include <QRandomGenerator>
#include <QLineEdit>
#include <unistd.h>
void Widget::varUpdates() {
NUM_OF_CELLS = ROWS_AND_COLUMNS * ROWS_AND_COLUMNS;
GRID_PXL_DIM = 12 * ROWS_AND_COLUMNS;
CELL_PXL_DIM = GRID_PXL_DIM / ROWS_AND_COLUMNS;
WINDOW_SIZE = QRect(SCREEN_POS, SCREEN_POS, GRID_PXL_DIM + (WINDOW_BUFFER * 2),
GRID_PXL_DIM + WINDOW_BUFFER + GRID_PXL_DIM / 10);
BUTTON_Y = GRID_PXL_DIM + 10;
cells = QVector<cell>(NUM_OF_CELLS);
cellsNextGen = QVector<cell>(NUM_OF_CELLS);
NORTH_CELL = -ROWS_AND_COLUMNS;
SOUTH_CELL = ROWS_AND_COLUMNS;
}
void Widget::buttons(bool repaint) {
if (repaint) {
this->updateButton->hide();
varUpdates();
this->setGeometry(WINDOW_SIZE);
}
this->updateButton->move(((GRID_PXL_DIM / 7) * 0) + WINDOW_BUFFER, BUTTON_Y);
this->updateButton->setText("refresh");
this->updateButton->show();
this->regenButton->move(((GRID_PXL_DIM / 7) * 1) + WINDOW_BUFFER, BUTTON_Y);
this->regenButton->setText("generate");
this->regenButton->show();
this->saveToJsonButton->move(((GRID_PXL_DIM / 7) * 2) + WINDOW_BUFFER, BUTTON_Y);
this->saveToJsonButton->setText("save");
this->saveToJsonButton->show();
this->loadCustomButton->move(((GRID_PXL_DIM / 7) * 3) + WINDOW_BUFFER, BUTTON_Y);
this->loadCustomButton->setText("load");
this->loadCustomButton->show();
this->customGridSizeButton->move(((GRID_PXL_DIM / 7) * 4) + WINDOW_BUFFER, BUTTON_Y);
this->customGridSizeButton->setText("gridsize");
this->customGridSizeButton->show();
this->spawnDensityButton->move(((GRID_PXL_DIM / 7) * 5) + WINDOW_BUFFER, BUTTON_Y);
this->spawnDensityButton->setText("density");
this->spawnDensityButton->show();
this->autoUpdateButton->move(((GRID_PXL_DIM / 7) * 6) + WINDOW_BUFFER, BUTTON_Y);
this->autoUpdateButton->setText("auto");
this->autoUpdateButton->show();
update();
}
Widget::Widget(QWidget *parent) : QWidget(parent) {
this->setGeometry(WINDOW_SIZE);
this->show();
connect(updateButton, SIGNAL(clicked()), this, SLOT(updateGrid()));
connect(regenButton, SIGNAL(clicked()), this, SLOT(regenerate()));
connect(saveToJsonButton, SIGNAL(clicked()), this, SLOT(saveToJson()));
connect(loadCustomButton, SIGNAL(clicked()), this, SLOT(loadCustom()));
connect(customGridSizeButton, SIGNAL(clicked()), this, SLOT(gridSize()));
connect(spawnDensityButton, SIGNAL(clicked()), this, SLOT(updateSpawnDensity()));
connect(autoUpdateButton, SIGNAL(clicked()), this, SLOT(autoUpdate()));
buttons(false);
}
void Widget::updateGrid() {
this->checkCells();
this->performChanges();
cells = cellsNextGen;
update();
}
void Widget::regenerate() {
int *setState = new int;
for (int i = 0; i < NUM_OF_CELLS; i++) {
*setState = QRandomGenerator::global()->bounded(spawn_density);
if (*setState == 1)
this->cells[i].state = true;
else
this->cells[i].state = false;
cells[i].x = i % ROWS_AND_COLUMNS;
for (int y = 0; y < ROWS_AND_COLUMNS; y++)
if (i >= y * ROWS_AND_COLUMNS && i < (y + 1) * ROWS_AND_COLUMNS)
this->cells[i].y = y;
}
update();
delete setState;
setState = nullptr;
}
void Widget::paintEvent(QPaintEvent *) {
QPainter bigbadwolf(this);
for (int i = 0; i < CELL_PXL_DIM * ROWS_AND_COLUMNS + 1; i += CELL_PXL_DIM) {
bigbadwolf.drawLine(WINDOW_BUFFER, i + WINDOW_BUFFER, CELL_PXL_DIM * ROWS_AND_COLUMNS + WINDOW_BUFFER,
i + WINDOW_BUFFER);
bigbadwolf.drawLine(i + WINDOW_BUFFER, WINDOW_BUFFER, i + WINDOW_BUFFER,
CELL_PXL_DIM * ROWS_AND_COLUMNS + WINDOW_BUFFER);
}
for (int i = 0; i < NUM_OF_CELLS; i++) {
if (cells[i].state) {
bigbadwolf.fillRect(
QRect(WINDOW_BUFFER + cells[i].x * CELL_PXL_DIM, WINDOW_BUFFER + cells[i].y * CELL_PXL_DIM,
CELL_PXL_DIM, CELL_PXL_DIM), FULL_CELL);
}
}
}
void Widget::checkCells() {
// reset neighbours
for (int i = 0; i < NUM_OF_CELLS; i++)
cells[i].neighbours = 0;
for (int i = 0; i < NUM_OF_CELLS; i++) {
// NORTH CELL ––working––
if (i > ROWS_AND_COLUMNS - 1)
if (cells[i + NORTH_CELL].state)
cells[i].neighbours++;
// SOUTH CELL ––working––
if (i < NUM_OF_CELLS - ROWS_AND_COLUMNS - 1)
if (cells[i + SOUTH_CELL].state)
cells[i].neighbours++;
// EAST CELL ––working––
if (i % ROWS_AND_COLUMNS < ROWS_AND_COLUMNS - 1)
if (cells[i + EAST_CELL].state)
cells[i].neighbours++;
// WEST CELL ––working––
if (i % ROWS_AND_COLUMNS > 0)
if (cells[i + WEST_CELL].state)
cells[i].neighbours++;
/* NORTH EAST
* – we need to avoid top most row and far right column
* – relative to the cell in consideration, the index will be the cell index minus ROWS_AND_COLLUMNS plus 1
*/
if ((i > ROWS_AND_COLUMNS - 1) && (i % ROWS_AND_COLUMNS < ROWS_AND_COLUMNS - 1))
if (cells[i + EAST_CELL + NORTH_CELL].state)
cells[i].neighbours++;
// north west
if ((i > ROWS_AND_COLUMNS - 1) && (i % ROWS_AND_COLUMNS > 0))
if (cells[i + NORTH_CELL + WEST_CELL].state)
cells[i].neighbours++;
// south east
if ((i < NUM_OF_CELLS - ROWS_AND_COLUMNS - 1) && (i % ROWS_AND_COLUMNS < ROWS_AND_COLUMNS - 1))
if (cells[i + SOUTH_CELL + EAST_CELL].state)
cells[i].neighbours++;
// south west
if ((i < NUM_OF_CELLS - ROWS_AND_COLUMNS - 1) && (i % ROWS_AND_COLUMNS > 0))
if (cells[i + SOUTH_CELL + WEST_CELL].state)
cells[i].neighbours++;
}
}
void Widget::performChanges() {
cellsNextGen = cells;
for (int i = 0; i < NUM_OF_CELLS; i++) {
// any live cell with fewer than two neighbours dies
if (cells[i].state && cells[i].neighbours < 2)
cellsNextGen[i].state = false;
// any live cell with two or three live neighbours lives
if (cells[i].state == true && (cells[i].neighbours == 2 || cells[i].neighbours == 3))
cellsNextGen[i].state = true;
// any live cell with more than three neighbours dies
if (cells[i].state && cells[i].neighbours == 4)
cellsNextGen[i].state = false;
// any dead cell with exactly three live neighbours becomes a live cell
if (!cells[i].state && cells[i].neighbours == 3)
cellsNextGen[i].state = true;
}
}
void Widget::saveToJson() {
QDir::setCurrent("../Resources/");
QFile file;
file.setFileName("save.json");
QJsonArray array;
for (int i = 0; i < NUM_OF_CELLS; i++)
array.append(QJsonArray() << cells[i].x << cells[i].y << cells[i].state << cells[i].neighbours);
QJsonDocument doc(array);
file.open(QIODevice::WriteOnly | QIODevice::Text);
file.write(doc.toJson());
file.close();
}
void Widget::loadCustom() {
for (int i = 0; i < NUM_OF_CELLS; i++) {
this->cells[i].state = false;
cells[i].x = i % ROWS_AND_COLUMNS;
for (int y = 0; y < ROWS_AND_COLUMNS; y++)
if (i >= y * ROWS_AND_COLUMNS && i < (y + 1) * ROWS_AND_COLUMNS)
this->cells[i].y = y;
}
cells[220].state = true;
cells[220 + ROWS_AND_COLUMNS].state = true;
cells[220 - ROWS_AND_COLUMNS].state = true;
cells[221].state = true;
update();
}
void Widget::gridSize() {
// this->customGridSizeInput->show();
bool done;
int res = QInputDialog::getInt(this, tr("grid size"), tr("enter grid size"), ROWS_AND_COLUMNS, 0, 100, 1,
&done);
if (done) {
this->ROWS_AND_COLUMNS = res;
buttons(true);
}
}
void Widget::updateSpawnDensity() {
bool done;
this->spawn_density = QInputDialog::getInt(this, tr("spawn density"), tr("enter spawn density"), 7, 0, 999, 1,
&done);
regenerate();
}
void Widget::autoUpdate() {
unsigned int wait = 500000;
for (int i = 0; i < 40; ++i) {
updateGrid();
repaint();
usleep(wait);
}
}