-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameOfLifeModel.cpp
362 lines (293 loc) · 8.08 KB
/
GameOfLifeModel.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
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
#include "GameOfLifeModel.h"
#include <QRandomGenerator>
#include <QFile>
#include <QRect>
#include <QSize>
#include <QThread>
#include <QtTest/QtTest>
#include <QDateTime>
#include <QDebug>
/////////////////////////////////////////////////////////
/// CONSTANTS
/////////////////////////////////////////////////////////
static const char IDENTIFIER_LIVING_CELL = '1';
static const char IDENTIFIER_DEAD_CELL = '0';
static const char IDENTIFIER_NEXT_LINE = '\n';
GameOfLifeModel::GameOfLifeModel(bool editable, QObject* parent)
: QAbstractTableModel(parent)
, m_isEditable(editable)
{
m_currentStateContainer.resize(size, false);
}
bool GameOfLifeModel::isEditable() const
{
return m_isEditable;
}
QHash<int, QByteArray> GameOfLifeModel::roleNames() const
{
return {
{CellRole, "value"}};
}
int GameOfLifeModel::rowCount(const QModelIndex& parent) const
{
if (parent.isValid())
{
return 0;
}
return height;
}
int GameOfLifeModel::columnCount(const QModelIndex& parent) const
{
if (parent.isValid())
{
return 0;
}
return width;
}
QVariant GameOfLifeModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid() || role != CellRole)
{
return QVariant();
}
return QVariant(m_currentStateContainer[cellIndex({index.column(),
index.row()})]);
}
bool GameOfLifeModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if (role != CellRole || data(index, role) == value) // so it can't be set or it doesn't need to be set
{
return false;
}
m_currentStateContainer[cellIndex({index.column(), index.row()})] = value.toBool();
Q_EMIT dataChanged(index, index, {role});
return true;
}
Qt::ItemFlags GameOfLifeModel::flags(const QModelIndex& index) const
{
Q_UNUSED(index)
Qt::ItemFlags flag;
if (m_isEditable)
{
flag = Qt::ItemIsEditable;
}
return flag;
}
void GameOfLifeModel::generatePattern()
{
clearPattern();
for (int i = 0; i < size; i++)
{
const quint32 randomNumber = QRandomGenerator::global()->generate() % 100;
if (randomNumber < m_livingCellsAtBeginningAsPercentage)
{
m_currentStateContainer[i] = true;
}
}
m_loopIsStopping = false;
Q_EMIT dataChanged(index(0, 0), index(height - 1, width - 1), {CellRole});
}
void GameOfLifeModel::nextStep()
{
qint64 startTime = QDateTime::currentMSecsSinceEpoch();
StateContainer newStateContainer(size);
for (int i = 0; i < size; i++)
{
const quint32 randomNumber = QRandomGenerator::global()->generate() % m_mutationConstant;
bool currentState = m_currentStateContainer[i];
int cellNeighboursCount = this->cellNeighboursCount(cellCoordinatesFromIndex(i));
if (currentState)
{
newStateContainer[i] = cellNeighboursCount == TwoNeighbours || cellNeighboursCount == ThreeNeighbours; // 2 or 3, so the cell lives
}
else
{
newStateContainer[i] = cellNeighboursCount == ThreeNeighbours || randomNumber == 0U; // 3 so the cell gets alive or because of the mutationConstant
}
}
m_currentStateContainer = std::move(newStateContainer);
Q_EMIT dataChanged(index(0, 0), index(height - 1, width - 1), {CellRole});
m_stepCount++;
Q_EMIT stepCountChanged();
qDebug() << QDateTime::currentMSecsSinceEpoch() - startTime << "ms";
}
void GameOfLifeModel::startInfiniteLoop()
{
m_loopIsStopping = false;
while (!m_loopIsStopping)
{
nextStep();
QTest::qWait(m_delay); // see on the linux variant
}
}
void GameOfLifeModel::startLoop()
{
m_loopIsStopping = false;
for (quint32 i = 0; i < m_loopCount && !m_loopIsStopping; i++)
{
nextStep();
QTest::qWait(m_delay); // see on the linux variant
}
}
bool GameOfLifeModel::loadFile(const QString fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
{
return false;
}
QTextStream in(&file);
loadPattern(in.readAll());
file.close();
return true;
}
void GameOfLifeModel::loadPattern(const QString& plainText)
{
clearPattern();
QStringList rows = plainText.split(IDENTIFIER_NEXT_LINE);
QSize patternSize(0, rows.count());
// knowing the length of each single row
for (QString row : rows)
{
if (row.size() > patternSize.width())
{
patternSize.setWidth(row.size());
}
}
QPoint patternLocation((width - patternSize.width()) / 2, (height - patternSize.height()) / 2);
for (int y = 0; y < patternSize.height(); y++)
{
const QString line = rows[y];
for (int x = 0; x < line.length(); x++)
{
QPoint cellPosition(x + patternLocation.x(), y + patternLocation.y());
m_currentStateContainer[cellIndex(cellPosition)] = line[x] == '1';
}
}
Q_EMIT dataChanged(index(0, 0), index(height - 1, width - 1), {CellRole});
}
bool GameOfLifeModel::saveFile(const QString fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
return false;
}
file.write(savePattern());
file.close();
return true;
}
void GameOfLifeModel::clearPattern()
{
m_stepCount = 0U;
Q_EMIT stepCountChanged();
m_currentStateContainer.fill(false);
Q_EMIT dataChanged(index(0, 0), index(height - 1, width - 1), {CellRole});
}
void GameOfLifeModel::stopLoop()
{
m_loopIsStopping = true;
}
QByteArray GameOfLifeModel::savePattern()
{
QByteArray rows;
for (int i = 0; i < m_currentStateContainer.size(); i++)
{
if (i % width == 0 && i != 0)
{
rows.append(IDENTIFIER_NEXT_LINE);
}
if (m_currentStateContainer[i])
{
rows.append(IDENTIFIER_LIVING_CELL);
}
else
{
rows.append(IDENTIFIER_DEAD_CELL);
}
}
return rows;
}
int GameOfLifeModel::cellNeighboursCount(const QPoint& cellCoordinates) const
{
int count = 0;
for (int x = -1; x < 2; x++)
{
for (int y = -1; y < 2; y++)
{
if (x == 0 && y == 0)
{
continue;
}
const QPoint neighbourPosition{cellCoordinates.x() + x, cellCoordinates.y() + y};
if (!areCellCoordinatesValid(neighbourPosition))
{
continue;
}
if (m_currentStateContainer[cellIndex(neighbourPosition)])
{
count++;
}
if (count > 3)
{
return count;
}
}
}
return count;
}
bool GameOfLifeModel::areCellCoordinatesValid(const QPoint& cellCoordinates)
{
return QRect(0, 0, width, height).contains(cellCoordinates);
}
QPoint GameOfLifeModel::cellCoordinatesFromIndex(int cellIndex)
{
return {cellIndex % width, cellIndex / width};
}
int GameOfLifeModel::cellIndex(const QPoint& cellCoordinates)
{
return cellCoordinates.y() * width + cellCoordinates.x();
}
int GameOfLifeModel::getLivingCellsAtBeginningAsPercentage() const
{
return m_livingCellsAtBeginningAsPercentage;
}
void GameOfLifeModel::setLivingCellsAtBeginningAsPercentage(int newLivingCellsAtBeginningAsPercentage)
{
if (m_livingCellsAtBeginningAsPercentage == newLivingCellsAtBeginningAsPercentage)
{
return;
}
m_livingCellsAtBeginningAsPercentage = newLivingCellsAtBeginningAsPercentage;
Q_EMIT livingCellsAtBeginningAsPercentageChanged();
}
void GameOfLifeModel::setLoopCount(quint32 newLoopCount)
{
if (m_loopCount == newLoopCount)
{
return;
}
m_loopCount = newLoopCount;
Q_EMIT loopCountChanged();
}
quint32 GameOfLifeModel::getStepCount() const
{
return m_stepCount;
}
quint32 GameOfLifeModel::getLoopCount() const
{
return m_loopCount;
}
quint32 GameOfLifeModel::getDelay() const
{
return m_delay;
}
void GameOfLifeModel::setDelay(quint32 newDelay)
{
if (m_delay == newDelay)
{
return;
}
m_delay = newDelay;
Q_EMIT delayChanged();
}