-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.cpp
More file actions
executable file
·102 lines (88 loc) · 1.83 KB
/
Copy pathdata.cpp
File metadata and controls
executable file
·102 lines (88 loc) · 1.83 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
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
#include "data.h"
#include <QPoint>
#include <QList>
#include <time.h>
#include <QDebug>
Data *Data::_data = nullptr;
Data::Data(int row, int column, int cellwith, int mines)
:_row(row),
_column(column),
_cellWidth(cellwith),
_mines(mines)
{
init();
deployMines();
}
void Data::init()
{
_gameStatus = G_runnig;
for(int i = 0; i < _row; i++)
{
QList<int> rowlist;
for(int j = 0; j < _column; j++)
{
rowlist.append(0);
}
_matrix.append(rowlist);
}
}
//雷的数据
void Data::deployMines()
{
srand(time(NULL));
QList<QPoint> list;
for(int i = 0; i < _row; i++)
{
for(int j = 0; j < _column; j++)
{
list.append(QPoint(i, j));
}
}
int index;
int row, column;
for(int i = 0; i < _mines; i++)
{
index = rand() % list.length();
row = list[index].x();
column = list[index].y();
_matrix[row][column] = -1;
addMine(row - 1, column -1);
addMine(row - 1, column);
addMine(row - 1, column + 1);
addMine(row, column -1);
addMine(row, column + 1);
addMine(row + 1, column - 1);
addMine(row + 1, column);
addMine(row + 1, column + 1);
list.removeAt(index);
}
}
void Data::addMine(int x, int y)
{
//判断x,y两个的坐标不超过界限
if(x < 0 || y < 0 || x >= _row || y >= _column)
{
return;
}
//-1表示雷
if(_matrix[x][y] != -1)
{
_matrix[x][y] += 1;
}
}
Data *Data::dis()
{
if(_data==nullptr)
{
_data = new Data;
}
return _data;
}
void Data::realease()
{
if(_data!=nullptr)
{
delete _data;
_data = nullptr;
}
}