-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTile.cpp
109 lines (95 loc) · 2.91 KB
/
Tile.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// Created by xcao2 on 11/29/2023.
//
#include "Tile.h"
#include "Toolbox.h"
#include "Bomb.h"
Tile::Tile(sf::Vector2f _position) : position(_position){
//default constructor
Toolbox &toolbox = Toolbox::getInstance();
sprite.setPosition(position.x, position.y);
currentState=HIDDEN;
sprite.setTexture(*toolbox.hidden);
}
//getters
sf::Vector2f Tile::getLocation() {
return position;
}
Tile::State Tile::getState() {
return currentState;
}
std::array<Tile*, 8>& Tile::getNeighbors() {
return neighbors;
}
//used to set normal tile state and texture (not including flags); recursion: setState->revealNeighbors->setState
//given when clicked on empty tile (no bomb neighbors), sets state of surrounding tiles
void Tile::setState(State _state) {
Toolbox& toolbox=Toolbox::getInstance();
currentState = _state;
if(mine==0){
sprite.setTexture(*toolbox.revealed);
revealNeighbors();
}
else{
sprite.setTexture(*toolbox.numbers[mine-1]);
}
}
//sets neighbors given neighbor: looping seems to work best as direct copy did not function
void Tile::setNeighbors(std::array<Tile*, 8> _neighbors) {
for(int i = 0; i < 8; i++){
neighbors[i]=_neighbors[i];
}
//if it's a mine, mine count ++
for(Tile* t : neighbors){
Bomb *derived=dynamic_cast<Bomb*>(t);
if(derived != nullptr) {
mine++;
}
}
}
//normal tile: when clicked Hidden, shows;
//if already revealed, shows neighbors (how certain editions of minesweeper does it)
void Tile::onClickLeft() {
if (currentState == HIDDEN) {
setState(REVEALED);
}
else if(currentState==REVEALED){
revealNeighbors();
}
}
//when clicked right: toggle between flagged and hidden
void Tile::onClickRight() {
Toolbox& toolbox=Toolbox::getInstance();
if (currentState == HIDDEN) {
currentState = FLAGGED;
sprite.setTexture(*toolbox.flagged);
} else if (currentState == FLAGGED) {
currentState = HIDDEN;
sprite.setTexture(*toolbox.hidden);
}
}
//draws on toolbox window
void Tile::draw() const {
Toolbox& toolbox=Toolbox::getInstance();
toolbox.window.draw(sprite);
}
//recursively reveals tile's neighbor and neighbor's neighbors (only if neighbor empty)
void Tile::revealNeighbors() {
for(Tile* neighbor : neighbors){
if(neighbor && neighbor->getState()==HIDDEN){
//if bomb, unfortunately player loses; not careful enough
Bomb *derived=dynamic_cast<Bomb*>(neighbor);
if(derived != nullptr && derived->getState()!=FLAGGED){
derived->setState(EXPLODED);
}
else{
if(neighbor->getState()!=REVEALED){
neighbor->setState(REVEALED);
if(neighbor->mine==0){
neighbor->revealNeighbors();
}
}
}
}
}
}