Home > Net >  How to check if a traversion has reached a certain Index in a 2D-Array in Java?
How to check if a traversion has reached a certain Index in a 2D-Array in Java?

Time:04-29

Let's say we have a 2D-boolean Array as a presentation of a maze, the size of the Array is not fixed and random. The walls are depicted as true:

boolean[][] mazeArray = new boolean[width][height];

The exit of the maze is at a fixed Index. How can I check wether the traversion has reached this certain index or not? My idea was to create an int[ ] to keep track of the position, it gets updated and overwritten with every step:

int[] location = {1,0};

... But I don't understand why my check in the while-loop doesn't work:

while( location[0] != (maze[0].length-1) && location[1] != (maze[1].length-2) ) {
   // traversion with pledge algorithm
    }

CodePudding user response:

The problem with your code is that you check wrong items in your maze array:

  • maze[0] is the first "line" of your 2d-array
  • maze[1] is the second "line" of your 2d-array

Proper way of traversing 2d-array is following (I've replaced your location array with separate x and y variables to better visualize the algorithm).

My algorithm enters the 2d mazeArray line by line and then iterates each line's elements.

public class Maze {
    public static void main(String[] args) {
        int width = 20;
        int height = 20;

        boolean[][] mazeArray = new boolean[width][height];

        int x = 0;
        int y = 0;

        while (y < mazeArray.length) {
            while (x < mazeArray[y].length) {
                System.out.println("Traverse at ("   x   ", "   y   ")");

                x  = 1;
            }

            x = 0;
            y  = 1;
        }
    }
}

CodePudding user response:

You're making everything so much harder for yourself. Go easier ways.

Use simpler locations handling

Instead of a location[], simlpy use int destinyX and int destinyY. And as your current position, you should use int positionX and int positionY.

If you'd like the OO-stlye more, or maybe wanna keep the gates open for a solution in 3D or n-D, you could introduce a Location class that has X and Y, and all movement and checks could be handled by that class. Then you'd have Location targetLocation = new Location(x,y); and your current position as Location currentPosition = new Location(x,y);. You then could check with if (currentPosition.equals(targetLocation))...; or in your case while(!currentPosition.equals(targetLocation)) {...}

It seems you have misunderstood the array.length function, or you're using it in an awfully inconvenient way.

At the moment you're blindly shooting at the array lengths of maze[]. This is bad for 2 reasons:

  1. array lengths should not have anything to do with positions inside the array (logical paradox), and
  2. because you could never freely move your destination, it would always stick to the right or bottom outsides of the maze

Use the positioning above, this will clear up that problem.

Suggestion: use a byte[][] or enum[][] for maze

At the moment, you only know if you have a wall at a certain location. If you wanna include other elements, like water, treasure, or the target location, wormholes etc, then you should either:

  • Use a byte[][] and fill it with values
    • value 0 could be pathways
    • value 1 could be walls
    • value 2 could be the exit
    • value 3 could be the water etc.
  • Use constants, like static public final int WATER_CODE = 3;

Or, alternatively, create your own enum:

public enum LocationType {PATH, WALL,EXIT,WATER}

and then have maze be like:

LocationType[][] mazeArray = new LocationType[width][height];

and everything is PATH in the beginning, and you can set up WALLS like this:

mazeArray[x][y] = LocationType.WALL;

or water:

mazeArray[x][y] = LocationType.WATER;

Use class or interface for maze[][]

For the sake of Wormhole or extended functionality, you could also use a class instead of an enum:

abstract class LocationType {}

and then implement certain types, like

class Wall extends LocationType {}

or even

class Wormhole extends LocationType {
    public Location leadsTo() { /* */ };
}

and

class Treasure extends LocationType {
    public int getAmoundOfGoldCoinsFound() { /* */ };
}

If you implement LocationType as an interface, replace 'extends' by 'implements'

  • Related