-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTile.cpp
More file actions
67 lines (54 loc) · 1.62 KB
/
Copy pathTile.cpp
File metadata and controls
67 lines (54 loc) · 1.62 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
#include "Tile.h"
#include "TextureManager.h"
Tile::Tile()
: mine(false)
, adjacentMines(0)
, state(Hidden)
{}
void Tile::init(int gx, int gy, int sz, float yo) {
x = gx; y = gy; size = sz; yOffset = yo;
mine = false;
adjacentMines = 0;
state = Hidden;
sprite.setTexture(TextureManager::get("tile_hidden.png"));
sprite.setPosition(x * size, y * size + yOffset);
}
void Tile::setMine(bool m) { mine = m; }
bool Tile::isMine() const { return mine; }
int Tile::getAdjacentMines() const { return adjacentMines; }
sf::Vector2f Tile::getPosition() const { return sprite.getPosition(); }
void Tile::addNeighbor(Tile* t) {
neighbors.push_back(t);
}
void Tile::reveal() {
if (state != Hidden) return;
state = Revealed;
if (mine) {
// only reveal this bomb
sprite.setTexture(TextureManager::get("mine.png"));
return;
}
// count neighbors
adjacentMines = 0;
for (auto* n : neighbors)
if (n->isMine())
++adjacentMines;
// grey revealed background
sprite.setTexture(TextureManager::get("tile_revealed.png"));
// **no** recursive calls here anymore
}
void Tile::toggleFlag() {
if (state == Hidden) {
state = Flagged;
sprite.setTexture(TextureManager::get("flag.png"));
}
else if (state == Flagged) {
state = Hidden;
sprite.setTexture(TextureManager::get("tile_hidden.png"));
}
}
void Tile::draw(sf::RenderWindow& window) const {
window.draw(sprite);
}
bool Tile::isRevealed() const { return state == Revealed; }
bool Tile::isFlagged() const { return state == Flagged; }