-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameboard.cpp
More file actions
198 lines (168 loc) · 5.37 KB
/
Copy pathgameboard.cpp
File metadata and controls
198 lines (168 loc) · 5.37 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
#include "gameboard.h"
#include <QRandomGenerator>
#include <QMessageBox>
#include <algorithm>
GameBoard::GameBoard(QWidget *parent)
: QWidget(parent), remainingCells(BOARD_WIDTH * BOARD_HEIGHT - MINE_COUNT), gameOver(false) {
gridLayout = new QGridLayout(this);
gridLayout->setSpacing(1);
setLayout(gridLayout);
// Initialize cells vector
cells.resize(BOARD_HEIGHT);
for (int i = 0; i < BOARD_HEIGHT; ++i) {
cells[i].resize(BOARD_WIDTH);
}
initializeBoard();
}
GameBoard::~GameBoard() {
if (gridLayout) {
delete gridLayout;
}
for (auto &row : cells) {
for (auto cell : row) {
if (cell) {
delete cell;
}
}
}
}
void GameBoard::initializeBoard() {
// Create cells
for (int row = 0; row < BOARD_HEIGHT; ++row) {
for (int col = 0; col < BOARD_WIDTH; ++col) {
MineCell *cell = new MineCell(this);
cell->setPosition(row, col);
cells[row][col] = cell;
gridLayout->addWidget(cell, row, col);
connect(cell, &MineCell::cellClicked,
this, &GameBoard::handleCellClick);
connect(cell, &MineCell::cellRightClicked,
this, &GameBoard::handleCellRightClick);
}
}
placeMines();
calculateAdjacentMines();
}
void GameBoard::placeMines() {
int minesPlaced = 0;
while (minesPlaced < MINE_COUNT) {
int row = QRandomGenerator::global()->bounded(BOARD_HEIGHT);
int col = QRandomGenerator::global()->bounded(BOARD_WIDTH);
if (!cells[row][col]->hasMine()) {
cells[row][col]->setMine(true);
minesPlaced++;
}
}
}
void GameBoard::calculateAdjacentMines() {
for (int row = 0; row < BOARD_HEIGHT; ++row) {
for (int col = 0; col < BOARD_WIDTH; ++col) {
if (!cells[row][col]->hasMine()) {
cells[row][col]->setAdjacentMines(countAdjacentMines(row, col));
}
}
}
}
int GameBoard::countAdjacentMines(int row, int col) const {
int count = 0;
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 (isValidCell(newRow, newCol) && cells[newRow][newCol]->hasMine()) {
count++;
}
}
}
return count;
}
void GameBoard::handleCellClick(int row, int col) {
if (gameOver || cells[row][col]->isRevealed()) {
return;
}
if (cells[row][col]->hasMine()) {
revealAllMines();
handleGameOver(false);
return;
}
revealCell(row, col);
if (remainingCells == 0) {
handleGameOver(true);
}
}
void GameBoard::revealCell(int row, int col) {
MineCell *cell = cells[row][col];
if (cell->isRevealed() || !cell->icon().isNull()) {
return;
}
cell->setRevealed(true);
cell->setEnabled(true); // Keep enabled but with different style
cell->setFlat(true); // Make it look flat/revealed
remainingCells--;
if (cell->getAdjacentMines() > 0) {
cell->setText(QString::number(cell->getAdjacentMines()));
// Different colors for different numbers
QString color;
switch(cell->getAdjacentMines()) {
case 1: color = "blue"; break;
case 2: color = "green"; break;
case 3: color = "red"; break;
case 4: color = "darkblue"; break;
default: color = "darkred"; break;
}
cell->setStyleSheet(QString("QPushButton { background-color: #E0E0E0; color: %1; font-weight: bold; }").arg(color));
} else {
cell->setStyleSheet("QPushButton { background-color: #E0E0E0; }");
revealAdjacentCells(row, col);
}
}
void GameBoard::revealAdjacentCells(int row, int col) {
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 (isValidCell(newRow, newCol) && !cells[newRow][newCol]->isRevealed()) {
revealCell(newRow, newCol);
}
}
}
}
void GameBoard::handleCellRightClick(int row, int col) {
if (!gameOver && !cells[row][col]->isRevealed()) {
cells[row][col]->toggleFlag();
}
}
void GameBoard::revealAllMines() {
for (int row = 0; row < BOARD_HEIGHT; ++row) {
for (int col = 0; col < BOARD_WIDTH; ++col) {
if (cells[row][col]->hasMine()) {
cells[row][col]->setMineIcon();
cells[row][col]->setStyleSheet("QPushButton { background-color: red; }");
}
}
}
}
void GameBoard::handleGameOver(bool won) {
gameOver = true;
if (won) {
emit gameWon();
} else {
emit gameLost();
}
}
bool GameBoard::isValidCell(int row, int col) const {
return row >= 0 && row < BOARD_HEIGHT && col >= 0 && col < BOARD_WIDTH;
}
void GameBoard::resetGame() {
gameOver = false;
remainingCells = BOARD_WIDTH * BOARD_HEIGHT - MINE_COUNT;
for (int row = 0; row < BOARD_HEIGHT; ++row) {
for (int col = 0; col < BOARD_WIDTH; ++col) {
cells[row][col]->reset();
}
}
placeMines();
calculateAdjacentMines();
}