Mainwindow CPP
Mainwindow CPP
cpp
#include "MainWindow.h"
#include <QMessageBox>
#include <QRandomGenerator>
#include <QDebug>
layout->addLayout(buttonLayout);
widget->setLayout(layout);
if(gameOver){
return;
}
cells[hintIndex]->hint = true;
cells[hintIndex]->flag = false;
cells[hintIndex]->updateIcon();
buttons[hintIndex]->setIcon(cells[hintIndex]->icon);
} else {
return;
}
}
}
}
}
// if all unrevealed cells around a revealed cell are mines, mark them as
known mines
if (unrevealedCount == currentCell->adjacentMines) {
for (int adjIndex : adjacentIndices) {
if(!knownMines.contains(adjIndex)){
knownMines.append(adjIndex);
change = true;
}
}
}
}
}
}
}
}
}
// if the number of known mines around a revealed cell matches its adjacent
mines count, mark rest of the celss as safe
if (neighborMines == currentCell->adjacentMines) {
for (int adjIndex : adjacentIndices) {
if(!safeCells.contains(adjIndex)){
safeCells.append(adjIndex);
change = true;
}
}
}
}
}
}
distributeMines();
calculateAdjacentMines();
score = 0;
updateScore();
for (int i = 0; i < cells.size(); ++i) {
buttons[i]->setIcon(cells[i]->icon);
}
}
if (gameOver || cells[index]->revealed) {
return;
}
if (!cells[index]->revealed) {
cells[index]->revealed = true;
cells[index]->updateIcon();
buttons[index]->setIcon(cells[index]->icon);
if (cells[index]->mine) {
revealAllMines();
QMessageBox::information(this, "Game Over", "You lose!");
gameOver = true;
return;
} else {
score++;
updateScore();
checkWinCondition();
if (gameOver) {
return;
}
if (!cells[index]->flag) {
cells[index]->flag = true;
cells[index]->updateIcon();
buttons[index]->setIcon(cells[index]->icon);
}
else{
cells[index]->flag = false;
cells[index]->updateIcon();
buttons[index]->setIcon(cells[index]->icon);
}