Skip to content

Commit

Permalink
many fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DevAlone committed May 21, 2017
1 parent a6591d7 commit e90f120
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 16 deletions.
1 change: 0 additions & 1 deletion colorchoosebutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ ColorChooseButton::ColorChooseButton(QWidget* parent)

void ColorChooseButton::buttonClicked()
{
qDebug() << "button clicked";
color = QColorDialog::getColor(color);
setColor(color);
}
Expand Down
3 changes: 1 addition & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

#include <QtWidgets>
#include <ctime>

// 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));
Expand Down
23 changes: 18 additions & 5 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "ui_mainwindow.h"
#include "wongamedialog.h"

// TODO: добавить показ количества оставшихся мин
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
Expand All @@ -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()
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class MainWindow : public QMainWindow {
public slots:
void loseGame();
void wonGame();
void markedCellCountChanged();
private slots:
void on_actionNew_game_triggered();

Expand Down
25 changes: 20 additions & 5 deletions minesfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cell>(this->rows * this->cols);
// генерим мины в первых minesCount элементах массива
for (unsigned i = 0; i < minesCount; i++) {
Expand Down Expand Up @@ -73,7 +72,6 @@ MinesField::MinesField(unsigned rows, unsigned cols, unsigned char mp)
}

created = true;
qDebug() << "finish creating field";
}

MinesField::~MinesField()
Expand Down Expand Up @@ -172,6 +170,11 @@ bool MinesField::getCreated() const
return created;
}

unsigned MinesField::getMarkedCells() const
{
return markedCells;
}

void MinesField::tryToOpenCell(const Point& point)
{
// да, костыль, но перемещать игровую логику в отдельный поток дольше
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
4 changes: 4 additions & 0 deletions minesfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cell> cells;
unsigned minesCount = 0;
int minesLeft = 0;
unsigned markedCells = 0;
unsigned rows;
unsigned cols;
double minesPercents = 0;
Expand Down
3 changes: 1 addition & 2 deletions minesfieldwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
1 change: 0 additions & 1 deletion minesfieldwidgetupdaterthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,4 @@ void MinesFieldWidgetUpdaterThread::run()
minesFieldWidget->updatingMutex.unlock();
static int n = 1;
n++;
qDebug() << "thread finished" << n;
}

0 comments on commit e90f120

Please sign in to comment.