summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordaniel-Jones <daniel@danieljon.es>2018-09-05 15:31:51 +0930
committerdaniel-Jones <daniel@danieljon.es>2018-09-05 15:31:51 +0930
commit63296bcb085a42ce0ef04738899f720b3469b1ce (patch)
tree5f21b5972f459c7d2d6268b5b94c795cc9cbff57
parent06aca0d336b4c6fc08f1b448c271ce8b4e5144e8 (diff)
downloadqtminesweeper-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--BUGS2
-rw-r--r--cell.cpp5
-rw-r--r--cell.h1
-rw-r--r--constants.cpp2
-rw-r--r--qtminesweeper.cpp40
-rw-r--r--qtminesweeper.h5
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 <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);