-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
351 lines (278 loc) · 9.84 KB
/
main.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <iostream>
#include <sstream>
#include <SFML/Graphics.hpp>
#include <cstdlib>
#include <ctime>
#include "button.hpp"
using namespace std;
class Board{
private:
int scalar;
int height;
int width;
float bomb_prob;
sf::Texture base;
sf::Texture bomb;
sf::Texture flagged;
sf::Texture number_textures[9];
sf::RenderWindow& window;
sf::Sprite grid[32][32];
bool bombs[32][32];
bool flags[32][32];
int suround[32][32];
int uncovered[32][32];
float ran;
public:
bool hasBoard = false;
// constructor
Board(int s, int w, int h, float p, sf::RenderWindow &win) : window(win){
scalar = s;
height = h;
width = w;
bomb_prob = p;
base.loadFromFile("sprites/base.png", sf::IntRect(0, 0, 16, 16));
bomb.loadFromFile("sprites/bomb.png", sf::IntRect(0, 0, 16, 16));
flagged.loadFromFile("sprites/flag.png", sf::IntRect(0, 0, 16, 16));
for(int i = 0; i < 9; i++){
number_textures[i].loadFromFile("sprites/"+ to_string(i) + ".png", sf::IntRect(0, 0, 16, 16));
}
}
//create a game board
void generateBoard() {
for(int i = 0; i < width; i++){
for(int j = 0; j < height; j++){
sf::Sprite sprite;
sprite.setTexture(base);
sprite.setOrigin(-i*16, -j*16);
sprite.setScale(scalar, scalar);
bombs[i][j] = false;
if (bomb_prob > ((float)rand())/RAND_MAX){
bombs[i][j] = true;
}
flags[i][j] = false;
uncovered[i][j] = false;
grid[i][j] = sprite;
suround[i][j] = 0;
}
}
for(int i = 0; i < width; i++){
for(int j = 0; j < height; j++){
for(int k = -1; k <= 1; k++){
for(int l = -1; l <= 1; l++){
if(i+k >= 0 and j+l >= 0){
if(i+k < width and j+l < width){
if(bombs[i+k][j+l]){
suround[i][j] ++;
//grid[i][j].setTexture(number_textures[suround[i][j]]);
}
}
}
}
}
}
}
}
// draw all tiles on board
void draw() {
for(int i = 0; i < width; i++){
for(int j = 0; j < height; j++){
window.draw(grid[i][j]);
}
}
}
// draw board completely uncovered
void drawExposed() {
for(int i = 0; i < width; i++){
for(int j = 0; j < height; j++){
grid[i][j].setTexture(number_textures[suround[i][j]]);
window.draw(grid[i][j]);
}
}
}
// uncover tile
int uncover(int x, int y){
if(uncovered[x][y]){
return -1;
}
if(!flags[x][y] && !uncovered[x][y]){
uncovered[x][y] = true;
grid[x][y].setTexture(number_textures[suround[x][y]]);
if(bombs[x][y]){
grid[x][y].setTexture(bomb);
return 1;
}
if(suround[x][y] == 0){
for(int i = -1; i <= 1; i ++){
for(int j = -1; j <= 1; j ++){
if(x+i < width && x+i >= 0){
if(y+j < height && y+j >= 0){
uncover(x + i, y+j);
}
}
}
}
}
return 0;
}
return -1;
}
// place flag on square clicked
int flag(int x, int y){
if(!uncovered[x][y]){
flags[x][y] = !flags[x][y];
if (flags[x][y]){
grid[x][y].setTexture(flagged);
}
else{
grid[x][y].setTexture(base);
}
return 0;
}
return -1;
}
// generate a board where the clicked square has no bombs around it
void generateZeroed(int x, int y){
generateBoard();
//drawExposed();
if(suround[x][y] != 0){
generateZeroed(x, y);
}
else{
hasBoard = true;
}
return;
}
// check if the board is in a winning state
bool checkwin(){
for(int i = 0; i < width; i++){
for(int j = 0; j < height; j++){
if(bombs[i][j] != flags[i][j]){
return false;
}
}
}
return true;
}
};
int main()
{
// create variables for window creation
srand(time(0));
int scalar = 2;
int height = 32; // can't change this for now I need to figure out how to initialize lists in classes
int width = 32; //
float bomb_prob = 0.15;
sf::Vector2i mouse_position;
// create window
sf::RenderWindow window(sf::VideoMode(scalar * 16 * width, scalar * 16 * height), "minesweeper", sf::Style::Close);
// create darken effect for endgame
sf::Texture blur;
blur.loadFromFile("sprites/translucent.png", sf::IntRect(0, 0, 16, 16));
sf::Sprite fullscreen;
fullscreen.setTexture(blur);
fullscreen.setScale(scalar * 16 * width, scalar * 16 * height);
//create endscreen loss message
sf::Texture won;
sf::Texture lost;
lost.loadFromFile("sprites/lose.png");
won.loadFromFile("sprites/won.png");
sf::Sprite endscreen;
endscreen.setScale(scalar * 3, scalar * 3);
endscreen.setOrigin((-8 * width) / 3 + 16, (-8 * height) / 3 + 32);
// create restart button
Button reset(window);
reset.setScale(scalar * 2, scalar * 2);
reset.setOrigin(-4 * width + 32, -4 * height);
// set textures for reset button
sf::Texture resetBase;
sf::Texture resetHover;
sf::Texture resetClicked;
resetBase.loadFromFile("sprites/restartBase.png");
resetHover.loadFromFile("sprites/restartHover.png");
resetClicked.loadFromFile("sprites/restartClicked.png");
reset.setBaseTexture(resetBase);
reset.setHoverTexture(resetHover);
reset.setClickedTexture(resetClicked);
// set icon for application
sf::Image icon;
icon.loadFromFile("sprites/bomb.png");
// create board
Board board = Board(scalar, width, height, bomb_prob, window);
board.generateBoard();
// initialize variables for use in windows
int x_index;
int y_index;
bool unclicked = false;
bool haslost = false;
bool haswon = false;
while (window.isOpen())
{
//if close window button clicked close window
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
mouse_position = sf::Mouse::getPosition(window);
haswon = board.checkwin();
if(!(haswon || haslost)){
// if click event get board index
if(event.type == sf::Event::MouseButtonPressed){
// only continue if the mouse is on the 'rising edge' of being clicked
if(unclicked)
{
unclicked = false;
// get tile of the board the mouse is above
x_index = (mouse_position.x - (mouse_position.x % (16 * scalar))) / (16 * scalar);
y_index = (mouse_position.y - (mouse_position.y % (16 * scalar))) / (16 * scalar);
if(!board.hasBoard){
board.generateZeroed(x_index, y_index);
}
//uncover board tile
if (event.mouseButton.button == sf::Mouse::Left)
{
if(board.uncover(x_index, y_index) == 1){
haslost = true;
}
}
// flag board tile
else{
board.flag(x_index, y_index);
}
}
}
else if(!unclicked)
{
unclicked = true;
}
}
// clear window
window.clear(sf::Color::White);
// draw tiles
board.draw();
// if the game has been won or lost show end screen
if(haswon || haslost) {
if(haslost){
endscreen.setTexture(lost);
}
else{
endscreen.setTexture(won);
}
if(reset.getfalling()){
haslost = false;
haslost = false;
board.generateBoard();
board.hasBoard = false;
}
window.draw(fullscreen);
reset.updatetexture();
window.draw(reset);
window.draw(endscreen);
// reset the board
}
//show display
window.display();
}
return EXIT_SUCCESS;
}