-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
257 lines (211 loc) · 7.33 KB
/
mainwindow.cpp
File metadata and controls
257 lines (211 loc) · 7.33 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), remainingFlag(MINE_COUNT), elapsedSeconds(0),
gameOver(false), firstClick(true)
{
initializeUI();
}
MainWindow::~MainWindow() {}
void MainWindow::initializeUI() {
centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
// Create layout
QVBoxLayout* mainLayout = new QVBoxLayout(centralWidget);
// Create info panel
QHBoxLayout* infoLayout = new QHBoxLayout();
flagCountLabel = new QLabel(QString("Flag: %1").arg(remainingFlag));
timerLabel = new QLabel("Time: 0");
newGameButton = new QPushButton("New Game");
infoLayout->addWidget(flagCountLabel);
infoLayout->addWidget(newGameButton);
infoLayout->addWidget(timerLabel);
mainLayout->addLayout(infoLayout);
// Create grid
gridLayout = new QGridLayout();
gridLayout->setSpacing(1);
buttons.resize(GRID_SIZE);
for(int i = 0; i < GRID_SIZE; ++i) {
buttons[i].resize(GRID_SIZE);
for(int j = 0; j < GRID_SIZE; ++j) {
MineButton* button = new MineButton(i, j);
button->setFixedSize(30, 30);
button->setContextMenuPolicy(Qt::CustomContextMenu);
connect(button, &QPushButton::clicked, this, &MainWindow::handleClick);
connect(button, &QPushButton::customContextMenuRequested, this, &MainWindow::handleRightClick);
buttons[i][j] = button;
gridLayout->addWidget(button, i, j);
}
}
mainLayout->addLayout(gridLayout);
// Set up timer
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MainWindow::updateTimer);
connect(newGameButton, &QPushButton::clicked, this, &MainWindow::newGame);
}
// Place Mines expect on the player first click
void MainWindow::placeMines(int rowFirstClick, int colFirstClick) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, GRID_SIZE - 1);
int placedMines = 0;
while(placedMines < MINE_COUNT) {
int row = dis(gen);
int col = dis(gen);
if (!(row == rowFirstClick && col == colFirstClick)){
MineButton* button = buttons[row][col];
if(!button->hasMine) {
button->hasMine = true;
placedMines++;
}
}
}
calculateAdjacentMines();
}
void MainWindow::calculateAdjacentMines() {
for(int i = 0; i < GRID_SIZE; ++i) {
for(int j = 0; j < GRID_SIZE; ++j) {
if(!buttons[i][j]->hasMine) {
int count = 0;
auto adjacentCells = getAdjacentCells(i, j);
for(const auto& cell : adjacentCells) {
if(buttons[cell.first][cell.second]->hasMine) {
count++;
}
}
buttons[i][j]->adjacentMines = count;
}
}
}
}
QVector<QPair<int, int>> MainWindow::getAdjacentCells(int row, int col) {
QVector<QPair<int, int>> adjacent;
for(int i = -1; i <= 1; ++i) {
for(int j = -1; j <= 1; ++j) {
if(i == 0 && j == 0) continue;
int newRow = row + i;
int newCol = col + j;
if(newRow >= 0 && newRow < GRID_SIZE &&
newCol >= 0 && newCol < GRID_SIZE) {
adjacent.push_back({newRow, newCol});
}
}
}
return adjacent;
}
void MainWindow::handleClick() {
if(gameOver) return;
MineButton* button = qobject_cast<MineButton*>(sender());
if(!button || button->isMarked) return;
if(firstClick) {
firstClick = false;
timer->start(1000);
placeMines(button->row, button->col);
}
revealCell(button->row, button->col);
if(button->hasMine) {
revealAllMines();
timer->stop();
gameOver = true;
QMessageBox::information(this, "Game Over", "You hit a mine!");
} else if(checkWin()) {
timer->stop();
gameOver = true;
QMessageBox::information(this, "Congratulations", "You won!");
}
}
void MainWindow::handleRightClick() {
if(gameOver) return;
MineButton* button = qobject_cast<MineButton*>(sender());
if(!button || button->isRevealed) return;
if(button->isMarked){
button->isMarked = false;
button->setText("");
remainingFlag++;
}
else{
if(remainingFlag>0){
button->isMarked = true;
button->setText("🚩");
remainingFlag--;
}
}
flagCountLabel->setText(QString("Flag: %1").arg(remainingFlag));
}
void MainWindow::revealCell(int row, int col) {
MineButton* button = buttons[row][col];
if(button->isRevealed || button->isMarked) return;
button->isRevealed = true;
if(button->hasMine) {
button->setText("💣");
button->setStyleSheet("background-color: red;");
} else if(button->adjacentMines > 0) {
button->setText(QString::number(button->adjacentMines));
// Set different colors for different numbers
QVector<QString> colors = {"blue", "green", "red", "purple", "maroon", "turquoise", "black", "gray"};
button->setStyleSheet(QString("color: %1;").arg(colors[button->adjacentMines - 1]));
} else {
// Reveal adjacent cells recursively
const auto adjacentCells = getAdjacentCells(row, col);
for(const auto& cell : adjacentCells) {
revealCell(cell.first, cell.second);
}
button->setStyleSheet("background-color: lightgray;");
}
}
void MainWindow::revealAllMines() {
for(int i = 0; i < GRID_SIZE; ++i) {
for(int j = 0; j < GRID_SIZE; ++j) {
MineButton* button = buttons[i][j];
if(button->hasMine) {
button->setText("💣");
button->setStyleSheet("background-color: red;");
}
}
}
}
bool MainWindow::checkWin() {
for(int i = 0; i < GRID_SIZE; ++i) {
for(int j = 0; j < GRID_SIZE; ++j) {
MineButton* button = buttons[i][j];
if(!button->hasMine && !button->isRevealed) {
return false;
}
}
}
return true;
}
void MainWindow::updateTimer() {
elapsedSeconds++;
timerLabel->setText(QString("Time: %1").arg(elapsedSeconds));
}
void MainWindow::newGame() {
for (int i = 0; i < GRID_SIZE; ++i) {
for (int j = 0; j < GRID_SIZE; ++j) {
if (buttons[i][j]) {
buttons[i][j]->deleteLater();
}
}
}
buttons.clear();
remainingFlag = MINE_COUNT;
elapsedSeconds = 0;
gameOver = false;
firstClick = true;
flagCountLabel->setText(QString("Flag: %1").arg(remainingFlag));
timerLabel->setText("Time: 0");
timer->stop();
buttons.resize(GRID_SIZE);
for (int i = 0; i < GRID_SIZE; ++i) {
buttons[i].resize(GRID_SIZE);
for (int j = 0; j < GRID_SIZE; ++j) {
MineButton* button = new MineButton(i, j);
button->setFixedSize(30, 30);
button->setContextMenuPolicy(Qt::CustomContextMenu);
connect(button, &QPushButton::clicked, this, &MainWindow::handleClick);
connect(button, &QPushButton::customContextMenuRequested, this, &MainWindow::handleRightClick);
buttons[i][j] = button;
gridLayout->addWidget(button, i, j);
}
}
}