首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >机器人不会回到以前的位置

机器人不会回到以前的位置
EN

Stack Overflow用户
提问于 2010-11-20 20:57:37
回答 1查看 2.3K关注 0票数 1

我正在制造一个机器人,它能在迷宫中给闪光器装上鳍,并把它带回原来的位置。当我测试我的代码时,机器人会找到闪光器,但是会被卡住,不会回到它的起始位置。我还编写了其他方法来简化程序,它们包括检查机器人是否访问了下一个交叉口的方法,计算下一条道路和街道的另一种方法,以及将机器人转向的方法是一个指定的方向。以下是完整的代码:

代码语言:javascript
运行
复制
import becker.robots.Robot;
import becker.robots.City;
import becker.robots.Direction;
import becker.robots.Intersection;
import java.util.ArrayList;

/* This program has a robot search a maze for a flasher, picks the flasher up, and
returns back to it's starting position. If no flasher is found, it will return back 
to the starting position as well.
*/

public class RobotUtils{
    Robot meow;
    /* This method says what avenue the robot will be on if it decides to move
    one intersection in the direction it's facing
    */
    public static int calculateNextAvenue(Robot meow){
        int avenue = meow.getAvenue();
        Direction direction = meow.getDirection();
        //Changes the value of the avenue the robot is on when facing west
        if (direction == Direction.WEST){
            return avenue - 1;
        }
        //Changes the value of the avenue the robot is on when facing east
        if (direction == Direction.EAST){
            return avenue + 1;
        }
        //If facing north or south, doesn't change the value
        else{
            return avenue;
        }
    }
    /* This method says what street the robot will be on if it decides to move
    one intersection in the direction it's facing
    */
    public static int calculateNextStreet(Robot meow){
        int street = meow.getStreet();
        Direction direction = meow.getDirection();
        //Changes the value of the avenue the robot is on when facing south
        if (direction == Direction.SOUTH){
            street = street + 1;
        }
        //Changes the value of the avenue the robot is on when facing north
        if (direction == Direction.NORTH){
            street = street - 1;
        }
        //If facing east or west, doesn't change the value
        return street;
    }
    //This method turns the robot to face a certain specified direction
    public static void turnRobot(Robot meow, Direction face){
        Direction direction = meow.getDirection();
        while (direction != face){
            meow.turnLeft();
            direction = meow.getDirection();
        }
    }
    //This method checks a list to see if a certain intersection is in the list
    public static boolean isVisited(ArrayList<Intersection> search , int
        street, int avenue){
        Intersection inter;
        boolean found = false;
        int i = 0;
        while (found == false && i < search.size()){
            inter = search.get(i);
            if (inter.getAvenue()== avenue && 
                inter.getStreet() == street){
                found = true;
            }
            i++;
        }
        return found;
    }
    //This method makes the robot search a maze, find a flasher if there is
    //one, pick the flasher up, and return back to its starting position.
    public static void retrieveLight(Robot meow){
        ArrayList<Direction> path;
        path = new ArrayList<Direction>();
        ArrayList<Intersection> intersections;
        intersections = new ArrayList<Intersection>();
        boolean searched = false;
        boolean flasher = false;
        int street = meow.getStreet();
        int avenue = meow.getAvenue();
        Intersection current = meow.getIntersection();

        //Adds the current intersection the robot is on to a list that
        //keeps track of where the robot has been
        while (searched == false  && flasher == false){
            Direction d = meow.getDirection();
            if (isVisited(intersections, street, avenue) == false){
                intersections.add(current);
            }
            //Robot picks flasher up
            if (meow.canPickThing()){
                flasher = true;
                meow.pickThing();
            }
            //Makes the robot move towards intersections it hasn't visited yet
            //to search the maze to find the flasher
            else {
    //Robot moves if an adjacent intersection hasn't been visited yet
    if (!isVisited(intersections, calculateNextStreet(meow),
    calculateNextAvenue(meow)) && meow.frontIsClear()){
            path.add(d);
            turnRobot(meow, d);
            meow.move();            
    }           
    //If path is blocked or intersection has been visited, 
    //robot turns to look for new direction to move in
        if (isVisited(intersections,  calculateNextStreet(meow),
        calculateNextAvenue(meow)) || !meow.frontIsClear()){
                        meow.turnLeft();
                    }
    //If the robot has searched the entire maze without
    //finding flasher, exits the loop
        else if (path.isEmpty()){
        searched = true;
    }
    //If all intersections around robot have been visited/are blocked,
    //robot back tracks to find a new intersection to visit
        else {
        int last = path.lastIndexOf(d);
        path.remove(last);
        turnRobot(meow, d.opposite());
        meow.move();
            }           
            }
        }
        //Have robot go back to start by backtracking all intersections it
        //has visited
            int i = path.size();
            while (i >= 0 && isVisited(intersections, 
            calculateNextStreet(meow),calculateNextAvenue(meow))){
                Direction d = meow.getDirection();
                int last = path.lastIndexOf(d);
                path.remove(last);
                turnRobot(meow, d.opposite());
                meow.move();
                i--;
                } 
    }
}

我认为代码的主要问题在本节:

代码语言:javascript
运行
复制
//Makes the robot move towards intersections it hasn't visited
//to search the maze to find the flasher
else {
//Robot moves if an adjacent intersection hasn't been visited yet
if (!isVisited(intersections, calculateNextStreet(meow),
  calculateNextAvenue(meow)) && meow.frontIsClear()){
    path.add(d);
    turnRobot(meow, d);
    meow.move();            
}           
//If path is blocked or intersection has been visited, 
//robot turns to look for new direction to move in
    if (isVisited(intersections, calculateNextStreet(meow),
     calculateNextAvenue(meow)) || !meow.frontIsClear()){
        meow.turnLeft();
    }
//If the robot has searched the entire maze without
//finding flasher, exits the loop
    else if (path.isEmpty()){
        searched = true
        }
//If all intersections around robot have been visited/are blocked,
//robot back tracks to find a new intersection to visit
    else {
    int last = path.lastIndexOf(d);
    path.remove(last);
    turnRobot(meow, d.opposite());
    meow.move();
    }           
                }
            }
//Have robot go back to start by backtracking all intersections it
//has visited
                int i = path.size();
                while (i >= 0 && isVisited(intersections, 
                calculateNextStreet(meow),calculateNextAvenue(meow))){
                    Direction d = meow.getDirection();
                    int last = path.lastIndexOf(d);
                    path.remove(last);
                    turnRobot(meow, d.opposite());
                    meow.move();
                    i--;
                    } 
        }
    }

如果你能帮我,我会非常感激的。

编辑,我的机器人一旦找到闪光灯,就一直撞到墙上,有什么想法吗?我应该如何终止最后一个while循环?

EN

回答 1

Stack Overflow用户

发布于 2010-11-20 21:44:24

你应该创建一个你在迷宫中穿过的所有点的堆栈,然后一旦你到达你的目的地,“弹出”堆栈中的每一个点。这将有效地使您能够追溯您的步骤,并且非常容易将代码编码到当前代码中。它比使用ArrayList容易得多.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4234980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档