Java Maze Boundary Check Method for Pathfinding

  • Context: Comp Sci 
  • Thread starter Thread starter apiwowar
  • Start date Start date
  • Tags Tags
    Boundary Java
Click For Summary
SUMMARY

The discussion focuses on a Java method for checking neighboring coordinates in a grid for pathfinding, specifically addressing boundary conditions. The user encountered issues with the conditions for column checks not being executed. The problem was identified as a logical error in the conditional statements, where the checks for row boundaries prevented the column checks from being evaluated. The solution involves restructuring the conditional logic to ensure all boundary checks are performed correctly.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of grid data structures
  • Knowledge of queue data structures
  • Familiarity with conditional statements and logical operators
NEXT STEPS
  • Review Java conditional statements and logical operators
  • Learn about implementing boundary checks in grid algorithms
  • Explore pathfinding algorithms such as A* and Dijkstra's
  • Investigate Java Queue implementations and their applications in pathfinding
USEFUL FOR

Java developers, computer science students, and anyone interested in implementing pathfinding algorithms in grid-based systems.

apiwowar
Messages
94
Reaction score
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
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:

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 11 ·
Replies
11
Views
10K
  • · Replies 8 ·
Replies
8
Views
13K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K