Results 1 to 2 of 2
  1. #1
    Rehad is offline Member
    Join Date
    Nov 2014
    Posts
    1
    Rep Power
    0

    Default Java Recursion Minesweeper

    Hey guys. Need some major help with recursion in a MineSweeper game.

    So far, when a user clicks on a square and it has a bomb on it, I reveal the bomb to the user - see clicked(). I also have the ability to show the user if the squares surrounding them have any adjacent bombs - see countAdjacentSquares().

    However, I am having trouble with my fillZeros() recursive method. When a user clicks on a square that has zero bombs in its surrounding squares, it not only reveals that square, but also any adjacent squares (horizontally, vertically or diagonally adjacent) that also have zero bombs in its surrounding squares.

    I don't have any recursion, if I click on a square with no bombs all it does it set the square to 0, but doesn't continue to check for other empty squares.

    Java Code:
    import javax.swing.*;
    
    import java.awt.*;
    import java.util.*;
    
    public class Square extends GameSquare
    {
    	private boolean revealed = false;
    	private boolean squareHasBomb = false;
    	public static final int MINE_PROBABILITY = 10;
    	public int mineCount = 0;
    
    	public Square(int x, int y, GameBoard board)
    	{
    		super(x, y, "images/blank.png", board);
    
    		Random r = new Random();
    		squareHasBomb = (r.nextInt(MINE_PROBABILITY) == 0);
    	}
    
    	private int countAdjacentSquares(){
    		for(int xOffset = -1; xOffset <= 1; xOffset++)
    			for (int yOffset = -1; yOffset <= 1; yOffset++){
    				Square adjacentSquare = (Square)board.getSquareAt(xLocation +xOffset, yLocation +yOffset);
    				if (adjacentSquare.squareHasBomb){
    					mineCount++;
    					revealed = true;
    				}
    			}
    		setImage("images/" + mineCount +".png");
    		return mineCount;	
    	}
    	
    	// Recursive method that will 0-fill all surround empty squares
    	private void fillZeros(){
    		for(int xOffset = -1; xOffset <= 1; xOffset++)
    			for (int yOffset = -1; yOffset <= 1; yOffset++){
    				Square zeroSquare = (Square)board.getSquareAt(xLocation +xOffset, yLocation +yOffset);
    				
    				// Base Case. Square must have bomb or a neighbouring bomb
    				if(zeroSquare.squareHasBomb == true && revealed == true){
    					return;
    				}
    				// The square doenst have a bomb and an adjacent bomb and can be set to empty
    				else if(zeroSquare.squareHasBomb == false && revealed == false){
    					setImage("images/0.png");
    					revealed = true;
    				}
    				
    			}fillZeros();
    	}
    	
    	
    	public void clicked(){
    		if (squareHasBomb == true)
    			setImage("images/bomb.png");
    		
    		else if(squareHasBomb == false && revealed == false)
    			countAdjacentSquares();
    			
    		else if(squareHasBomb == false)
    			fillZeros();
    
    	}
    }
    I cannot see where I am going wrong here. Any help would be greatly appreciated

    Thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Java Recursion Minesweeper

    but doesn't continue to check for other empty squares.
    How are you debugging the code?
    Add some println statements that print out messages as the loops execute. Include the x and y values and the has bomb values in the printed messages.

    The print out will help you see what the computer sees when it executes the code.
    Last edited by Norm; 11-16-2014 at 09:08 PM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: 06-13-2013, 04:36 PM
  2. Replies: 7
    Last Post: 12-11-2011, 10:06 AM
  3. [suggestions] minesweeper
    By temyong in forum New To Java
    Replies: 7
    Last Post: 12-30-2010, 04:10 AM
  4. MineSweeper!
    By CrushOverwrite in forum New To Java
    Replies: 17
    Last Post: 02-24-2010, 03:34 PM
  5. Minesweeper problem! need help!
    By jeffrey in forum New To Java
    Replies: 1
    Last Post: 10-02-2009, 05:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •