-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcell.cpp
More file actions
70 lines (59 loc) · 1.93 KB
/
cell.cpp
File metadata and controls
70 lines (59 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "cell.h"
// the constructor
Cell::Cell(QWidget* parent)
: QPushButton(parent), isMine(false), isRevealed(false), adjacentMines(0), isFlagged(false) {
setFixedSize(40, 40); // setting the size of each cell
}
// sets true or false depending on whether a cell has a mine or not
void Cell::setMine(bool mine) {
isMine = mine;
}
// returns true or false if a cell does have a mine
bool Cell::getMine() const {
return isMine;
}
// reveals a cell that the player clicked on and updates in accordingly
void Cell::setRevealed(bool revealed) {
isRevealed = revealed;
if (isRevealed) {
setEnabled(false);
if (getMine()) {
setText("M"); // M is mine
} else if (adjacentMines > 0) {
setText(QString::number(adjacentMines)); // shows the number of adjacent mines
} else {
setText(""); // otherwise, it remains empty (there are also no mines around)
}
}
}
// returns the status of a revealed cell (if it is revealed or not)
bool Cell::getRevealed() const {
return isRevealed;
}
// sets the number of adjacent mines to this cell
void Cell::setAdjacentMines(int count) {
adjacentMines = count;
}
// returns the number of adjacent mines
int Cell::getAdjacentMines() const {
return adjacentMines;
}
// toggles the flag state and updates the display
void Cell::setFlagged(bool value) {
isFlagged = value;
setText(isFlagged ? "?" : ""); // ? is the flagging symbol
}
// returns the status of the cell (if it is flagged or not)
bool Cell::getFlagged() const {
return isFlagged;
}
// handles the mouse clicks (the left and right clicks for revealing or flagging a cell)
void Cell::mousePressEvent(QMouseEvent *event) {
if (event->button() == Qt::RightButton) {
if (!getRevealed()) {
emit rightClicked();
}
} else {
QPushButton::mousePressEvent(event); // Normal left-click
}
}