Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm trying write the code for a minesweeper game. trying to write a function in the c language to reveal the tiles around the selected tile if the selected tile has no bombs around it. i think this can be done recursively but got no clue about how to do it that way. the thing is i don't know if this can be done by one recursive function because it has to check every direction and reveal them if there are no bombs around them and stop until it reaches a tile which has at least a bomb around it can someone explain how i can create this function. and an another problem is how can i make it evaluate all of the tiles around it? can someone help me and it doesn't have to be a recursive solution because i'm guessing that would be way more confusing.

What I have tried:

guys i'm sorry but i just don't have a single clue!!!
Updated 20-Dec-18 13:18pm
KarstenK 22-Dec-18 14:02pm    
use an array like int playground[8][8];

1 solution

Here's one way. Let's say your location of interest is at position (x,y). You can write a function (or method) called CheckTile and pass it the position to check. Then you can write your check logic something like this:
CheckTile( x-1, y );  // to the left
CheckTile( x, y-1 );  // the one above
CheckTile( x+1, y );  // to the right
CheckTile( x, y+1 );  // the one below
Remember to make certain the position passed into CheckTile is valid - negative coordinates are not valid for example. If you need to check the adjacent tiles at 45-degree angles also then those would be at positions (x+1,y+1), (x+1, y-1), (x-1,y-1), and (x-1,y+1).

This is just a basic algorithm. You will likely need to pass more parameters and return something useful from the function.

To do what you described then order the search in a way that makes sense and return when the first one is found. For example, you could check in a clock-wise direction beginning with the one above. The key is to write very basic functions that you can easily arrange to make the logic work the way it needs to.
Share this answer
CPallini 21-Dec-18 3:11am    
Rick York 21-Dec-18 13:39pm    
Thank you CP.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900