我正在开发一个机器人迷宫,机器人在这里找到目标而不会撞到墙上。我知道我错过了什么或做错了什么(很可能是几件事哈哈)但是我现在已经把我的大脑搁置了几个小时,并尝试了几种替代品。我很确定我的错误是在哪里以及如何宣布ArrayList
。
找不到passageDirections
和nonwallDirections
的符号。
感谢任何帮助:)
PS:我是一名初学程序员,仍在学习如此解释你的答案,好像你要向三岁的孩子解释一样:)答案 0 :(得分:3)
您的问题在nonwallExits
方法中 - 在return
语句后您不能拥有任何代码,因为此语句会终止该方法。只需将return nonwallExits;
移动到方法的最后一行就可以了。
答案 1 :(得分:2)
例如,我在此方法中看到了一个问题:
/* Junction method states if there is more than one passage, it will randomly select one.
If there is no passage, it will randomly select a nonwall/BeenBefore direction. */
public int junction(IRobot robot) {
if (passageExits(robot) >= 1) {
int randomPassage = ((Math.random())*(passageDirections.length()));
passageDirections.get(randomPassage);
} else {
int randomNonwall = ((Math.random())*(nonwallDirections.length()));
nonwallDirections.get(randomNonwall);
}
}
在这里您使用列表'passageDirections'和'nonwallDirections'而不首先声明它们。我看到你已经在其他函数中将这些列表声明为 local 变量,这意味着只要函数正在执行它们就会存在它们。所以如果你想在其他函数中使用它们,你不应该以某种方式使它们全局化或将它们作为参数传递给它们......
如果不完全理解您的代码,我建议尝试做类似的事情,使代码“可编译”:
import uk.ac.warwick.dcs.maze.logic.IRobot;
import java.util.ArrayList;
import java.util.*;
public class Explorer2 {
private ArrayList<Integer> passageDirections = new ArrayList<Integer>();
private ArrayList<Integer> nonWallDirections = new ArrayList<Integer>();
private Random random = new Random();
public void controlRobot (IRobot robot) {
int exits = nonwallExits(robot);
int direction;
if (exits < 2) {
direction = deadEnd(robot);
} else if (exits == 2) {
direction = corridor(robot);
} else if (exits == 3) {
direction = junction(robot);
} else {
direction = crossroads(robot);
}
robot.face(direction);
}
/* Deadend method will make the robot turn around except from the beginning.
Because the robot can face any direction at the start, it will follow
the one and only passage it will detect. */
public int deadEnd (IRobot robot) {
if (passageExits(robot) == 0)
return IRobot.BEHIND;
else
return -1; //FIXME: return correct direction!!!
}
/* Corridor method will make the robot follow the one and only passage. */
public int corridor (IRobot robot) {
return -1; //FIXME: return correct direction!!!
}
/* Junction method states if there is more than one passage, it will randomly select one.
If there is no passage, it will randomly select a nonwall/BeenBefore direction. */
public int junction(IRobot robot) {
if (passageExits(robot) >= 1) {
int randomPassage = random.nextInt(passageDirections.size());
return passageDirections.get(randomPassage);
} else {
int randomNonwall = random.nextInt(nonWallDirections.size());
return nonWallDirections.get(randomNonwall);
}
}
/* Crossroads method states if there is more than one passage, it will randomly select one.
If there is no passage, it will randomly select a nonwall/BeenBefore direction. */
public int crossroads (IRobot robot) {
if (passageExits(robot) >= 1) {
int randomPassage = random.nextInt(passageDirections.size());
return passageDirections.get(randomPassage);
} else {
int randomNonwall = random.nextInt(nonWallDirections.size());
return nonWallDirections.get(randomNonwall);
}
}
//Calculates number of exits and stores the direction of the exits in an ArrayList
private int nonwallExits (IRobot robot) {
int nonwallExits = 4;
if(robot.look(IRobot.AHEAD) == IRobot.WALL)
nonwallExits--;
if(robot.look(IRobot.LEFT) == IRobot.WALL)
nonwallExits--;
if(robot.look(IRobot.RIGHT) == IRobot.WALL)
nonwallExits--;
if(robot.look(IRobot.BEHIND) == IRobot.WALL)
nonwallExits--;
for(int direction = IRobot.AHEAD; direction < IRobot.LEFT; direction++) {
if(robot.look(direction) != IRobot.PASSAGE)
nonWallDirections.add(direction);
}
return nonwallExits;
}
//Calculates number of passages and stores the direction of the passages in an ArrayList
private int passageExits (IRobot robot) {
int passageExits = 0;
if(robot.look(IRobot.AHEAD) == IRobot.PASSAGE)
passageExits++;
if(robot.look(IRobot.LEFT) == IRobot.PASSAGE)
passageExits++;
if(robot.look(IRobot.RIGHT) == IRobot.PASSAGE)
passageExits++;
for(int direction = IRobot.AHEAD; direction < IRobot.LEFT; direction++) {
if(robot.look(direction) == IRobot.PASSAGE)
passageDirections.add(direction);
}
return passageExits;
}
}
这样您可以将列表声明为实例变量,这意味着它们代表Explorer2对象的状态。现在,在Explorer2对象上运行的所有实例方法都可以访问this.passageDirections
和this.nonWallDirections
这些变量,其中this
指的是您当前正在“处理”的对象。 (如果没有含糊之处,可以跳过this.
部分)。