-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTile.cpp
More file actions
146 lines (136 loc) · 3.58 KB
/
Tile.cpp
File metadata and controls
146 lines (136 loc) · 3.58 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "Tile.h"
Tile::Tile() {
this->coordinates.x = 0;
this->coordinates.y = 0;
this->image = sf::Sprite(TextureManager::GetTexture("tile_hidden"));
this->image.setPosition(coordinates.x, coordinates.y);
this->revealedTile = sf::Sprite(TextureManager::GetTexture("tile_revealed"));
this->revealedTile.setPosition(coordinates.x, coordinates.y);
this->mineNumber = 0;
this->isFlag = false;
this->canClick = true;
this->isRevealed = false;
}
Tile::Tile(sf::Vector2f xyCoordinates, const char* texture, int mineNumber) {
this->coordinates = xyCoordinates;
this->image = sf::Sprite(TextureManager::GetTexture(texture));
this->image.setPosition(xyCoordinates.x, xyCoordinates.y);
this->revealedTile = sf::Sprite(TextureManager::GetTexture("tile_revealed"));
this->revealedTile.setPosition(xyCoordinates.x, xyCoordinates.y);
//Checking if the tile is a mine to initialize isMine
this->mineNumber = mineNumber;
if (mineNumber == 9) {
this->isMine = true;
}
else {
this->isMine = false;
}
this->isFlag = false;
this->canClick = true;
this->isRevealed = false;
}
void Tile::SetImage(const char* texture) {
this->image.setTexture(TextureManager::GetTexture(texture));
this->image.setPosition(coordinates);
}
void Tile::SetRevealedTile(const char* texture) {
this->revealedTile.setTexture(TextureManager::GetTexture(texture));
this->revealedTile.setPosition(coordinates);
}
void Tile::SetDebugFlag(const char* texture) {
this->debugFlag.setTexture(TextureManager::GetTexture(texture));
this->debugFlag.setPosition(coordinates);
}
sf::Sprite& Tile::GetImage(){
return this->image;
}
sf::Sprite& Tile::GetRevealedTile() {
return this->revealedTile;
}
sf::Sprite& Tile::GetDebugFlag() {
return this->debugFlag;
}
void Tile::newNeighbor(Tile* neighborTile){
this->neighborTiles.push_back(neighborTile);
}
bool Tile::RevealTile() {
bool clickedMine = false;
if (this->canClick) {
if (this->isRevealed == false) {
this->isRevealed = true;
this->canClick = false;
switch (this->mineNumber) {
case 0:
SetImage("tile_revealed");
clickedMine = false;
//Recursive if the tile is empty
for (int i = 0; i < neighborTiles.size(); i++) {
this->neighborTiles[i]->RevealTile();
}
break;
case 1:
SetImage("number_1");
clickedMine = false;
break;
case 2:
SetImage("number_2");
clickedMine = false;
break;
case 3:
SetImage("number_3");
clickedMine = false;
break;
case 4:
SetImage("number_4");
clickedMine = false;
break;
case 5:
SetImage("number_5");
clickedMine = false;
break;
case 6:
SetImage("number_6");
clickedMine = false;
break;
case 7:
SetImage("number_7");
clickedMine = false;
break;
case 8:
SetImage("number_8");
clickedMine = false;
break;
case 9:
SetImage("mine");
clickedMine = true;
break;
}
return clickedMine;
}
}
return clickedMine;
}
void Tile::PlaceFlag() {
if (this->isFlag == false && this->canClick == true) {
if (this->isRevealed == false) {
this->canClick = false;
this->isFlag = true;
SetRevealedTile("tile_hidden");
SetImage("flag");
}
else {
cout << "Cannot place flag here" << endl;
}
}
else {
if (this->isRevealed == false) {
this->canClick = true;
this->isFlag = false;
SetRevealedTile("tile_revealed");
SetImage("tile_hidden");
}
else {
cout << "Cannot place flag here" << endl;
}
}
}