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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: recursion for identifying amount of zeroes in minesweeper demo

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry 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
    }


  2. #2
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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");
        }
    }


    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 .
    Last edited by Kerr; November 28th, 2011 at 04:01 PM.

Similar Threads

  1. Need help with simple class and demo program
    By TurboST2 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 11th, 2011, 06:30 AM
  2. Identifying invalid calls
    By joshft91 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 26th, 2011, 05:38 PM
  3. Can anyone please help me with this java code for minesweeper game!
    By Mahela in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 25th, 2011, 08:10 AM
  4. Identifying calling object
    By frogfury in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 25th, 2010, 05:13 PM
  5. Minesweeper java problem
    By bluez_exe in forum Paid Java Projects
    Replies: 1
    Last Post: March 3rd, 2010, 08:55 PM

Tags for this Thread