问题描述
我有一个二维网格,我想创建所有的墙壁之间的联系。
网格状构造:
格=新的国家[8] [8]。
的for(int i = 0; I< 8;我++){
对于(INT J = 0; J< 8,J ++){
电网[I] [J] = State.blank;
}
}
我有一个机器人,应该能够通过的壁传递到相对侧像上的游戏蛇
因此,例如,如果机器人朝北并且在位置x [0] Y [1],那么它应该连接到x [7] Y [1]。
机器人还应该能够在它前面的三个块中读取最新,一到左边,一到右边,一个直接前方
#X =空
#R =机器人
#S =空间机器人传感器拾取
如果它是朝北这是机器人便拿起:
[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语句长度块,但有太多的possiblities覆盖所有没有要疯了!
我也记下的更改X'放大器;当放置在特定的情况下,Y对纸,但我实在看不出这暗示对算法任何图案。
任何帮助将是AP preciated!
公共类机器人{
公众诠释X;
公众诠释Ÿ;
公共机器人(INT X,int y)对{
this.x = X;
this.y = Y;
}
公共无效移动(INT方向,诠释步骤){
开关(方向){
案例1://北
INT temp1中=(X-步)8%;
X = temp1中℃,(temp1中+ 8):的temp1;
打破;
案例2://南
X =(X +步骤)%8;
打破;
案例3://西
INT TEMP3 =(Y-步)8%;
Y = TEMP3℃,(TEMP3 + 8):TEMP3;
打破;
壳4://东
Y =(Y +步)8%;
打破;
默认:
的System.out.println(我不是足够聪明来处理所提供的direciton!);
}
}
公共静态无效的主要(字串[] args){
INT [] [] =格新INT [8] [8];
机器人机器人=新的机器人(0,0);
的System.out.println(我开始在(0,0)。);
robot.move(3,9);
的System.out.println(我西临9步动。);
的System.out.println(我已经到达(+ robot.x +,+ robot.y +)。);
}
}
望code以上给出了一个想法。我测试过它。随意一试。三个块在机器人前面的计算是相似的。你自己看着办吧你自己了。
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!
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.
这篇关于连接迷宫/网格的墙壁等等都是相互关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!