2010-11-20 57 views
1

我正在製作一個機器人,它能夠在迷宮中將閃光燈鰭片放回到原來的位置。當我測試我的代碼時,機器人會發現閃光器,但後來卡住了,不回到它的起始位置。我已經編寫了其他方法來使程序更簡單,包括檢查機器人是否訪問過下一個路口的方法,計算下一條街道和街道的另一種方法,以及將機器人轉向指定方向的方法。 下面是完整的代碼:機器人將不會返回到之前的位置

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--; 
       } 
    } 
} 

我想用代碼的主要問題是在本節:

//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循環?

+0

deedex,你的代碼提到機器人應該在沒有找到閃爍器的情況下返回到開始。這是否工作? – 2010-11-20 21:16:44

+0

發生什麼事是機器人前進然後在兩個交叉點之間來回切換(不在起始位置) – deedex11 2010-11-20 21:38:52

回答

1

您應該創建一個堆棧,您在迷宮中遍歷的所有點,然後一旦到達目的地,將每個點彈出堆棧。這將有效地讓您回溯您的步驟,並且很容易編入您當前的代碼。這比使用ArrayList容易得多...

+0

我是Java編程的新手,所以我不確定如何使用堆棧。還有人指出我需要使用ArrayLists來完成這個程序。 – deedex11 2010-11-20 21:46:11

+0

只有一個線程訪問它時,ArrayList的順序纔是可預測的。您可以從ArrayList中導出數組,然後像Trevor所說的那樣進行操作,或者從ArrayList中獲取Iterator並向後迭代它。 – 2010-11-20 21:50:49

+0

是的,用循環向後遍歷ArrayList。我建議在機器人移動之前標出起始位置,以便當您想要回溯時使用起點作爲循環的停止標準 – 2010-11-21 00:17:32

相關問題