Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.39 KB | None | 0 0
  1. import uk.ac.warwick.dcs.maze.logic.IRobot;
  2. import java.util.Stack;
  3.  
  4. public class Explorer2 {
  5.  
  6.     private RobotData2 robotData;
  7.     private int pollRun = 0;
  8.     private int explorerMode; //1 = explore, 0 = backtrack
  9.     private Stack<RobotData2> junctionRecorder = new Stack<RobotData2>();
  10.    
  11.     public void controlRobot(IRobot robot) {
  12.  
  13.         //If the robot is surrounded by paths it has been to before it should backtrack
  14.         if (beenBeforeExits(robot) == nonWallExits(robot))
  15.             explorerMode = 0;
  16.         //If it can go down a new path set the robot to explore
  17.         else   
  18.             explorerMode = 1;
  19.        
  20.         if (junctionRecorder.isEmpty() == false)   
  21.             //System.out.println(robotData.getX(junctionRecorder.peek()) + ", " + robotData.getY(junctionRecorder.peek()));
  22.             System.out.println(junctionRecorder);
  23.            
  24.         if (explorerMode == 1)
  25.             exploreControl(robot);
  26.         else
  27.             backtrackControl(robot);   
  28.     }
  29.    
  30.     private void exploreControl(IRobot robot) {
  31.         int exits = nonWallExits(robot);
  32.         int direction;
  33.        
  34.         //Read how many exits there are and pick the corresponding method depending on how many there are
  35.         switch (exits) {
  36.             case 1:
  37.                 direction = deadEnd(robot);
  38.                 break;
  39.             case 2:
  40.                 direction = twoWalls(robot);
  41.                 break;
  42.             default:
  43.                 direction = junction(robot);
  44.                 //Record the junction the robot is at an the direction it has come from
  45.                 if (beenBeforeExits(robot) == 1)
  46.                     robotData = new RobotData2(robot.getLocation().x, robot.getLocation().y, robot.getHeading());
  47.                     junctionRecorder.push(robotData);
  48.                 break;
  49.         }
  50.         robot.face(direction);
  51.     }
  52.    
  53.     private void backtrackControl(IRobot robot) {
  54.         int exits = nonWallExits(robot);
  55.         int direction;
  56.         //Read how many exits there are and pick the corresponding method depending on ho many there are
  57.         switch (exits) {
  58.             case 1:
  59.                 direction = deadEnd(robot);
  60.                 break;
  61.             case 2:
  62.                 direction = twoWalls(robot);
  63.                 break;
  64.             default:
  65.                 //The robot should go back down the path it originally arrived from and remove the junction from the memory
  66.                 robot.setHeading(searchJunction(robot));
  67.                 RobotData2 junc = junctionRecorder.pop();
  68.                 direction = IRobot.BEHIND;
  69.                 break;
  70.         }
  71.         robot.face(direction);
  72.     }
  73.    
  74.     private int searchJunction(IRobot robot) {
  75.         //Return the heading that the robot had when it arrived at the junction
  76.         return robotData.getHeading(junctionRecorder.peek());
  77.     }
  78.    
  79.     //Method for calculating how many exits there are around the robot
  80.     private int nonWallExits(IRobot robot) {
  81.         int exits = 0;
  82.         //Read how many exits there are around the robot and add them to the variable 'exits'
  83.         if (robot.look(robot.AHEAD) != IRobot.WALL)
  84.             exits = exits + 1;
  85.         if (robot.look(robot.BEHIND) != IRobot.WALL)
  86.             exits = exits + 1;
  87.         if (robot.look(robot.LEFT) != IRobot.WALL)
  88.             exits = exits + 1;
  89.         if (robot.look(robot.RIGHT) != IRobot.WALL)
  90.             exits = exits + 1;
  91.         return exits;
  92.     }
  93.    
  94.     //Method for calculating how many passages are around the robot
  95.     private int passageExits(IRobot robot) {
  96.         int passages = 0;
  97.         //Read how many passage exits there are around the robot and add them to the variable 'passages'
  98.         if (robot.look(robot.AHEAD) == IRobot.PASSAGE)
  99.             passages = passages + 1;
  100.         if (robot.look(robot.BEHIND) == IRobot.PASSAGE)
  101.             passages = passages + 1;
  102.         if (robot.look(robot.LEFT) == IRobot.PASSAGE)
  103.             passages = passages + 1;
  104.         if (robot.look(robot.RIGHT) == IRobot.PASSAGE)
  105.             passages = passages + 1;
  106.         return passages;
  107.     }
  108.    
  109.     //Method for calculating how many exits are around the robot that have already been explored
  110.     private int beenBeforeExits(IRobot robot) {
  111.         int been = 0;
  112.         //Read how many been before exits there are around the robot and add them to the variable 'been'
  113.         if (robot.look(robot.AHEAD) == IRobot.BEENBEFORE)
  114.             been = been + 1;
  115.         if (robot.look(robot.BEHIND) == IRobot.BEENBEFORE)
  116.             been = been + 1;
  117.         if (robot.look(robot.LEFT) == IRobot.BEENBEFORE)
  118.             been = been + 1;
  119.         if (robot.look(robot.RIGHT) == IRobot.BEENBEFORE)
  120.             been = been + 1;
  121.         return been;
  122.     }
  123.    
  124.     //Method for picking a direction at a dead end
  125.     private int deadEnd(IRobot robot) {
  126.         int direction;
  127.         //Pick the only direction the robot can go
  128.         direction = randomDirection(robot);
  129.         return direction;
  130.     }
  131.    
  132.     //Method for picking a direction in a corridor or at a corner
  133.     private int twoWalls(IRobot robot) {
  134.         int direction;
  135.         //If all paths have previously been explored, pick a random direction
  136.         if (passageExits(robot) == 0)
  137.             direction = randomDirection(robot);
  138.         //If there is a passage available, go down the passage'
  139.         else {
  140.             do {
  141.                 direction = randomDirection(robot);
  142.             } while(robot.look(direction) == IRobot.BEENBEFORE);
  143.         }
  144.         return direction;
  145.     }
  146.    
  147.     //Method for picking a direction at a junction and crossroads
  148.     private int junction(IRobot robot) {
  149.         int direction;
  150.         //If all paths have previously been explored, pick a random direction
  151.         if (passageExits(robot) == 0)
  152.             direction = randomDirection(robot);
  153.         //If there are 1 or more passages available, pick randomly between the available passages
  154.         else {
  155.             do {
  156.                 direction = randomDirection(robot);
  157.             } while(robot.look(direction) == IRobot.BEENBEFORE);
  158.         }
  159.         return direction;
  160.     }
  161.    
  162.     //Method for picking a direction at random with equal probability
  163.     private int randomDirection(IRobot robot) {  
  164.         int direction;
  165.         double randno;
  166.         do {
  167.             // Select a random number with equal probability
  168.             randno = (Math.random()*4);
  169.             if (randno >= 0 && randno <= 1)
  170.                 direction = IRobot.LEFT;
  171.             else if (randno > 1 && randno <= 2)
  172.                 direction = IRobot.RIGHT;
  173.             //Only pick the direction behind if the robot is at a dead end
  174.             else if (randno > 2 && randno <= 3 && nonWallExits(robot) == 1)  
  175.                 direction = IRobot.BEHIND;
  176.             else
  177.                 direction = IRobot.AHEAD;
  178.         } while(robot.look(direction) == IRobot.WALL);  //Keep repeating until a direction is found that doesn't lead to a wall
  179.         return direction;
  180.     }
  181. }
  182.  
  183. class RobotData2 {
  184.    
  185.     int x;
  186.     int y;
  187.     int heading;
  188.  
  189.     public RobotData2(int x, int y, int heading) {
  190.         this.x = x;
  191.         this.y = y;
  192.         this.heading = heading;
  193.     }
  194.    
  195.     //Gets the x coordinate of the object
  196.     public int getX(RobotData2 junction) {
  197.         return junction.x;
  198.     }
  199.    
  200.     //Gets the y coordinate of the object
  201.     public int getY(RobotData2 junction) {
  202.         return junction.y;
  203.     }
  204.    
  205.     //Gets the heading of the object
  206.     public int getHeading(RobotData2 junction) {
  207.         return junction.heading;
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement