diff --git a/colorchoosebutton.cpp b/colorchoosebutton.cpp index a116180..199c823 100644 --- a/colorchoosebutton.cpp +++ b/colorchoosebutton.cpp @@ -12,7 +12,6 @@ ColorChooseButton::ColorChooseButton(QWidget* parent) void ColorChooseButton::buttonClicked() { - qDebug() << "button clicked"; color = QColorDialog::getColor(color); setColor(color); } diff --git a/main.cpp b/main.cpp index 337ecee..647b7c2 100644 --- a/main.cpp +++ b/main.cpp @@ -5,8 +5,7 @@ #include #include - -// add showing mines left +// TODO: add hints, for example it will open cells, that marked as question int main(int argc, char* argv[]) { srand(time(0)); diff --git a/mainwindow.cpp b/mainwindow.cpp index 0d4b922..9f6430e 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -4,7 +4,6 @@ #include "ui_mainwindow.h" #include "wongamedialog.h" -// TODO: добавить показ количества оставшихся мин MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) , ui(new Ui::MainWindow) @@ -17,8 +16,15 @@ MainWindow::MainWindow(QWidget* parent) minesFieldWidget->setCellSize(QSize(25, 25)); minesFieldWidget->setBorderSize(QSize(0, 0)); - connect(minesField.get(), SIGNAL(userLost()), this, SLOT(loseGame())); - connect(minesField.get(), SIGNAL(userWon()), this, SLOT(wonGame())); + connect(minesField.get(), SIGNAL(userLost()), + this, SLOT(loseGame())); + connect(minesField.get(), SIGNAL(userWon()), + this, SLOT(wonGame())); + + connect(minesField.get(), SIGNAL(markedCellsCountChanged()), + this, SLOT(markedCellCountChanged())); + + markedCellCountChanged(); } MainWindow::~MainWindow() @@ -54,8 +60,6 @@ bool MainWindow::event(QEvent* event) minesFieldWidget->verticalScrollPosChanged(1); } - qDebug() << cursorPos << "?" << cell.x() << ":" << cell.y(); - auto stepSize = minesFieldWidget->getCellSize() + minesFieldWidget->getBorderSize(); QPoint cellPos = QPoint(cell.x() * stepSize.width() + stepSize.width() / 2, cell.y() * stepSize.height() + stepSize.height() / 2); @@ -120,6 +124,11 @@ void MainWindow::wonGame() } } +void MainWindow::markedCellCountChanged() +{ + setWindowTitle(QString("mines left: ") + QString::number(minesField->getMinesCount() - minesField->getMarkedCells())); +} + //bool MainWindow::event(QEvent* event) //{ // return true; @@ -146,6 +155,10 @@ void MainWindow::on_actionNew_game_triggered() connect(minesField.get(), SIGNAL(userLost()), this, SLOT(loseGame())); connect(minesField.get(), SIGNAL(userWon()), this, SLOT(wonGame())); + connect(minesField.get(), SIGNAL(markedCellsCountChanged()), + this, SLOT(markedCellCountChanged())); + + markedCellCountChanged(); minesFieldWidget->setCellSize(data.cellSize); minesFieldWidget->setBorderSize(data.borderSize); diff --git a/mainwindow.h b/mainwindow.h index 6c99524..1e187b2 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -23,6 +23,7 @@ class MainWindow : public QMainWindow { public slots: void loseGame(); void wonGame(); + void markedCellCountChanged(); private slots: void on_actionNew_game_triggered(); diff --git a/minesfield.cpp b/minesfield.cpp index 147d32d..dd1176f 100644 --- a/minesfield.cpp +++ b/minesfield.cpp @@ -19,7 +19,6 @@ MinesField::MinesField(unsigned rows, unsigned cols, unsigned char mp) minesLeft = minesCount = rows * cols * minesPercents; srand(time(0)); - qDebug() << "start creating field"; cells = std::vector(this->rows * this->cols); // генерим мины в первых minesCount элементах массива for (unsigned i = 0; i < minesCount; i++) { @@ -73,7 +72,6 @@ MinesField::MinesField(unsigned rows, unsigned cols, unsigned char mp) } created = true; - qDebug() << "finish creating field"; } MinesField::~MinesField() @@ -172,6 +170,11 @@ bool MinesField::getCreated() const return created; } +unsigned MinesField::getMarkedCells() const +{ + return markedCells; +} + void MinesField::tryToOpenCell(const Point& point) { // да, костыль, но перемещать игровую логику в отдельный поток дольше @@ -254,11 +257,18 @@ void MinesField::markCell(const Point& point, Cell::CellState markAs) switch (markAs) { case Cell::CellState::MarkedAsBomb: cell.setCellState(Cell::CellState::MarkedAsBomb); - minesLeft--; + + if (cell.isMine()) + minesLeft--; + markedCells++; + emit markedCellsCountChanged(); break; case Cell::CellState::MarkedAsQuestion: cell.setCellState(Cell::CellState::MarkedAsQuestion); - minesLeft++; + if (cell.isMine()) + minesLeft++; + markedCells--; + emit markedCellsCountChanged(); break; default: break; @@ -286,8 +296,13 @@ void MinesField::unmarkCell(const Point& point) if (!(cell.cellState() == Cell::CellState::MarkedAsBomb || cell.cellState() == Cell::CellState::MarkedAsQuestion)) return; + if (cell.cellState() == Cell::CellState::MarkedAsBomb) { + minesLeft++; + markedCells--; + emit markedCellsCountChanged(); + } + cell.setCellState(Cell::CellState::Closed); - minesLeft++; checkForWin(); } diff --git a/minesfield.h b/minesfield.h index 46e7196..ef40fd1 100644 --- a/minesfield.h +++ b/minesfield.h @@ -47,15 +47,19 @@ class MinesField : public QObject { bool getCreated() const; + unsigned getMarkedCells() const; + signals: void userLost(); void userWon(); + void markedCellsCountChanged(); public slots: private: std::vector cells; unsigned minesCount = 0; int minesLeft = 0; + unsigned markedCells = 0; unsigned rows; unsigned cols; double minesPercents = 0; diff --git a/minesfieldwidget.cpp b/minesfieldwidget.cpp index 62dfc7b..bc6500b 100644 --- a/minesfieldwidget.cpp +++ b/minesfieldwidget.cpp @@ -195,11 +195,10 @@ void MinesFieldWidget::mouseMoveEvent(QMouseEvent* event) void MinesFieldWidget::updatingThreadFinished() { - qDebug() << "updating thread finished"; update(); if (isUpdatingQueued) { isUpdatingQueued = false; - thread->start(QThread::HighestPriority); //TODO: do + thread->start(QThread::HighestPriority); } } diff --git a/minesfieldwidgetupdaterthread.cpp b/minesfieldwidgetupdaterthread.cpp index 9ee31b9..bac5a72 100644 --- a/minesfieldwidgetupdaterthread.cpp +++ b/minesfieldwidgetupdaterthread.cpp @@ -63,5 +63,4 @@ void MinesFieldWidgetUpdaterThread::run() minesFieldWidget->updatingMutex.unlock(); static int n = 1; n++; - qDebug() << "thread finished" << n; }