• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Minesweeper - Having problems figuring out logic for adjacent checks

5 posts in this topic

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0