fixing winning condition of the game

This commit is contained in:
Bernardo Magri
2025-03-12 17:07:10 +00:00
parent 4ff86eb027
commit 2a1d45f998
4 changed files with 17 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
project('minesweeper', 'cpp',
version : '0.1',
default_options : ['warning_level=3'])
default_options : ['warning_level=3', 'cpp_std=c++20'])
gnome = import('gnome')

View File

@@ -1,4 +1,5 @@
#include "minefield.hpp"
#include <iostream>
MineField::MineField(int cols, int rows, int mines): m_rows(rows),
m_cols(cols),
@@ -13,7 +14,9 @@ MineField::MineField(int cols, int rows, int mines): m_rows(rows),
}
MineField::~MineField() {
// stopTimer();
if(m_timerRunning) {
stopTimer();
}
}
void MineField::timerTick() {
@@ -39,6 +42,7 @@ void MineField::startTimer() {
void MineField::stopTimer() {
m_timerRunning = false;
if(m_timerThread.joinable()) {
m_timerThread.join();
}
@@ -135,11 +139,18 @@ int MineField::bombsNearby(int x, int y) {
void MineField::setOpenCell(int x, int y) {
m_cells.at(x + y * m_rows)->isCleared = true;
openCellSignal.emit(x, y);
if((++m_openCells == (m_cols * m_rows - m_totalMines)) && (m_exploded == false)) {
++m_openCells;
checkGameWon();
}
void MineField::checkGameWon() {
if((m_openCells == (m_cols * m_rows - m_totalMines)) && (m_exploded == false) && (m_remainingFlags == 0)) {
m_gameWon = true;
stopTimer();
gameWonSignal.emit();
}
std::cout << "Open cells: " << m_openCells << "\n" << "Remaining Flags: " << m_remainingFlags << "\n";
}
bool MineField::toggleFlag(int x, int y) {
@@ -153,6 +164,7 @@ bool MineField::toggleFlag(int x, int y) {
m_cells.at(x + y * m_rows)->isFlagged = true;
--m_remainingFlags;
remainingFlagsSignal.emit(m_remainingFlags);
checkGameWon();
return true;
}
return false;

View File

@@ -33,6 +33,7 @@ class MineField {
void computeBombsNearby(int x, int y);
void openNeighboorhood(int x, int y);
void setOpenCell(int x, int y);
void checkGameWon();
void timerTick();
void startTimer();
void stopTimer();

View File

@@ -24,7 +24,7 @@ class MainWindow : public Gtk::Window
Gtk::Button optionButton;
Gtk::Label flagLabel;
Gtk::Label clockLabel;
MineField field {16, 16, 40};
MineField field {16, 16, 1};
int m_elapsedTime;
bool newGame;
std::shared_ptr<Gdk::Texture> m_textureBomb;