2 Replies - 14219 Views - Last Post: 30 April 2009 - 05:28 PM Rate Topic: -----

#1 dotsp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 15-February 09

Minesweeper Recursion

Posted 30 April 2009 - 06:32 AM

I'm working on a Minesweeper applet and I'm not sure why my recursive method isn't working. I'm thinking that it is because it isn't executing all the if statements. If that is the case, I'm not sure how to fix it. It sometimes removes more than one tile, sometimes it only removes one. Am I even headed the right direction with this?

I know other instructors give this assignment so I'm not posting all my code. (Not to mention I tend to do things the long way and my code is pretty long.) If I need to post the rest of my code, let me know.

Thanks for taking the time to point me in the right direction.

// hiddenBoard is an int array used to determine where the bombs, empty tiles, and number tiles are

// gameBoard is what the player sees

// EMPTY is a final int set to 0 (zero bombs next to it)

// I'm trying to keep from going out of bounds and check all eight tiles (around the tile clicked)
// in the hiddenBoard and if they are 0 set the gameBoard to EMPTY

// I'm repainting gameBoard in another method

	public int[][] isNeighborZero(int i, int j)
	{
		if((i-1 >= 0) && (j-1 >= 0) && (hiddenBoard[i][j] == EMPTY) && (hiddenBoard[i-1][j-1] == EMPTY))
		{
			gameBoard[i-1][j-1] = EMPTY;
			isNeighborZero(i-1, j-1);
		}
		if((i-1 >= 0) && (hiddenBoard[i][j] == EMPTY) && (hiddenBoard[i-1][j] == EMPTY))
		{
			gameBoard[i-1][j] = EMPTY;
			isNeighborZero(i-1, j);
		}
		if((i-1 >= 0) && (j+1 < NUMCOLS) && (hiddenBoard[i][j] == EMPTY) && (hiddenBoard[i-1][j+1] == EMPTY))
		{
			gameBoard[i-1][j+1] = EMPTY;
			isNeighborZero(i-1, j+1);
		}
		if((j-1 >= 0) && (hiddenBoard[i][j] == EMPTY) && (hiddenBoard[i][j-1] == EMPTY))
		{
			gameBoard[i][j-1] = EMPTY;
			isNeighborZero(i, j-1);
		}
		if((j+1 < NUMCOLS) && (hiddenBoard[i][j] == EMPTY) && (hiddenBoard[i][j+1] == EMPTY))
		{
			gameBoard[i][j+1] = EMPTY;
			isNeighborZero(i, j+1);
		}
		if((i+1 < NUMROWS) && (j-1 >= 0) && (hiddenBoard[i][j] == EMPTY) && (hiddenBoard[i+1][j-1] == EMPTY))
		{
			gameBoard[i+1][j-1] = EMPTY;
			isNeighborZero(i+1, j-1);
		}
		if((i+1 < NUMROWS) && (hiddenBoard[i][j] == EMPTY) && (hiddenBoard[i+1][j] == EMPTY))
		{
			gameBoard[i+1][j] = EMPTY;
			isNeighborZero(i+1, j);
		}
		if((i+1 < NUMROWS) && (j+1 < NUMCOLS) && (hiddenBoard[i][j] == EMPTY) && (hiddenBoard[i+1][j+1] == EMPTY))
		{
			gameBoard[i+1][j+1] = EMPTY;
			isNeighborZero(i+1, j+1);
		}
		return gameBoard;
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Minesweeper Recursion

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Minesweeper Recursion

Posted 30 April 2009 - 07:29 AM

You're doing way too much work. You also never check if you're in bounds before you start working, which is you problem.

Not 100% sure what you're aiming at, but this may help.

void checkTile(int i, int j) {
	// first, check the bounds.  If it's out, leave
	if (i<0 || j<0 || i>=hiddenBoard.length || j>=hiddenBoard[0].length) { return; }
	
	// you need a way to stop searching here
	// maybe
	if (hiddenBoard[i][j]!=EMPTY) { return; }
	
	// assume that if gameBoard is empty, you've been here already
	if (gameBoard[i][j]==EMPTY) { return; }
	
	// time to do work
	gameBoard[i][j] = EMPTY;
	checkTile(i+1, j);
	checkTile(i-1, j);
	checkTile(i, j+1);
	checkTile(i, j-1);
	
}


Was This Post Helpful? 2
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Minesweeper Recursion

Posted 30 April 2009 - 05:28 PM

When recursion is involved... call Baavgai :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1