-
Notifications
You must be signed in to change notification settings - Fork 0
/
minecell.cpp
120 lines (106 loc) · 3.14 KB
/
minecell.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
#include <QApplication>
#include <QColor>
#include <QDebug>
#include <QMainWindow>
#include <QMouseEvent>
#include <QPainter>
#include <QPen>
#include "minecell.h"
MineCell::MineCell(int size, QWidget *parent)
: QPushButton(parent), value(0), bomb(false), marked(false), opened(false)
{
this->cell = {0.0, 0.0, (double)size, (double)size};
this->setMinimumSize(size, size);
this->setMaximumSize(size, size);
}
void MineCell::addNeighbour(MineCell *neighbour)
{
this->neighbours.push_back(neighbour);
this->value += neighbour->isBomb() ? 1 : 0;
connect(neighbour, SIGNAL(hasOpened()), this, SLOT(propagateOpen()));
}
bool MineCell::isBomb() { return this->bomb; }
void MineCell::setBomb() { this->bomb = true; }
bool MineCell::isOpened() { return this->opened; }
bool MineCell::isMarked() { return this->marked; }
void MineCell::setMarked()
{
if (!this->marked)
this->toggleMarked();
}
void MineCell::toggleMarked()
{
this->marked = !this->marked;
if (this->marked) {
this->setText("*");
} else {
this->setText("");
}
emit sigMarked(this->marked);
this->repaint();
}
int MineCell::getValue() { return this->value; }
void MineCell::paintEvent(QPaintEvent *event)
{
if (this->opened) {
QPainter painter(this);
painter.drawRect(this->cell);
painter.fillRect(this->cell, Qt::lightGray);
if (this->value != 0) {
painter.setFont(QFont(tr("Ubuntu"), this->cell.height() * 0.4));
// Green with increasing intensity with bigger numbers
painter.setPen(
QPen(QColor::fromHsl(120 + 60 * this->value, 100 * 2.55,
85 * 2.55 - 2.55 * 10 * this->value)));
painter.drawText(this->cell, Qt::AlignCenter,
tr(std::to_string(this->value).c_str()));
}
} else {
QPushButton::paintEvent(event);
}
}
void MineCell::propagateOpen()
{
if (!this->opened) {
this->opened = true;
if (this->value == 0)
emit hasOpened();
this->repaint();
}
}
void MineCell::setOpened()
{
if (!this->marked && !this->opened) {
if (this->bomb) {
emit hasExploded();
} else {
this->opened = true;
if (this->value == 0)
emit hasOpened();
this->repaint();
emit checkWin();
}
}
}
void MineCell::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
if (!this->opened) {
this->toggleMarked();
} else {
int count = 0;
foreach (MineCell *mc, neighbours) {
count += mc->marked ? 1 : 0;
}
if (count == this->value) {
foreach (MineCell *mc, neighbours) {
mc->setOpened();
}
}
}
emit checkWin();
} else if (event->button() == Qt::RightButton) {
setOpened();
}
QPushButton::mouseReleaseEvent(event);
}