From 63296bcb085a42ce0ef04738899f720b3469b1ce Mon Sep 17 00:00:00 2001 From: daniel-Jones Date: Wed, 5 Sep 2018 15:31:51 +0930 Subject: 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. --- BUGS | 2 +- cell.cpp | 5 +++++ cell.h | 1 + constants.cpp | 2 +- qtminesweeper.cpp | 40 ++++++++++++++++++++++++++++++++-------- qtminesweeper.h | 5 ++++- 6 files changed, 44 insertions(+), 11 deletions(-) diff --git a/BUGS b/BUGS index ba206ea..61bdacd 100644 --- a/BUGS +++ b/BUGS @@ -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) diff --git a/cell.cpp b/cell.cpp index d32fa6e..cfedeb6 100644 --- a/cell.cpp +++ b/cell.cpp @@ -44,3 +44,8 @@ int cell::getflags() { return flags; } + +void cell::reveal() +{ + setflags((int)flags ^ (int)REVEALED); +} diff --git a/cell.h b/cell.h index 8ede3fa..70b37d3 100644 --- a/cell.h +++ b/cell.h @@ -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 #include +#include #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::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::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 #include #include +#include #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 cells; + std::vector cells; protected: void paintEvent(QPaintEvent *event); -- cgit v1.2.3