-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathField.cpp
More file actions
67 lines (53 loc) · 1.04 KB
/
Field.cpp
File metadata and controls
67 lines (53 loc) · 1.04 KB
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
#include "Field.hpp"
#include "Cell.hpp"
Field::Field(QObject *parent) :
QObject(parent),
m_width(10),
m_height(10)
{
applySize();
}
Cell *Field::cellAt(int x, int y)
{
if (x < 0 || x >= width()) {
return nullptr;
}
if (y < 0 || y >= height()) {
return nullptr;
}
int index = x + y * width();
if (index >= m_cells.count()) {
return nullptr;
}
return m_cells.at(index);
}
void Field::setWidth(int width)
{
if (m_width == width) {
return;
}
m_width = width;
emit widthChanged(width);
}
void Field::setHeight(int height)
{
if (m_height == height) {
return;
}
m_height = height;
emit heightChanged(height);
}
void Field::applySize()
{
for (Cell *cell : m_cells) {
delete cell;
}
m_cells.clear();
for (int y = 0; y < height(); ++y) {
for (int x = 0; x < width(); ++x) {
Cell *cell = new Cell(this);
cell->setKey(qrand() % 6);
m_cells.append(cell);
}
}
}