Connecting a maze/grid's walls so all are interconnected

吃可爱长大的小学妹 提交于 2020-03-01 10:20:12

问题


I have a 2d grid that I'm trying to create links between all walls.

The grid is constructed like so:

    grid = new State[8][8];
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            grid[i][j] = State.blank;
        }
    }

I have a robot that should be able to pass through the walls to the opposite side like on a game of snake.

So for example if the robot is facing NORTH and is in position x[0]y[1] then it should connect to x[7]y[1].

The robot should also be able to read whats in the three blocks in front of it, one to the left, one to the right and one directly infront.

# x = empty space
# R = robot
# S = spaces robots sensors pick up

If it were facing north this is what the robot would pick up:

[S][S][S][x][x][x][x][x]
[x][R][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]

Like wise if the robot was facing east this is what it would pick up:

[x][x][S][x][x][x][x][x]
[x][R][S][x][x][x][x][x]
[x][x][S][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]

The problem I'm having is finding the right algorithm to make sure that the robot can not only pass through walls but also read sensors through the walls.

If the robot was in the top left corner and facing NORTH then it would read through the wall like so:

[R][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[x][x][x][x][x][x][x][x]
[S][S][x][x][x][x][x][S]

As you can imagine I've already tried doing length chunks of IF statements but there is too many possiblities to cover them all without going insane!

I've also wrote down the changes to X & Y on paper when placed in certain circumstances but I can't really see any pattern that hints toward an algorithm.

Any help would be appreciated!


回答1:


public class Robot {
    public int x;
    public int y;
    public Robot(int x,int y) {
        this.x = x;
        this.y = y;
    }
    public void move(int direction, int steps) {
        switch(direction) {
            case 1: //north
                int temp1 = (x-steps)%8;
                x = temp1<0?(temp1+8):temp1;
                break;
            case 2: //south
                x = (x+steps)%8;
                break;
            case 3: //west
                int temp3 = (y-steps)%8;
                y = temp3<0?(temp3+8):temp3;
                break;
            case 4: //east
                y = (y+steps)%8;
                break;
            default:
                System.out.println("I'm not smart enough to handle the direciton provided!");
        }
    }

    public static void main(String[] args) {
        int[][] grid = new int[8][8];
        Robot robot = new Robot(0,0);
        System.out.println("I'm starting at (0,0).");
        robot.move(3, 9);
        System.out.println("I'm moving west by 9 steps.");
        System.out.println("I've arrived at ("+robot.x+","+robot.y+").");
    }
}

Hope the code above gives an idea. I've tested it. Feel free to have a try. The computation of the three blocks in front of the robot is similar. You can figure it out on your own.




回答2:


Use an iterator like x = (x + 1) % array.length

(or x = (x - 1) % array.length)




回答3:


I'll try to break it down into different parts, hopefully that will help you. So, you have a 8x8 grid, represented by X and Y coordinates, with (0,0) being the top left corner and (7,7) being the bottom right corner. Your algorithm will look like this:

walking through walls:
N -> x = x, y = (y==0)?7:y-1
S -> x = x, y = (y==7)?0:y+1
E -> x = (x==7)?0:x+1, y = y
W -> x = (x==0)?7:x-1, y = y

Look ahead
N -> LH1 = x=x, y=y-1
     LH2 = x=x-1, y=y-1
     LH3 = x=x+1, y=y-1
S -> LH1 = x=x, y=y+1
     LH2 = x=x-1, y=y+1
     LH3 = x=x+1, y=y+1
E -> LH1 = x=x+1, y=y
     LH2 = x=x+1, y=y-1
     LH3 = x=x+1, y=y+1
W -> LH1 = x=x-1, y=y
     LH2 = x=x-1, y=y-1
     LH3 = x=x-1, y=y+1

Now if I convert this algorithm into java methods, they will look like this:

public int getNextX (int currentX, String direction)
{
    if ("N".equals (direction) || "S".equals (direction))
    {
        return currentX;
    }
    else if ("E".equals (direction))
    {
        return ((currentX==7) ? 0 : currentX + 1);
    }
    else if ("W".equals (direction))
    {
        return ((currentX==0) ? 7 : currentX - 1);
    }
}

public int getNextY (int currentY, String direction)
{
    if ("E".equals (direction) || "W".equals (direction))
    {
        return currentY;
    }
    else if ("S".equals (direction))
    {
        return ((currentY==7) ? 0 : currentY + 1);
    }
    else if ("N".equals (direction))
    {
        return ((currentY==0) ? 7 : currentY - 1);
    }
}


public ArrayList getLookAheads (int currentX, int currentY, String direction)
{
    ArrayList lookAheads = new ArrayList ();
    int x[3];
    int y[3];
    if ("N".equals (direction))
    {
        // LH1
        x[0] = currentX;
        y[0] = currentY - 1;

        // LH2
        x[1] = currentX - 1;
        y[1] = currentY - 1;

        // LH3
        x[2] = currentX + 1;
        y[2] = currentY - 1;
    } 
    else if ("S".equals (direction))
    {
        // LH1
        x[0] = currentX;
        y[0] = currentY + 1;

        // LH2
        x[1] = currentX - 1;
        y[1] = currentY + 1;

        // LH3
        x[2] = currentX + 1;
        y[2] = currentY + 1;
    } 
    else if ("E".equals (direction))
    {
        // LH1
        x[0] = currentX + 1;
        y[0] = currentY;

        // LH2
        x[1] = currentX + 1;
        y[1] = currentY - 1;

        // LH3
        x[2] = currentX + 1;
        y[2] = currentY + 1;
    } 
    else if ("E".equals (direction))
    {
        // LH1
        x[0] = currentX - 1;
        y[0] = currentY;

        // LH2
        x[1] = currentX - 1;
        y[1] = currentY - 1;

        // LH3
        x[2] = currentX - 1;
        y[2] = currentY + 1;
    }

    for (int i=0;i < 3;i++)
    {
        HashMap h = new HashMap ();
        h.put ("X", new Integer (getNextX (x[i], direction)));
        h.put ("Y", new Integer (getNextY (y[i], direction)));

        lookAheads.add (h);
    }

    return lookAheads;
}

I didn't test the syntax of the methods (I just wrote them in a notepad), so pardon me if there are some compilation errors, but you should be able to figure that out.

Hope that helps.




回答4:


This can be solved quite simply, by using the modulus operator (%). Modulo cycles values around under a certain upper bound. So if the robot's x value steps over the maximum boundary, it simply jumps back to 0. This way the robot can move through one wall on the right, and the x co-ordinate is reset back to 0 and they will appear on the left side of the stage.



来源:https://stackoverflow.com/questions/15096066/connecting-a-maze-grids-walls-so-all-are-interconnected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!