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', project('minesweeper', 'cpp',
version : '0.1', version : '0.1',
default_options : ['warning_level=3']) default_options : ['warning_level=3', 'cpp_std=c++20'])
gnome = import('gnome') gnome = import('gnome')

View File

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

View File

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

View File

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