diff options
author | daniel-Jones <daniel@danieljon.es> | 2018-09-05 15:31:51 +0930 |
---|---|---|
committer | daniel-Jones <daniel@danieljon.es> | 2018-09-05 15:31:51 +0930 |
commit | 63296bcb085a42ce0ef04738899f720b3469b1ce (patch) | |
tree | 5f21b5972f459c7d2d6268b5b94c795cc9cbff57 | |
parent | 06aca0d336b4c6fc08f1b448c271ce8b4e5144e8 (diff) | |
download | qtminesweeper-63296bcb085a42ce0ef04738899f720b3469b1ce.tar.gz qtminesweeper-63296bcb085a42ce0ef04738899f720b3469b1ce.zip |
replaced qlist with std::vector, basic cell revealing
The revealing is bound to change, it isn't very nice. I also iterate over every cell to find the right gridx/y. This is inefficient, I need to map them somehow.
-rw-r--r-- | BUGS | 2 | ||||
-rw-r--r-- | cell.cpp | 5 | ||||
-rw-r--r-- | cell.h | 1 | ||||
-rw-r--r-- | constants.cpp | 2 | ||||
-rw-r--r-- | qtminesweeper.cpp | 40 | ||||
-rw-r--r-- | qtminesweeper.h | 5 |
6 files changed, 44 insertions, 11 deletions
@@ -1,4 +1,4 @@ -If GRIDOFFSET isn't 10 You can't get to some edges of the grid +If GRIDOFFSET isn't 10 you (sometimes) can't get to some edges of the grid (see qtminesweeper::keyPressEvent(..) in qtminesweeper.cpp) @@ -44,3 +44,8 @@ int cell::getflags() { return flags; } + +void cell::reveal() +{ + setflags((int)flags ^ (int)REVEALED); +} @@ -33,6 +33,7 @@ class cell : public square }; void flagcheck(); int getflags(); + void reveal(); private: enum flagtype flags; diff --git a/constants.cpp b/constants.cpp index 4e392f2..41d7a24 100644 --- a/constants.cpp +++ b/constants.cpp @@ -1,7 +1,7 @@ #include "constants.h" const int BORDEROFFSET = 10; -const int GRIDWIDTH = 600; +const int GRIDWIDTH = 200; const int GRIDHEIGHT = 600; const int SQUARESIZE = 20; const int NUMBEROFCELLS = ((GRIDWIDTH*GRIDHEIGHT)/SQUARESIZE)/SQUARESIZE; diff --git a/qtminesweeper.cpp b/qtminesweeper.cpp index 8be08c3..0108850 100644 --- a/qtminesweeper.cpp +++ b/qtminesweeper.cpp @@ -16,6 +16,7 @@ #include <stdio.h> #include <QApplication> +#include <math.h> #include "qtminesweeper.h" qtminesweeper::qtminesweeper() @@ -29,12 +30,13 @@ qtminesweeper::qtminesweeper() bluepen = QPen(Qt::blue); /* create cells */ + cells.reserve(NUMBEROFCELLS); for (int c = 0; c < NUMBEROFCELLS; c++) { int f; f = cell::MINE; cell tmp(f); - cells.append(tmp); + cells.push_back(tmp); } generatecellpos(); @@ -78,14 +80,16 @@ void qtminesweeper::drawcursor(QPainter *painter) void qtminesweeper::drawcells(QPainter *painter) { - for (int c = 0; c < cells.length(); c++) + std::vector<int>::size_type c; + QString f; + for (c = 0; c < cells.size(); c++) { - if (cells[c].getflags() & cell::REVEALED) - { - painter->drawText(cells[c].getrealx()+(SQUARESIZE/4), - cells[c].getrealy()+(SQUARESIZE/2+5), - "R"); - } + int tf = cells[c].getflags(); + f = ""; + if (tf & cell::REVEALED) f = "R"; + painter->drawText(cells[c].getrealx()+(SQUARESIZE/4), + cells[c].getrealy()+(SQUARESIZE/2+5), + f); } } @@ -122,6 +126,8 @@ void qtminesweeper::keyPressEvent(QKeyEvent *event) } break; case Qt::Key_Space: + revealcell(getcellindexfrompos(cursor.getgridx(), cursor.getgridy())); + break; default: break; @@ -146,3 +152,21 @@ void qtminesweeper::generatecellpos() x = 0; } } + +void qtminesweeper::revealcell(int index) +{ + cells[index].reveal(); +} +int qtminesweeper::getcellindexfrompos(int gridx, int gridy) +{ + std::vector<int>::size_type c; + for (c = 0; c < cells.size(); c++) + { + if (cells[c].getgridx() == gridx && + cells[c].getgridy() == gridy) + { + return c; + } + } + return -1; +} diff --git a/qtminesweeper.h b/qtminesweeper.h index 7c8d914..84aa29f 100644 --- a/qtminesweeper.h +++ b/qtminesweeper.h @@ -20,6 +20,7 @@ #include <QtGui> #include <QWidget> #include <QList> +#include <vector> #include "selector.h" #include "cell.h" #include "constants.h" @@ -42,9 +43,11 @@ class qtminesweeper : public QWidget void drawcursor(QPainter *painter); void drawcells(QPainter *painter); void generatecellpos(); + void revealcell(int index); + int getcellindexfrompos(int gridx, int gridy); selector cursor; - QList<cell> cells; + std::vector<cell> cells; protected: void paintEvent(QPaintEvent *event); |