-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminesweeper.cpp
More file actions
135 lines (128 loc) · 5.84 KB
/
minesweeper.cpp
File metadata and controls
135 lines (128 loc) · 5.84 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
#include "minesweeper.h"
#include "Toolbox.h"
#include <iostream>
using namespace std;
bool debugMode = false;
std::vector<std::vector<Tile::State>> originalTileStates; // Store original states
int launch() {
render();
return 0;
}
void restart() {
}
void render() {
// Main loop
Toolbox& toolbox = Toolbox::getInstance();
while (toolbox.window.isOpen()) {
sf::Event event;
while (toolbox.window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
toolbox.window.close();
} else if (event.type == sf::Event::MouseButtonPressed) {
sf::Vector2f mousePosition = toolbox.window.mapPixelToCoords(
sf::Vector2i(event.mouseButton.x, event.mouseButton.y));
if (event.mouseButton.button == sf::Mouse::Left && mousePosition.x >= 368 && mousePosition.x <= 368 + 64 &&
mousePosition.y >= 512 && mousePosition.y <= 512 + 64) {
delete toolbox.gameState;
// Create a new game state
toolbox.gameState = new GameState();
// Reset debug mode and original states
debugMode = false;
originalTileStates.clear();
} else if (mousePosition.x >= 496 && mousePosition.x <= 496 + 64 &&
mousePosition.y >= 512 && mousePosition.y <= 512 + 64) {
// Handle click on the debug button
toggleDebugMode();
if (debugMode == true) {
// Store original states if entering debug mode
originalTileStates.clear();
for (int y = 0; y < 25; ++y) {
std::vector<Tile::State> rowStates;
for (int x = 0; x < 16; ++x) {
Tile* currentTile = toolbox.gameState->getTile(x, y);
rowStates.push_back(currentTile->getState());
}
originalTileStates.push_back(rowStates);
}
// Change states to EXPLODED in debug mode
for (int y = 0; y < 25; ++y) {
for (int x = 0; x < 16; ++x) {
Tile* currentTile = toolbox.gameState->getTile(x, y);
// Check if it's a mine
if (auto mine = dynamic_cast<Mine*>(currentTile)) {
mine->setState(Tile::EXPLODED);
}
}
}
} else {
// Restore original states when exiting debug mode
for (int y = 0; y < 25; ++y) {
for (int x = 0; x < 16; ++x) {
Tile* currentTile = toolbox.gameState->getTile(x, y);
currentTile->setState(originalTileStates[y][x]);
}
}
}
} else if (mousePosition.x >= 560 && mousePosition.x <= 560 + 64 &&
mousePosition.y >= 512 && mousePosition.y <= 512 + 64) {
// Handle click on test button 1
// ...
} else if (mousePosition.x >= 624 && mousePosition.x <= 624 + 64 &&
mousePosition.y >= 512 && mousePosition.y <= 512 + 64) {
// Handle click on test button 2
// ...
} else {
// The click is not on any button, so it must be on a tile
// Iterate over tiles and check for left/right-click on each tile
for (int y = 0; y < 25; ++y) {
for (int x = 0; x < 16; ++x) {
Tile *currentTile = toolbox.gameState->getTile(x, y);
if (mousePosition.x >= currentTile->getLocation().x &&
mousePosition.x <= currentTile->getLocation().x + 32 &&
mousePosition.y >= currentTile->getLocation().y &&
mousePosition.y <= currentTile->getLocation().y + 32) {
// Handle left/right-click on the current tile
if (event.mouseButton.button == sf::Mouse::Right) {
currentTile->onClickRight();
} else if (event.mouseButton.button == sf::Mouse::Left) {
currentTile->onClickLeft();
}
}
}
}
}
}
}
toolbox.window.clear(sf::Color::White);
// display tiles
for (int y = 0; y < 25; ++y) {
for (int x = 0; x < 16; ++x) {
toolbox.gameState->getTile(x, y)->draw();
}
}
// display buttons
toolbox.newGameButton->getSprite()->setPosition(368,512);
toolbox.window.draw(*toolbox.newGameButton->getSprite());
toolbox.debugButton->getSprite()->setPosition(496,512);
toolbox.window.draw(*toolbox.debugButton->getSprite());
toolbox.testButton1->getSprite()->setPosition(560,512);
toolbox.window.draw(*toolbox.testButton1->getSprite());
toolbox.testButton2->getSprite()->setPosition(624,512);
toolbox.window.draw(*toolbox.testButton2->getSprite());
toolbox.window.display();
}
}
void toggleDebugMode() {
if (!debugMode) {
debugMode = true;
}
else {
debugMode = false;
}
}
bool getDebugMode(){
return debugMode;
}
int main() {
launch();
}