无法阻止我的机器人的运动自行恢复



我有一个相当标准的ascii迷宫,里面有一个机器人,它可以在棋盘上移动,在找到出口并离开(获胜!)之前获得所需数量的黄金。然而,我似乎无法阻止机器人重新振作起来,因此获胜所需的时间太长了。

例如,如果机器人刚刚向南移动,那么我不希望它在那之后立即向北移动。

我知道它可以更高效,但不要告诉我如何改进机器人,只告诉我如何阻止运动自行恢复。

response.equals("FAIL")表示移动是否会将机器人带到墙上(用#表示)。

public static void randomMove(){
String reponse = "FAIL";
int lastrand = -1;
int rand = -1;
while(reponse.equals("FAIL")){      
lastrand = rand;
rand = new Random().nextInt(4);
if(rand==0 && lastrand != 1){
reponse = robot.Move("N",'B');
}else if(rand==1 && lastrand != 0){
reponse = robot.Move("S",'B');
}else if(rand==2 && lastrand != 3){ 
reponse = robot.Move("E",'B');
}else if(rand==3 && lastrand != 2){ 
reponse = robot.Move("W",'B');
}else{
continue;
}
lastrand = rand;
}
if(robot.getNewSquare()=='G'){
robot.print(robot.Pickup());
}
if(reponse == "Winner!"){
robot.print(reponse);
robot.Quit();
}
}

您添加else continue时似乎有"如果我们不移动,就不要更新lastrand"的想法。但是,在循环开始时仍然有一个lastrand = rand;,它无论如何都会更新它。所以,如果你连续两次随机进入北方,它就会朝着这个方向发展。

最新更新