-
Notifications
You must be signed in to change notification settings - Fork 0
/
Field.cpp
291 lines (268 loc) · 7.2 KB
/
Field.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "Minesweeper.hpp"
using namespace sf;
Field::Field(int ax, int ay, int ar)
: x(static_cast<float>(ax))
, y(static_cast<float>(ay))
, s(static_cast<float>(ar))
{
float skala = s / 40;
Field_Condition = _Field_Condition::Covered;
Square.setPosition(x, y);
Square.setSize(Vector2f(s, s));
Square.setTexture(Tx_Field);
Square.setTextureRect(fieldTexture());
number_of_bombs.setFont(Arial);
number_of_bombs.setPosition(x + s / 4, y);
number_of_bombs.setCharacterSize(static_cast<unsigned int>(15 * s / 20));
number_of_bombs.setFillColor(Color(0, 0, 0));
}
void Field::draw(sf::RenderTarget &Window, sf::RenderStates states) const
{
Window.draw(Square);
if (Field_Condition == _Field_Condition::Revealed)
Window.draw(number_of_bombs);
}
short Field::setFlag()
{
short change = 0;
switch (Field_Condition)
{
case _Field_Condition::Covered:
Field_Condition = _Field_Condition::Flag;
break;
case _Field_Condition::Bomb:
Field_Condition = _Field_Condition::Marked_Bomb;
break;
case _Field_Condition::Flag:
Field_Condition = _Field_Condition::Covered;
break;
case _Field_Condition::Marked_Bomb:
Field_Condition = _Field_Condition::Bomb;
break;
default:
break;
}
if (Field_Condition == _Field_Condition::Flag || Field_Condition == _Field_Condition::Marked_Bomb)
change = -1;
else if (Field_Condition == _Field_Condition::Covered || Field_Condition == _Field_Condition::Bomb)
change = 1;
Square.setTextureRect(fieldTexture());
return change;
}
bool Field::reaveal(bool map_discovered)
{
if (map_discovered)
{
if (Field_Condition == _Field_Condition::Bomb || Field_Condition == _Field_Condition::Marked_Bomb)
Field_Condition = _Field_Condition::Revealed_Bomb;
if (Field_Condition == _Field_Condition::Covered)
Field_Condition = _Field_Condition::Revealed;
Square.setTextureRect(fieldTexture());
}
else if (Field_Condition == _Field_Condition::Bomb)
return false;
else if (Field_Condition == _Field_Condition::Covered)
{
Field_Condition = _Field_Condition::Revealed;
Square.setTextureRect(fieldTexture());
}
return true;
}
bool Field::setBomb()
{
if (Field_Condition != _Field_Condition::Bomb)
{
Field_Condition = _Field_Condition::Bomb;
return true;
}
return false;
}
bool Field::isBomb() const
{
if (Field_Condition == _Field_Condition::Bomb || Field_Condition == _Field_Condition::Marked_Bomb)
return true;
return false;
}
bool Field::isSolved() const
{
if (Field_Condition == _Field_Condition::Flag || Field_Condition == _Field_Condition::Covered)
return false;
return true;
}
bool Field::isEmpty(bool revealed) const
{
if (number_of_bombs.getString().getSize() == 0)
{
if (revealed && Field_Condition == _Field_Condition::Revealed)
return true;
else if (Field_Condition == _Field_Condition::Flag || Field_Condition == _Field_Condition::Marked_Bomb)
return false;
else if (!revealed && Field_Condition != _Field_Condition::Revealed && Field_Condition != _Field_Condition::Bomb)
return true;
}
return false;
}
void Field::setNumberOfBombs(int l)
{
std::string text = "";
if (l != 0 && Field_Condition != _Field_Condition::Bomb)
text = std::to_string(l);
number_of_bombs.setString(text);
switch (text[0])
{
case '1':
number_of_bombs.setFillColor(Color(50, 50, 255));
break;
case '2':
number_of_bombs.setFillColor(Color(0, 155, 0));
break;
case '3':
number_of_bombs.setFillColor(Color(180, 180, 0));
break;
case '4':
number_of_bombs.setFillColor(Color(255, 0, 0));
break;
default:
break;
}
}
sf::IntRect Field::fieldTexture()
{
IntRect texture_square;
texture_square.top = 0;
texture_square.height = texture_square.width = 32;
if (Field_Condition == _Field_Condition::Covered || Field_Condition == _Field_Condition::Bomb )
{
texture_square.left = 96;
Square.changeBackground(Color::White);
}
else if (Field_Condition == _Field_Condition::Revealed)
{
texture_square.left = 0;
Square.changeBackground(Square.Light_Grey);
}
else if (Field_Condition == _Field_Condition::Flag || Field_Condition == _Field_Condition::Marked_Bomb)
{
texture_square.left = 32;
Square.changeBackground(Color::White);
}
else if (Field_Condition == _Field_Condition::Revealed_Bomb)
{
texture_square.left = 64;
Square.changeBackground(Color::White);
}
return texture_square;
}
void Field::setNeighborhood(std::vector<int> _neighbors)
{
neighboring_fields = _neighbors;
}
std::vector<int> Field::getNeighborhood() const
{
return neighboring_fields;
}
// FIELD_SHAPE
Field_Shape::Field_Shape(float _size, float _spacing)
:Dark_Grey(197, 197, 197), Light_Grey(229, 229, 229)
{
Size.x = Size.y = _size;
spacing = _spacing;
_Shape.setPrimitiveType(sf::Quads);
_Shape.resize(48);
Background = Light_Grey;
setColor(Color::White);
setSize(Size, spacing);
}
void Field_Shape::setColor(sf::Color _New_Color)
{
Mix_Color = _New_Color;
Color Mixed_LG = Light_Grey * Mix_Color;
Color Mixed_DG = Dark_Grey * Mix_Color;
Color Mixed_BG = Background * Mix_Color;
for (int i = 0; i < 4; ++i)
_Shape[i].color = Mixed_DG;
for (int i = 4; i < 8; ++i)
_Shape[i].color = Mixed_LG;
for (int i = 8; i < 12; ++i)
_Shape[i].color = Mixed_DG;
for (int i = 12; i < 16; ++i)
_Shape[i].color = Mixed_BG;
for (int i = 16; i < 48; ++i)
_Shape[i].color = Mixed_DG;
}
void Field_Shape::setSize(sf::Vector2f _Size, float _spacing)
{
Size = _Size;
float move = 0;
float spacing = _spacing * (Size.y / 40.0F);
if (spacing < 1.0F)
spacing = 1.0F;
//Frame
for (int i = 0; i < 16; i += 4)
{
_Shape[i + 0].position = Vector2f(move, move);
_Shape[i + 1].position = Vector2f(Size.x - move, move);
_Shape[i + 2].position = Vector2f(Size.x - move, Size.y - move);
_Shape[i + 3].position = Vector2f(move, Size.y - move);
if ((i == 0 || i == 8) && spacing >= 2.0F)
move += spacing / 2;
else
move += spacing;
}
//Frame points
for (int i = 16; i < 32; i += 4)
{
float x = spacing / 2, mx = spacing / 2;
float y = spacing / 2, my = spacing / 2;
if (i >= 28 || (i >= 20 && i < 23))
{
x = Size.x - spacing / 2;
mx *= -1;
}
if (i >= 24)
{
y = Size.y - spacing / 2;
my *= -1;
}
_Shape[i + 0].position = Vector2f(x, y);
_Shape[i + 1].position = Vector2f(x + mx, y);
_Shape[i + 2].position = Vector2f(x + mx, y + my);
_Shape[i + 3].position = Vector2f(x, y + my);
}
for (int i = 32; i < 48; i += 4)
{
float x = spacing, mx = spacing / 2;
float y = spacing, my = spacing / 2;
if (i >= 44 || (i >= 36 && i < 39))
{
x = Size.x - spacing;
mx *= -1;
}
if (i >= 40)
{
y = Size.y - spacing;
my *= -1;
}
_Shape[i + 0].position = Vector2f(x, y);
_Shape[i + 1].position = Vector2f(x + mx, y);
_Shape[i + 2].position = Vector2f(x + mx, y + my);
_Shape[i + 3].position = Vector2f(x, y + my);
}
}
void Field_Shape::changeBackground(const sf::Color color)
{
Background = color;
setColor(Color::White);
}
void Field_Shape::updateTexCoords()
{
float left = static_cast<float>(TextureRect.left);
float right = left + TextureRect.width;
float top = static_cast<float>(TextureRect.top);
float bottom = top + TextureRect.height;
int i = 12;
_Shape[i + 0].texCoords = Vector2f(left, top);
_Shape[i + 1].texCoords = Vector2f(right, top);
_Shape[i + 2].texCoords = Vector2f(right, bottom);
_Shape[i + 3].texCoords = Vector2f(left, bottom);
}