-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameOfLifeModel.h
119 lines (103 loc) · 4.42 KB
/
GameOfLifeModel.h
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
#ifndef GAMEOFLIFEMODEL_H1B7AD7A5506F4C6995FCE4824C51B436
#define GAMEOFLIFEMODEL_H1B7AD7A5506F4C6995FCE4824C51B436
#include <QAbstractTableModel>
#include <QByteArray>
#include <QPoint>
#include <QVector>
class GameOfLifeModel : public QAbstractTableModel
{
Q_OBJECT
Q_PROPERTY(quint32 livingCellsAtBeginningAsPercentage READ getLivingCellsAtBeginningAsPercentage WRITE setLivingCellsAtBeginningAsPercentage NOTIFY livingCellsAtBeginningAsPercentageChanged)
Q_PROPERTY(quint32 loopCount READ getLoopCount WRITE setLoopCount NOTIFY loopCountChanged)
Q_PROPERTY(quint32 stepCount READ getStepCount NOTIFY stepCountChanged)
Q_PROPERTY(quint32 delay READ getDelay WRITE setDelay NOTIFY delayChanged)
public:
explicit GameOfLifeModel(bool editable = true, QObject* parent = nullptr);
// Model-Enum
enum Roles
{
CellRole
};
Q_ENUM(Roles)
// Cell-Enum
enum CellStates : quint32
{
TwoNeighbours = 2U,
ThreeNeighbours = 3U,
};
Q_ENUM(CellStates)
/////////////////////////////////////////////////////////
/// MODEL-FUNCTIONS
/////////////////////////////////////////////////////////
QHash<int, QByteArray> roleNames() const override;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::DisplayRole) override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
bool isEditable() const;
/////////////////////////////////////////////////////////
/// GAME-MECHANIC-FUNCTIONS
/////////////////////////////////////////////////////////
// generates a random pattern
Q_INVOKABLE void generatePattern();
// saves the pattern into a file
Q_INVOKABLE bool saveFile(const QString fileName);
// displaying the current pattern as 0 and 1 in a QByteArray
QByteArray savePattern();
// loads the pattern from a file
Q_INVOKABLE bool loadFile(const QString fileName);
// read out the file and recreate the pattern of 0 and 1 into a normal pattern
Q_INVOKABLE void loadPattern(const QString& plainText);
// clears the current pattern
Q_INVOKABLE void clearPattern();
// starts an infinite loop
Q_INVOKABLE void startInfiniteLoop();
// starts a loop with a specific amount of loopSteps
Q_INVOKABLE void startLoop();
// stops every current loop
Q_INVOKABLE void stopLoop();
// calculates the pattern of each next step
Q_INVOKABLE void nextStep();
Q_SIGNALS:
void livingCellsAtBeginningAsPercentageChanged();
void loopCountChanged();
void stepCountChanged();
void delayChanged();
private:
/////////////////////////////////////////////////////////
/// GETTER AND SETTER
/////////////////////////////////////////////////////////
int getLivingCellsAtBeginningAsPercentage() const;
void setLivingCellsAtBeginningAsPercentage(int newLivingCellsAtBeginningAsPercentage);
quint32 getLoopCount() const;
void setLoopCount(quint32 newLoopCount);
quint32 getStepCount() const;
quint32 getDelay() const;
void setDelay(quint32 newDelay);
/////////////////////////////////////////////////////////
/// CELL-CALCULATIONS
/////////////////////////////////////////////////////////
// calculates the living cellNeighbours of a cell
int cellNeighboursCount(const QPoint& cellCoordinates) const;
// checks if the given cellCoordinates are valid or not
static bool areCellCoordinatesValid(const QPoint& cellCoordinates);
// calculates the real coordinates based on a given index
static QPoint cellCoordinatesFromIndex(int cellIndex);
// calculates the index based on a given QPoint
static int cellIndex(const QPoint& cellCoordinates);
private:
static constexpr int width = 20;
static constexpr int height = 20;
static constexpr int size = width * height;
static constexpr quint32 m_mutationConstant = 1000U;
typedef QVector<int> StateContainer;
StateContainer m_currentStateContainer;
quint32 m_livingCellsAtBeginningAsPercentage = 50U;
quint32 m_loopCount = 100U;
quint32 m_stepCount = 0U;
quint32 m_delay = 0U;
bool m_loopIsStopping = false;
bool m_isEditable = true;
};
#endif // GAMEOFLIFEMODEL_H1B7AD7A5506F4C6995FCE4824C51B436