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].addActionListener(l);
spots[x][y].setMineStatus(false);
add(spots[x][y]);
}
}
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(){
ArrayList<Spot> adjMineSpots = new ArrayList<Spot>();
if (isExistingLocation(myX-1, myY) && spots[myX-1][myY].getMineStatus())
adjMineSpots.add(spots[myX-1][myY]);
if (isExistingLocation(myX-1, myY-1) && spots[myX-1][myY-1].getMineStatus())
adjMineSpots.add(spots[myX-1][myY-1]);
if (isExistingLocation(myX-1, myY+1) && spots[myX-1][myY+1].getMineStatus())
adjMineSpots.add(spots[myX-1][myY+1]);
return adjMineSpots;
}
public ArrayList<Spot> getSurroundingLocations(){
ArrayList<Spot> surLocs = new ArrayList<Spot>();
if (isExistingLocation(myX-1, myY))
surLocs.add(spots[myX-1][myY]);
if (isExistingLocation(myX-1, myY-1))
surLocs.add(spots[myX-1][myY-1]);
if (isExistingLocation(myX-1, myY+1))
surLocs.add(spots[myX-1][myY+1]);
if (isExistingLocation(myX+1,myY))
surLocs.add(spots[myX+1][myY]);
if (isExistingLocation(myX+1,myY-1))
surLocs.add(spots[myX+1][myY-1]);
if (isExistingLocation(myX+1, myY+1))
surLocs.add(spots[myX+1][myY+1]);
if (isExistingLocation(myX, myY-1))
surLocs.add(spots[myX][myY]);
if (isExistingLocation(myX, myY+1))
surLocs.add(spots[myX][myY]);
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
}
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:
Code java:
public class SomeClazz {
public static void main(String[] argv) {
System.out.println("Hi there");
}
}
:)
Anyway, will take a look at your code and see if I can help you.
EDIT:
Ok, I am not sure what you mean by "adjacent zeroes" to be honest. Could you explain in more detail?
And I am not sure it matters, but at first glance the "findSurroundingMines" method in the Spot class seems incomplete. At least to me :P.