MCPcopy Create free account
hub / github.com/LemonOSProject/LemonOS / OnMouseUp

Method OnMouseUp

Applications/Minesweeper/main.cpp:177–227  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

175 }
176
177 void OnMouseUp(vector2i_t pos){
178 pos -= fixedBounds.pos;
179 int tileX = pos.x / 16;
180 int tileY = pos.y / 16;
181
182 if(tileX < 0 || tileX >= mapSize.x || tileY < 0 || tileY > mapSize.y) return;
183
184 Tile& tile = tiles[tileY][tileX];
185
186 if(tile.flagged){
187 return; // Don't reveal flagged tiles
188 }
189
190 std::function<void(int,int)> revealAdjacent = [&](int x, int y) {
191 for(int i = std::max(0, y - 1); i <= y + 1 && i < mapSize.y; i++){
192 if(x - 1 >= 0 && !tiles[i][x - 1].flagged){ // If something has been incorrectly flagged make sure nto to reveal
193 bool wasHidden = tiles[i][x - 1].hidden;
194 tiles[i][x - 1].hidden = false;
195 if(tiles[i][x - 1].surroundingMines == 0 && wasHidden){ // If tile does not have a number reveal more tiles
196 revealAdjacent(x - 1, i);
197 }
198 }
199
200 if(!tiles[i][x].flagged){
201 bool wasHidden = tiles[i][x].hidden;
202 tiles[i][x].hidden = false;
203 if(tiles[i][x].surroundingMines == 0 && wasHidden){
204 revealAdjacent(x, i);
205 }
206 }
207
208 if(x + 1 < mapSize.x && !tiles[i][x + 1].flagged){
209 bool wasHidden = tiles[i][x + 1].hidden;
210 tiles[i][x + 1].hidden = false;
211 if(tiles[i][x + 1].surroundingMines == 0 && wasHidden){
212 revealAdjacent(x + 1, i);
213 }
214 tiles[i][x + 1].hidden = false;
215 }
216 }
217 };
218
219 if(tile.type == MineTile){
220 GameOver();
221 } else {
222 tile.hidden = false;
223 if(tile.surroundingMines == 0){
224 revealAdjacent(tileX, tileY);
225 }
226 }
227 }
228
229 void OnRightMouseUp(vector2i_t pos){
230 pos -= fixedBounds.pos;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected