-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamewidget.cpp
118 lines (104 loc) · 3.05 KB
/
gamewidget.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
#include "gamewidget.h"
#include <QDebug>
#include <QMessageBox>
#include <QMouseEvent>
#include <QPainter>
#include <QRectF>
#include <QTimer>
#include <qmath.h>
GOLWidget::GOLWidget(QWidget *parent)
: QWidget(parent), timer(new QTimer(this)), size(50), current(size) {
timer->setInterval(100);
masterColor = palette().color(QPalette::Light);
connect(timer, SIGNAL(timeout()), this, SLOT(next()));
}
GOLWidget::~GOLWidget() { delete ¤t; }
void GOLWidget::start() { timer->start(); }
void GOLWidget::stop() { timer->stop(); }
void GOLWidget::tick() {
timer->stop();
next();
}
void GOLWidget::clear() {
current.clear();
timer->stop();
update();
}
bool GOLWidget::isAlive(int x, int y) {
int power = 0;
power += current(x + 1, y) + current(x - 1, y) + current(x, y + 1) +
current(x, y - 1) + current(x + 1, y - 1) + current(x - 1, y + 1) +
current(x - 1, y - 1) + current(x + 1, y + 1);
if (((current(x, y) == true) && (power == 2)) || (power == 3))
return true;
return false;
}
void GOLWidget::next() {
int notChanged = 0;
GOLMatrix next(size);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
next(i, j) = isAlive(i, j);
if (next(i, j) == current(i, j))
notChanged++;
}
}
if (notChanged == size * size) {
QMessageBox::information(this, tr("Game over"),
tr("All next generations will be the same."));
clear();
return;
}
current = next;
update();
}
void GOLWidget::paintEvent(QPaintEvent *) {
QPainter p(this);
paintGrid(p);
paintCells(p);
}
void GOLWidget::mousePressEvent(QMouseEvent *e) {
double cellWidth = (double)width() / size;
double cellHeight = (double)height() / size;
int y = floor(e->y() / cellHeight);
int x = floor(e->x() / cellWidth);
// universe[k*size + j] = !universe[k*size + j];
current(y, x) ^= true;
update();
}
void GOLWidget::mouseMoveEvent(QMouseEvent *e) {
double cellWidth = (double)width() / size;
double cellHeight = (double)height() / size;
int y = floor(e->y() / cellHeight);
int x = floor(e->x() / cellWidth);
if (!current(y, x)) {
current(y, x) ^= true;
update();
}
}
void GOLWidget::paintGrid(QPainter &p) {
QRect borders(0, 0, width() - 1, height() - 1);
QColor gridColor = masterColor;
p.setPen(gridColor);
double cellWidth = (double)width() / size;
double cellHeight = (double)height() / size;
for (double k = cellWidth; k <= width(); k += cellWidth)
p.drawLine(k, 0, k, height());
for (double k = cellHeight; k <= height(); k += cellHeight)
p.drawLine(0, k, width(), k);
p.drawRect(borders);
}
void GOLWidget::paintCells(QPainter &p) {
double cellWidth = (double)width() / size;
double cellHeight = (double)height() / size;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (current(i, j)) {
qreal left = (qreal)(cellWidth * j);
qreal top = (qreal)(cellHeight * i);
QRectF r(left, top, (qreal)cellWidth, (qreal)cellHeight);
p.fillRect(r, QBrush(masterColor));
}
}
}
}