|
@@ -16,8 +16,8 @@ void MineField::timerTick() {
|
|
|
|
|
|
|
|
auto start = std::chrono::system_clock::now();
|
|
auto start = std::chrono::system_clock::now();
|
|
|
|
|
|
|
|
- while((m_exploded == false) && (m_gameWon == false)) {
|
|
|
|
|
- std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
|
|
|
|
|
|
+ while(m_timerRunning) {
|
|
|
|
|
+ std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
auto now = std::chrono::system_clock::now();
|
|
auto now = std::chrono::system_clock::now();
|
|
|
const auto duration = now - start;
|
|
const auto duration = now - start;
|
|
|
std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
|
|
std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
|
|
@@ -25,7 +25,17 @@ void MineField::timerTick() {
|
|
|
timerSignal.emit(m_time);
|
|
timerSignal.emit(m_time);
|
|
|
start = std::chrono::system_clock::now();
|
|
start = std::chrono::system_clock::now();
|
|
|
}
|
|
}
|
|
|
- //I should use std::duration to represent the time instead
|
|
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void MineField::startTimer() {
|
|
|
|
|
+ m_time = 0;
|
|
|
|
|
+ m_timerRunning = true;
|
|
|
|
|
+ m_timerThread = std::thread(&MineField::timerTick, this);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void MineField::stopTimer() {
|
|
|
|
|
+ m_timerRunning = false;
|
|
|
|
|
+ m_timerThread.join();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void MineField::initBombs(int x, int y) {
|
|
void MineField::initBombs(int x, int y) {
|
|
@@ -44,16 +54,19 @@ void MineField::initBombs(int x, int y) {
|
|
|
--remainingMines;
|
|
--remainingMines;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ startTimer();
|
|
|
|
|
+
|
|
|
//init the timer to zero and start the timer thread
|
|
//init the timer to zero and start the timer thread
|
|
|
- m_time = 0;
|
|
|
|
|
- timerThread = std::thread(&MineField::timerTick, this);
|
|
|
|
|
- timerThread.detach(); //not sure if this is okay (better to call join() when I set the condition to stop the thread)
|
|
|
|
|
|
|
+ // m_time = 0;
|
|
|
|
|
+ //timerThread = std::thread(&MineField::timerTick, this);
|
|
|
|
|
+ //timerThread.detach(); //not sure if this is okay (better to call join() when I set the condition to stop the thread)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
bool MineField::openCell(int x, int y) {
|
|
bool MineField::openCell(int x, int y) {
|
|
|
if(isBomb(x, y)) {
|
|
if(isBomb(x, y)) {
|
|
|
m_exploded = true;
|
|
m_exploded = true;
|
|
|
gameOverSignal.emit();
|
|
gameOverSignal.emit();
|
|
|
|
|
+ stopTimer();
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -120,6 +133,7 @@ void MineField::setOpenCell(int x, int y) {
|
|
|
openCellSignal.emit(x, y);
|
|
openCellSignal.emit(x, y);
|
|
|
if((++m_openCells == (m_cols * m_rows - m_totalMines)) && (m_exploded == false)) {
|
|
if((++m_openCells == (m_cols * m_rows - m_totalMines)) && (m_exploded == false)) {
|
|
|
m_gameWon = true;
|
|
m_gameWon = true;
|
|
|
|
|
+ stopTimer();
|
|
|
gameWonSignal.emit();
|
|
gameWonSignal.emit();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|