Quellcode durchsuchen

fixing winning condition of the game

Bernardo Magri vor 10 Monaten
Ursprung
Commit
2a1d45f998
4 geänderte Dateien mit 17 neuen und 4 gelöschten Zeilen
  1. 1 1
      meson.build
  2. 14 2
      src/minefield.cpp
  3. 1 0
      src/minefield.hpp
  4. 1 1
      src/window.hpp

+ 1 - 1
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')
 

+ 14 - 2
src/minefield.cpp

@@ -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;

+ 1 - 0
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();

+ 1 - 1
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<Gdk::Texture> m_textureBomb;