Java 连接迷宫/网格';所以所有的墙都是相互连接的

Java 连接迷宫/网格';所以所有的墙都是相互连接的,java,algorithm,game-physics,Java,Algorithm,Game Physics,我有一个二维网格,我正在尝试在所有墙之间创建链接 网格的构造如下所示: grid = new State[8][8]; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { grid[i][j] = State.blank; } } 如果它朝北,这就是机器人将拾取的内容: [S][S][S][x][x][x][x][x] [x][R][x

我有一个二维网格,我正在尝试在所有墙之间创建链接

网格的构造如下所示:

    grid = new State[8][8];
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            grid[i][j] = State.blank;
        }
    }
如果它朝北,这就是机器人将拾取的内容:

[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]
[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]
像怀斯一样,如果机器人面向东方,它将拾取以下信息:

[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]
[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]
我面临的问题是找到正确的算法,以确保机器人不仅能穿过墙壁,还能读取穿过墙壁的传感器

如果机器人在左上角,面向北方,那么它会像这样在墙上读:

[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]
你可以想象,我已经尝试过做大量的IF语句,但是有太多的可能性来涵盖它们而不会发疯

在某些情况下,我也在纸上写下了对X&Y的更改,但我看不到任何暗示算法的模式


任何帮助都将不胜感激

这可以非常简单地通过使用模运算符(%)来解决。在某个上限下循环值。因此,如果机器人的x值跨过最大边界,它只会跳回到0。通过这种方式,机器人可以穿过右侧的一面墙,x坐标被重置为0,它们将出现在舞台的左侧。

使用迭代器,如
x=(x+1)%array。长度


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

我将尝试将其分解为不同的部分,希望这将对您有所帮助。 因此,您有一个8x8网格,由X和Y坐标表示,(0,0)是左上角,(7,7)是右下角。 您的算法如下所示:

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
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;
}
现在,如果我将此算法转换为java方法,它们将如下所示:

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
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;
}
public int getNextX(int currentX,字符串方向)
{
如果(N.等于(方向)| |“S.等于(方向))
{
返回电流x;
}
否则,如果(“E”。等于(方向))
{
返回((currentX==7)?0:currentX+1);
}
如果(“W”等于(方向))
{
返回((currentX==0)?7:currentX-1;
}
}
public int getNextY(int currentY,字符串方向)
{
如果(“E”。等于(方向)| |“W”。等于(方向))
{
返回电流;
}
否则,如果(“S”等于(方向))
{
返回((当前Y==7)?0:currentY+1);
}
如果(“N”等于(方向))
{
返回值((当前Y==0)?7:currentY-1;
}
}
公共ArrayList getLookAheads(int-currentX、int-currentY、字符串方向)
{
ArrayList lookAheads=新的ArrayList();
int x[3];
int-y[3];
如果(“N”。等于(方向))
{
//LH1
x[0]=当前x;
y[0]=当前y-1;
//LH2
x[1]=当前x-1;
y[1]=当前y-1;
//LH3
x[2]=电流x+1;
y[2]=当前y-1;
} 
否则,如果(“S”等于(方向))
{
//LH1
x[0]=当前x;
y[0]=当前y+1;
//LH2
x[1]=当前x-1;
y[1]=电流y+1;
//LH3
x[2]=电流x+1;
y[2]=电流y+1;
} 
否则,如果(“E”。等于(方向))
{
//LH1
x[0]=当前x+1;
y[0]=当前y;
//LH2
x[1]=电流x+1;
y[1]=当前y-1;
//LH3
x[2]=电流x+1;
y[2]=电流y+1;
} 
否则,如果(“E”。等于(方向))
{
//LH1
x[0]=当前x-1;
y[0]=当前y;
//LH2
x[1]=当前x-1;
y[1]=当前y-1;
//LH3
x[2]=当前x-1;
y[2]=电流y+1;
}
对于(int i=0;i<3;i++)
{
HashMap h=新的HashMap();
h、 put(“X”,新整数(getNextX(X[i],方向));
h、 put(“Y”,新整数(getNextY(Y[i],方向));
lookAheads.add(h);
}
回头看;
}
我没有测试这些方法的语法(我只是把它们写在记事本上),所以如果有一些编译错误,请原谅,但您应该能够找到答案

希望有帮助。

公共类机器人{
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+").");
    }
}
公共int x; 公共智力; 公共机器人(整数x,整数y){ 这个.x=x; 这个。y=y; } 公共无效移动(整数方向,整数步数){ 开关(方向){ 案例1://北 int temp1=(x步)%8;
x=temp1我很困惑。如果你在坐标[0][0]的一个角落,你向左移动([-1][0]),在使用上述迭代器之后,你的新坐标应该是[9][0]。这就是你想要的,是吗?用y替换x,然后向上移动,它变成[0][9]。如果你添加对角线移动,它变成[9][9]使用左上角,但面向北方的例子,如果你搜索机器人位置[x-1][y-1],[x][y-1],[x+1][y-1],你会使用模迭代器得到你的标记网格。谢谢,伙计,我已经完成了今天的编码,但会告诉你我是怎么做的!谢谢Terry,它可以工作,但我不懂语法“?”我以前从没见过像你这样的地方"y=temp3@silverzx这就是所谓的。传感器部分的提示:你通过向前一步直接到达传感器,你通过向前一步和向左一步到达左边的传感器,你对右边的传感器做类似的操作。你可以重复使用我提供的
move
方法,方法是做一些小的修改奥斯。