summaryrefslogtreecommitdiff
path: root/qtminesweeper.cpp
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 /qtminesweeper.cpp
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.
Diffstat (limited to 'qtminesweeper.cpp')
-rw-r--r--qtminesweeper.cpp40
1 files changed, 32 insertions, 8 deletions
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;
+}