Jump to content
  • Advertisement
Sign in to follow this  

Minesweeper - Having problems figuring out logic for adjacent checks

This topic is 1675 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm just finished my first year of CompSci at my college. I'm new to game programming, but I did very well in my programming classes, albeit the programming assignments were asinine and simple. 


To practice and learn more I am creating a Minesweeper game in Java. So far I have been able to construct a board with various sizes depending on the users selection and then populate it randomly with mines. I have implemented flagging a square to disable it, and detonating all bombs on the field should you click on one. What I'm having trouble with is the recursive logic I need to implement should you click on a square without a bomb in it. I can only check itself and usually at most one other square.


My field is constructed of a 2D array of Mine objects that extend JButton.


What I need help with is figuring out how to recursively check all adjacent buttons, then should it not find any bombs in those, to call itself again.

Share this post

Link to post
Share on other sites

I don't actually know the rules for minesweeper, so I'm having trouble constructing an answer for you.

I also haven't written Java in many years. So, I hope you understand what I write below.


For your recursion, you need one main function that calls a helper recursive function. In the end you should be getting a list of objects that will help you detonate tiles over time.


void detonateHelper(int x, int y, SomeList<> detonationList)


     TilePosition tpos(x, y);

     // here we can check if there are any particular reasons to not detonate this area (i don't know what this function is supposed to do)


     // here we can check whether the position (x, y) is out of range, for example via a isValid() function in tpos:

     if (tpos.isValid() == false) return;


     // if we can't add this particular position to our detonation list, we exit

     if (detonationList.contains(tpos)) return;



     // try all the different directions we can detonate in

     detonateHelper(x+1, y, detonationList);

     detonateHelper(x-1, y, detonationList);

     detonateHelper(x, y+1, detonationList);

     detonateHelper(x, y-1, detonationList);



SomeList<TilePosition> detonate(int x, int y)


     SomeList<TilePosition> detonationList = new JavaBS;

     detonateHelper(x, y, detonationList);

     return detonationList;



Using the detonationList you can now create a cool detonation animation that happens over time.

Note that the detonation will happen in a very specific order, which you can change by adding a depth-value to the object you store in your list.

You can then sort your list by depth, so that the explosion could happen outwards.


Alternatively you can create your list as a tree, by having each helper function return a list. Then as your helpers find subtrees, you will eventually have a full grown tree that you can explode outwards in any tree-iterative fashion you want. Pretty nifty.

Edited by Kaptein

Share this post

Link to post
Share on other sites

I can try to explain it.


Basically, when you click on a button,


if it is a bomb: the game ends and all bombs show up.            I got this working.

if it there is a bomb adjacent to it, then a number shows up on the button displaying how many adjacent bombs there are.              This isn't too hard.


This is the problem I have: if you click a button and there are no adjacent bombs, then you repeat this step with all adjacent tiles. The pseudo code would be like this:


method CheckBomb()


if bomb, then detonate


else if adjacent bomb >= 1, display number of adjacent bombs,


else if adjacent bomb = 0, check bomb() <except on each adjacent tile>


the last case is where I'm having problems.





EDIT: Thanks for the help.

Edited by phyremynd

Share this post

Link to post
Share on other sites

This is the problem I have: if you click a button and there are no adjacent bombs, then you repeat this step with all adjacent tiles.



Oh right, you want to floodfill an area in between numbers and bombs, like the grey area in minesweeper?

For that you can use a simple floodfill recursion algorithm with a boolean helper array.

See my example above, except there's an additional test: If the area is non-gray return immediately.

At the end of the detonation (now known as grey-ifying) just set each tile in the list to grey. Or set the tiles to grey directly, and use that as your "I've been here before" return condition.


Depending on how you store your tiles the recursive function is different. In my case I used a List container, which doesn't have to be necessary. If you store your tile identifiers as integers in your tile map you can recursively just set each tile to Grey status and use that as a return condition for not recursively spreading into that tile again (preventing infinite recursion).


void greyifier(int x, int y)


     // here we can check whether the position (x, y) is out of range, for example via an isValid() function

     if (tilemap.isValid(x, y) == false) return;

     // check if we've been here before, since this is the greyifier we don't want to revisit a tile that is already grey

    if (tilemap.getTile(x, y).getID() == Tilemap::GREY) return;


     // here you need to add your own tests, basically checking if this tile can be set to gray

     // for that to happen the tile must be greyifiable and meet the conditions you outlined yourself (I'm sure you can figure this out)


     // try all the different directions we can go in

     greyifier(x+1, y);

     greyifier(x-1, y);

     greyifier(x, y+1);

     greyifier(x, y-1);


Edited by Kaptein

Share this post

Link to post
Share on other sites

It seems like you already have a set implementation, but I thought I would give my brief 2-cents.


I am unsure if you are familiar with the Queue data structure and linked lists, but in the interest of avoiding the overhead of recursion, implementing a breadth-first search would seem very relevant.


The basic idea is to setup a queue which will contain all nodes to be visited. The initial node, the 0-adjacent bomb tile the user clicked on, would form your starting central node.  You then proceed to enqueue all adjacent nodes to the initial node that meet your criteria; that is, are also 0-adjacency bomb nodes. Finally, dequeue a node from the queue, mark it as visited, and then proceed to start the process over with this new node as your central node. 


Proceeding in this fashion would eliminate the overhead of recursion, all the while being fairly easy to implement.


If you have any questions just ask, all I'm doing is procrastinating while I should be studying for finals.

Edited by nubbin

Share this post

Link to post
Share on other sites

How I would approach this is as follows:

  1. The procedure is called on the selected tile
  2. Check if the selected tile contains a mine, if so the game is over
  3. Otherwise, make the selected tile visible and check if the selected tile has any adjacent mines
  4. If there are no adjacent mines, call the procedure on all the non-visible adjacent tiles
Edited by RulerOfNothing

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!