1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java maze boundary check

  1. Dec 5, 2011 #1
    I'm writing a method that checks a given coordinates neighbors to see if they are part of the path. the problem im having with is the conditions that ihave to make sure i dont go outside of the grid. it doesnt 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 (Text):
    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();
           
           
        }
     
  2. jcsd
  3. Dec 8, 2011 #2
    It looks like you're not even getting to those last two outer else if statements because if

    Code (Text):

    (row - 1) >= 0 || (row + 1) <= grid.length
     
    Then you don't get to test if

    Code (Text):

    (col - 1) >= 0 || (col + 1) <= grid[0].length
     
    Yeah, like you said, I suppose that was an obvious mistake. Funny how those get ya all the time no ;)

    ======================================
    Arts, crafts and sciences uplift the world of being and are conducive to its exaltation
    ~Baha'u'llah
     
    Last edited: Dec 8, 2011
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java maze boundary check
  1. Java maze building (Replies: 0)

  2. Java program (Replies: 3)

  3. Java prob. (Replies: 1)

  4. HeapSort Java (Replies: 3)

Loading...