Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: recursion for identifying amount of zeroes in minesweeper demo

1. ## recursion for identifying amount of zeroes in minesweeper demo

Hi there, not too sophisticated code. But I cannot seem to find correct usage of recursion to find the number of adjacent zeroes when demoing my
minesweeper program. Clearly ive been trying to use said recursion in the revealZeroesOf(Spot s) method. Any ideas?

Thanks.

_____________________________________

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.ArrayList;

public class MinesweeperGame extends JPanel{
private Spot[][] spots = new Spot[10][10];
private ButtonListener l = new ButtonListener();
private Random rand = new Random();
public MinesweeperGame(){
setLayout(new GridLayout(10,10));
for (int x = 0; x < spots.length; x++){
for (int y = 0; y < spots[x].length; y++){
spots[x][y] = new Spot(x,y);
spots[x][y].setMineStatus(false);
}
}

for (int x = 0; x < 10; x++){
spots[rand.nextInt(10)][rand.nextInt(10)].setMineStatus(true);
}

setPreferredSize(new Dimension(200,200));
}
public boolean isExistingLocation(int a, int b){
if (a < 10 && b < 10
&& a >=0 && b >=0)
return true;
return false;
}
private class Spot extends JButton{
private int myX, myY;
private int mineCount = 0;
private boolean hasMine;
public Spot(int x, int y){
myX = x;
myY = y;
setBorder(BorderFactory.createLineBorder(Color.bla ck));
}
public void setMineStatus(boolean b){
hasMine = b;
}
public boolean getMineStatus(){
return hasMine;
}
public int getMineCount(){
mineCount = findSurroundingMines().size();
return mineCount;
}
public ArrayList<Spot> findSurroundingMines(){
if (isExistingLocation(myX-1, myY) && spots[myX-1][myY].getMineStatus())
if (isExistingLocation(myX-1, myY-1) && spots[myX-1][myY-1].getMineStatus())
if (isExistingLocation(myX-1, myY+1) && spots[myX-1][myY+1].getMineStatus())
}
public ArrayList<Spot> getSurroundingLocations(){
ArrayList<Spot> surLocs = new ArrayList<Spot>();
if (isExistingLocation(myX-1, myY))
if (isExistingLocation(myX-1, myY-1))
if (isExistingLocation(myX-1, myY+1))
if (isExistingLocation(myX+1,myY))
if (isExistingLocation(myX+1,myY-1))
if (isExistingLocation(myX+1, myY+1))
if (isExistingLocation(myX, myY-1))
if (isExistingLocation(myX, myY+1))

return surLocs;
}
}
public void revealZeroesOf(Spot s){
s.setText(""+s.getMineCount());
s.setBackground(Color.yellow);

/* ArrayList<Spot> locs = s.getSurroundingLocations();
ArrayList<Spot> mines = s.findSurroundingMines();

while (mines.size() == 0){
mines = s.getSurroundingLocations();
for (int x = 0; x < mines.size(); x++){
revealZeroesOf(mines.get(x));
}
}
*/
}
public void reveal(Spot s){
if (s.getMineStatus())
s.setText("!");
else if (s.getMineCount() == 0){
revealZeroesOf(s);
}
else{
s.setText(s.getMineCount()+"");
s.setBackground(Color.yellow);
}
}
private class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
for (int x = 0; x < spots.length ; x++){
for (int y = 0; y < spots[x].length; y++){
if (e.getSource() == spots[x][y]){
reveal(spots[x][y]);
}
}
}
}
}// end of ButtonListener
}

2. ## Re: recursion for identifying amount of zeroes in minesweeper demo

Not yet, but an advice. Place your code in between a [ highlight="java" ] tag and a [ /highlight ] tag. Minus the spaces, of course. Makes your code look much prettier and easier to read, like this:
```public class SomeClazz {
public static void main(String[] argv) {
System.out.println("Hi there");
}
}```