-
Notifications
You must be signed in to change notification settings - Fork 7
/
Cell.cpp
56 lines (45 loc) · 986 Bytes
/
Cell.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
#include "Cell.hpp"
#include "Field.hpp"
Cell::Cell(Field *field, int x, int y)
{
m_field = field;
m_x = x;
m_y = y;
m_haveMine = false;
m_open = false;
}
int Cell::minesAround() const
{
int mines = 0;
for (Cell *cell : getNeighbors()) {
if (cell->haveMine()) {
++mines;
}
}
return mines;
}
void Cell::setHaveMine(bool haveMine)
{
m_haveMine = haveMine;
}
void Cell::open()
{
m_open = true;
}
void maybeAddCell(QVector<Cell*> *vector, Cell *cell)
{
if (cell) {
vector->append(cell);
}
}
QVector<Cell *> Cell::getNeighbors() const
{
QVector<Cell*> neighbors;
for (int x = m_x - 1; x <= m_x + 1; ++x) {
maybeAddCell(&neighbors, m_field->cellAt(x, m_y - 1));
maybeAddCell(&neighbors, m_field->cellAt(x, m_y + 1));
}
maybeAddCell(&neighbors, m_field->cellAt(m_x - 1, m_y));
maybeAddCell(&neighbors, m_field->cellAt(m_x + 1, m_y));
return neighbors;
}