diff options
author | daniel-Jones <daniel@danieljon.es> | 2018-09-06 22:38:55 +0930 |
---|---|---|
committer | daniel-Jones <daniel@danieljon.es> | 2018-09-06 22:38:55 +0930 |
commit | d844919907573cfd7dd3f602026e60e6d4ff27d2 (patch) | |
tree | 3ff98f931ea17c9d6956da2a31394cd831019840 | |
parent | 63296bcb085a42ce0ef04738899f720b3469b1ce (diff) | |
download | qtminesweeper-master.tar.gz qtminesweeper-master.zip |
added conversions for 1d->2d mapping. auto-reveals all cells now. generates random mines.
-rw-r--r-- | cell.cpp | 5 | ||||
-rw-r--r-- | cell.h | 1 | ||||
-rw-r--r-- | constants.cpp | 3 | ||||
-rw-r--r-- | constants.h | 1 | ||||
-rw-r--r-- | qtminesweeper.cpp | 51 | ||||
-rw-r--r-- | qtminesweeper.h | 3 |
6 files changed, 51 insertions, 13 deletions
@@ -49,3 +49,8 @@ void cell::reveal() { setflags((int)flags ^ (int)REVEALED); } + +void cell::makemine() +{ + setflags((int)flags | (int)MINE); +} @@ -34,6 +34,7 @@ class cell : public square void flagcheck(); int getflags(); void reveal(); + void makemine(); private: enum flagtype flags; diff --git a/constants.cpp b/constants.cpp index 41d7a24..4f012ba 100644 --- a/constants.cpp +++ b/constants.cpp @@ -2,6 +2,7 @@ const int BORDEROFFSET = 10; const int GRIDWIDTH = 200; -const int GRIDHEIGHT = 600; +const int GRIDHEIGHT = 200; const int SQUARESIZE = 20; const int NUMBEROFCELLS = ((GRIDWIDTH*GRIDHEIGHT)/SQUARESIZE)/SQUARESIZE; +const int NUMBEROFMINES = NUMBEROFCELLS/SQUARESIZE*1.5; diff --git a/constants.h b/constants.h index b1201c0..34954d9 100644 --- a/constants.h +++ b/constants.h @@ -22,5 +22,6 @@ extern const int GRIDWIDTH; extern const int GRIDHEIGHT; extern const int SQUARESIZE; extern const int NUMBEROFCELLS; +extern const int NUMBEROFMINES; #endif diff --git a/qtminesweeper.cpp b/qtminesweeper.cpp index 0108850..4998f01 100644 --- a/qtminesweeper.cpp +++ b/qtminesweeper.cpp @@ -15,13 +15,16 @@ */ #include <stdio.h> +#include <stdlib.h> +#include <time.h> #include <QApplication> -#include <math.h> #include "qtminesweeper.h" qtminesweeper::qtminesweeper() { this->setStyleSheet("background-color: black;"); + /* seed rand */ + srand(time(NULL)); /* setup function */ blackpen = QPen(Qt::black); whitepen = QPen(Qt::white); @@ -33,12 +36,11 @@ qtminesweeper::qtminesweeper() cells.reserve(NUMBEROFCELLS); for (int c = 0; c < NUMBEROFCELLS; c++) { - int f; - f = cell::MINE; - cell tmp(f); - cells.push_back(tmp); + cells.push_back(generatecell()); } generatecellpos(); + generatemines(); + revealallcells(); cursor.movetogridpos(0, 0); } @@ -86,7 +88,7 @@ void qtminesweeper::drawcells(QPainter *painter) { int tf = cells[c].getflags(); f = ""; - if (tf & cell::REVEALED) f = "R"; + if (tf & cell::REVEALED && tf & cell::MINE) f = "M"; painter->drawText(cells[c].getrealx()+(SQUARESIZE/4), cells[c].getrealy()+(SQUARESIZE/2+5), f); @@ -157,16 +159,41 @@ 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++) + return gridx + GRIDWIDTH/SQUARESIZE*gridy; +} + +cell qtminesweeper::generatecell() +{ + int flags = 0; + cell tmp(flags); + return tmp; +} + +void qtminesweeper::generatemines() +{ + // rand() %NUMBEROFCELLS + int c, randcell; + while (c < NUMBEROFMINES) { - if (cells[c].getgridx() == gridx && - cells[c].getgridy() == gridy) + randcell = rand() %NUMBEROFCELLS; + if (cells[randcell].getflags() & cell::MINE) + break; + else { - return c; + cells[randcell].makemine(); + c++; } } - return -1; +} + +void qtminesweeper::revealallcells() +{ + unsigned int c = 0; + for(c = 0; c < cells.size(); c++) + { + cells[c].reveal(); + } } diff --git a/qtminesweeper.h b/qtminesweeper.h index 84aa29f..16b5200 100644 --- a/qtminesweeper.h +++ b/qtminesweeper.h @@ -45,6 +45,9 @@ class qtminesweeper : public QWidget void generatecellpos(); void revealcell(int index); int getcellindexfrompos(int gridx, int gridy); + cell generatecell(); + void generatemines(); + void revealallcells(); selector cursor; std::vector<cell> cells; |