-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
182 lines (145 loc) · 4.12 KB
/
mainwindow.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "mainwindow.h"
#include "cell.h"
#include <QVBoxLayout>
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QString>
#include <QTime>
class Cell;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QWidget *widget = new QWidget();
QGridLayout *gameField = new QGridLayout();
init(gameField);
resetGame();
widget->setLayout(gameField);
setCentralWidget(widget);
}
void MainWindow::init(QGridLayout *gameField){
for (int i=0;i<sizeField;i++) {
for (int j = 0; j < sizeField; ++j) {
mass[i][j] = new Cell(0,0,i,j);
gameField->addWidget(mass[i][j],i,j,1,1);
connect(mass[i][j], &Cell::clicked_left, this, &MainWindow::clickedLeft );
connect(mass[i][j], &Cell::clicked_right, this, &MainWindow::clickedRight);
}
}
}
void MainWindow::resetGame(){
for (int i=0;i<sizeField;i++) {
for (int j = 0; j < sizeField; ++j) {
mass[i][j]->setState(0);
mass[i][j]->resetHazard();
mass[i][j]->setText("");
}
}
// Seed the random generator with current time
QTime time = QTime::currentTime();
qsrand((uint)time.msec());
for (int k = 0; k < mineNumber;) {
int i = qrand() % sizeField;
int j = qrand() % sizeField;
if (99 == mass[i][j]->getHazard())
continue;
else {
setHazard(i,j);
++k;
}
}
}
bool MainWindow::isValid(int row, int column) {
return row >= 0 && column >= 0
&& row < sizeField && column < sizeField
&& 99 != mass[row][column]->getHazard();
}
void MainWindow::setHazard(int row, int column){
mass[row][column]->setMine();
int i = row;
int j = column;
if (isValid(i - 1, j - 1)) mass[i - 1][j - 1]->incValue();
if (isValid(i - 1, j)) mass[i - 1][j] ->incValue();
if (isValid(i - 1, j + 1)) mass[i - 1][j + 1]->incValue();
if (isValid(i, j - 1)) mass[i] [j - 1]->incValue();
if (isValid(i, j + 1)) mass[i] [j + 1]->incValue();
if (isValid(i + 1, j - 1)) mass[i + 1][j - 1]->incValue();
if (isValid(i + 1, j)) mass[i + 1][j] ->incValue();
if (isValid(i + 1, j + 1)) mass[i + 1][j + 1]->incValue();
}
void MainWindow::clickedRight(int row, int column)
{
Cell *a = mass[row][column];
int st = a->getState();
if(st == 1) //1-activated
return;
else if(st == 0) //0-wait
a->setState(2);
else if(st == 2) //2-mark
a->setState(0);
}
void MainWindow::clickedLeft(int row, int column)
{
// Alredy open
if(mass[row][column]->getState() == 1)
return;
// BANG!
if(mass[row][column]->getHazard() == 99){
openAll();
QMessageBox::information(0, "Game over", "! ! ! BANG ! ! !");
return;
}
if(mass[row][column]->getHazard() == 0)
openZero(row, column);
else{
mass[row][column]->setState(1);
countVictory --;
openValue(row, column);
}
// Win check
if(countVictory == 0){
openAll();
QMessageBox::information(0, "Game over", "You won !");
}
}
void MainWindow::openZero(int row, int column){
mass[row][column]->setState(1);
countVictory --;
for (int i=row-1; i<row+2; i++) {
for (int j = column-1; j < column+2; j++) {
if(isValid(i,j) && mass[i][j]->getState() == 0){
if(mass[i][j]->getHazard() == 0){
openZero(i,j);
}
else {
mass[i][j]->setState(1);
countVictory --;
}
}
}
}
}
void MainWindow::openValue(int row, int column){
for (int i=row-1; i<row+2; i++) {
for (int j = column-1; j < (column+2); j++) {
if(isValid(i,j) && mass[i][j]->getState() == 0){
if(mass[i][j]->getHazard() == 0)
openZero(i,j);
}
}
}
}
void MainWindow::openAll(){
for (int i=0;i<sizeField;i++) {
for (int j = 0; j < sizeField; ++j) {
mass[i][j]->setState(1);
if(mass[i][j]->getHazard() == 99){
mass[i][j]->setText("BANG");
mass[i][j]->setStyleSheet("background-color: rgb(250, 0, 0)");
}
}
}
}
MainWindow::~MainWindow()
{
}