Java Maze Boundary Check Method for Pathfinding

In summary, The conversation is a discussion about writing a method to check for neighbors in a grid, and the problem of making sure the conditions do not go outside of the grid. The code provided shows a series of if statements being used to check for neighbor coordinates, but it seems that the two conditions for col are not being checked. After some discussion, it is realized that the last two outer else if statements are not being reached due to the earlier conditions being met. The conversation ends with a quote about the importance of arts, crafts, and sciences.
  • #1
apiwowar
96
0
I'm writing a method that checks a given coordinates neighbors to see if they are part of the path. the problem I am having with is the conditions that ihave to make sure i don't go outside of the grid. it doesn't seem that the two conditions for col are being checked. i put print statements inside each check to see if they get printed and the two for col dont. is there any obvious mistake that i made?

Thank you

Code:
public static void neighbors(Coordinate current, int[][] grid, Queue q)
	{
	        int row = current.getRow();
		int col = current.getCol();
		
		if((row - 1) >= 0)
		{
			System.out.println("Checking north");
			if(grid[row-1][col] == 1)
			{
				if(grid[row][col] == -1)
				{
					grid[row-1][col] = grid[row][col] + 2;
				}
			
				else
				{
					grid[row-1][col] = grid[row][col] + 1;
				}
			
				Coordinate x = new Coordinate(row-1,col);
			
				System.out.println("first if in neighbor");
				System.out.println("enqueue");
				q.enqueue(x);
				System.out.println("test count " + q.getCount());
			}
		}
		else if((row + 1) <= grid.length)
		{
			System.out.println("checking south");
			if(grid[row+1][col] == 1)
			{
				if(grid[row][col] == -1)
				{
					grid[row+1][col] = grid[row][col] + 2;
				}
			
				else
				{
					grid[row+1][col] = grid[row][col] + 1;
				}
			
				Coordinate x = new Coordinate(row+1,col);
				System.out.println("second if in neighbor");
				System.out.println("enqueue");
				q.enqueue(x);
				System.out.println("test count " + q.getCount());
			}
		}
		
		else if((col - 1) >= 0)
		{
			System.out.println("Checking east");
			if(grid[row][col-1] == 1)
			{
				if(grid[row][col] == -1)
				{
					grid[row][col-1] = grid[row][col] + 2;
				}
			
				else
				{
					grid[row][col-1] = grid[row][col] + 1;
				}
			
				Coordinate x = new Coordinate(row, col - 1);
				System.out.println("third if in neighbor");
				System.out.println("enqueue");
				q.enqueue(x);
				System.out.println("test count " + q.getCount());
			
			}
		}
		
		else if((col + 1) <= grid[0].length)
		{
			System.out.println("checking west");
			if(grid[row][col+1] == 1)
			{
				if(grid[row][col+1] == -1)
				{
					grid[row][col+1] = grid[row][col] + 1;
				}
			
				else
				{
					grid[row][col+1] = grid[row][col] + 1;
				}
			
				Coordinate x = new Coordinate(row, col + 1);
				System.out.println("fourth if in neighbor");
				System.out.println("enqueue");
				q.enqueue(x);
				System.out.println("test count " + q.getCount());
			}
		}
		
		else
		{
		
		}
		
		System.out.println("dequeue in neighbor");
		q.dequeue();
		
		
	}
 
Physics news on Phys.org
  • #2
It looks like you're not even getting to those last two outer else if statements because if

Code:
(row - 1) >= 0 || (row + 1) <= grid.length

Then you don't get to test if

Code:
(col - 1) >= 0 || (col + 1) <= grid[0].length

Yeah, like you said, I suppose that was an obvious mistake. Funny how those get you all the time no ;)

======================================
Arts, crafts and sciences uplift the world of being and are conducive to its exaltation
~Baha'u'llah
 
Last edited:

1. What is a boundary check in a Java maze?

A boundary check in a Java maze refers to the process of ensuring that the player or character does not go beyond the boundaries or walls of the maze. It is a crucial step in creating a successful maze game as it prevents the player from cheating or getting stuck in unreachable areas.

2. Why is a boundary check important in a Java maze?

A boundary check is important in a Java maze to maintain the integrity and fairness of the game. Without it, players can easily cheat by going through walls or getting stuck in unreachable areas, which can ruin the gameplay experience. A boundary check also adds an element of challenge and strategy to the game.

3. How is a boundary check implemented in Java?

In Java, a boundary check can be implemented using conditional statements such as if/else or switch statements. These statements can be used to check the coordinates of the player or character and ensure that they are within the boundaries of the maze. Additionally, Java libraries such as the AWT and Swing provide tools for creating graphical user interfaces that can be used to visually represent the maze and its boundaries.

4. Can a boundary check be applied to all types of mazes in Java?

Yes, a boundary check can be applied to all types of mazes in Java. Whether the maze is created using arrays, graphs, or other data structures, a boundary check can be implemented to ensure that the player stays within the designated boundaries. The specific implementation may vary depending on the type of maze, but the overall concept remains the same.

5. Are there any tools or resources available to help with implementing a boundary check in a Java maze?

Yes, there are various tools and resources available to help with implementing a boundary check in a Java maze. These include online tutorials, forums, and documentation that provide step-by-step instructions and examples. Additionally, there are game development libraries and frameworks, such as LibGDX and Unity, that offer built-in features for creating and managing boundaries in maze games.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
949
Back
Top