-
Notifications
You must be signed in to change notification settings - Fork 7
/
Field.cpp
53 lines (41 loc) · 869 Bytes
/
Field.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
#include "Field.hpp"
#include "Cell.hpp"
Field::Field()
{
}
void Field::setSize(int width, int height)
{
m_width = width;
m_height = height;
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
m_cells.append(new Cell(this, x, y));
}
}
}
void Field::setNumberOfMines(int number)
{
m_numberOfMines = number;
}
void Field::generate()
{
int minesToPlace = m_numberOfMines;
while (minesToPlace > 0) {
Cell *cell = m_cells.at(qrand() % m_cells.count());
if (cell->haveMine()) {
continue;
}
cell->setHaveMine(true);
--minesToPlace;
}
}
Cell *Field::cellAt(int x, int y) const
{
if (x < 0 || x >= m_width) {
return 0;
}
if (y < 0 || y >= m_height) {
return 0;
}
return m_cells.at(x + y * m_width);
}