From 2a1d45f9986ce9e948b792a71b28b2bca344909c Mon Sep 17 00:00:00 2001 From: Bernardo Magri Date: Wed, 12 Mar 2025 17:07:10 +0000 Subject: [PATCH] fixing winning condition of the game --- meson.build | 2 +- src/minefield.cpp | 16 ++++++++++++++-- src/minefield.hpp | 1 + src/window.hpp | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/meson.build b/meson.build index 95347bf..e0f43b7 100644 --- a/meson.build +++ b/meson.build @@ -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') diff --git a/src/minefield.cpp b/src/minefield.cpp index c0bb24b..14725bb 100644 --- a/src/minefield.cpp +++ b/src/minefield.cpp @@ -1,4 +1,5 @@ #include "minefield.hpp" +#include 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; diff --git a/src/minefield.hpp b/src/minefield.hpp index 7f6b44b..45d8384 100644 --- a/src/minefield.hpp +++ b/src/minefield.hpp @@ -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(); diff --git a/src/window.hpp b/src/window.hpp index ee6f112..6634384 100644 --- a/src/window.hpp +++ b/src/window.hpp @@ -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 m_textureBomb;