Raizekas

Untitled

Mar 7th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.99 KB | None | 0 0
  1. // Note: program might malfunction if any of heading/direction valeus will be equal to -1
  2. import uk.ac.warwick.dcs.maze.logic.IRobot;
  3. import java.util.*;
  4.  
  5. public class GrandFinale
  6. {
  7.   //Variables
  8.   private int pollRun = 0;  // how many steps are made
  9.   private List<Integer> allHeadings;
  10.  
  11.  
  12.   public void controlRobot(IRobot robot)
  13.   {
  14.     List<Integer> pExits = new ArrayList<Integer>();    // passage exits
  15.     List<Integer> nwExits = new ArrayList<Integer>();   // non-wall exits
  16.  
  17.     // If robot's first time in the maze
  18.     if (robot.getRuns() == 0)
  19.     {
  20.       // If it's the first step of the current run
  21.       if (pollRun == 0)
  22.       {
  23.     allHeadings = new ArrayList<Integer>();
  24.       }
  25.       findExits(robot, pExits, nwExits);        // find all current exits (passage and nonwall exits)
  26.       if (pExits.size() == 0)
  27.       {
  28.     backtrackControl(robot, pExits, nwExits);
  29.       }
  30.       else
  31.       {
  32.     exploreControl(robot, pExits, nwExits);
  33.       }  
  34.     }
  35.     else
  36.       robot.setHeading(allHeadings.get(pollRun));
  37.     pollRun++;
  38.    
  39.   }
  40.  
  41.   public void reset()
  42.   {
  43.     pollRun = 0;
  44.   }
  45.  
  46.   //---------------------
  47.   // Controller methods
  48.   //---------------------
  49.   /* Method controls the robot under exploring rules:
  50.    * 1. Never go back in corridor.
  51.    * 2. If in not-visited junction select random passage, if not present - random nonwall exit.
  52.    * 3. If in visited junction or deadend switch to backtrack controller
  53.    * Methods gets IRobot object
  54.    * Method faces the robot to the direction, does not return anything
  55.    */
  56.   private void exploreControl(IRobot robot, List<Integer> pExits, List<Integer> nwExits)
  57.   {    
  58.     // If deadend
  59.     if (nwExits.size() == 1)
  60.     {
  61.       // If it's not the first step
  62.       if (pollRun != 0)
  63.       {
  64.     robot.face(universalDeadend(robot));
  65.       }
  66.       else
  67.       {
  68.     robot.face(exploreJunction(robot, pExits, nwExits));
  69.       }
  70.     }
  71.     else
  72.       // If corridor
  73.       if (nwExits.size() == 2)
  74.       {
  75.     robot.face(universalCorridor(robot));
  76.       }
  77.       // If junction/crossroad
  78.       else
  79.       {
  80.     robot.face(exploreJunction(robot, pExits, nwExits));
  81.       }
  82.     allHeadings.add(robot.getHeading());
  83.      
  84.   }
  85.  
  86.   /* Method controls robot under backtracking rules:
  87.    * 1. If it's a deadend - turn around.
  88.    * 2. Never go back in a corridor
  89.    * 3. If it's a junction: pick random passage exit, if not present, go back to the direction, from which robot first came to that junction
  90.    * Method gets IRobot object
  91.    * Method faces the robot to the direction, does not return anything
  92.    */
  93.   private void backtrackControl(IRobot robot, List<Integer> pExits, List<Integer> nwExits)
  94.   {
  95.       robot.setHeading(reverseHeading(robot, allHeadings.get(allHeadings.size() - 1)));
  96.       allH
  97.   }
  98.  
  99.  
  100.   //---------------------
  101.   // Other methods
  102.   //---------------------
  103.   /* Method checks if given direction is a wall
  104.    * Method gets a IRobot object and a direction (int value)
  105.    * Method returns true if it's wall, false otherwise
  106.    */
  107.   private boolean isWall(IRobot robot, int direction)
  108.   {
  109.     return robot.look(direction) == IRobot.WALL;
  110.   }
  111.  
  112.   /* Method checks if given direction is a passage
  113.    * Method gets a IRobot object and a direction (int value)
  114.    * Method returns true if it's passage, false otherwise
  115.    */
  116.   private boolean isPassage(IRobot robot, int direction)
  117.   {
  118.     return robot.look(direction) == IRobot.PASSAGE;
  119.   }
  120.  
  121.   /* Method reverses heading
  122.    * Method gets IRobot object and heading to be reversed
  123.    * Method returns reversed heading (int value)
  124.    */
  125.   private int reverseHeading(IRobot robot, int h)
  126.   {
  127.     boolean isIncrease = false;     // do you need to +2 to reverse the heading or -2
  128.     for (int i = IRobot.NORTH; i <= IRobot.WEST; i++)
  129.     {
  130.       if (h + 2 == i)
  131.     isIncrease = true;
  132.     }
  133.     if (isIncrease)
  134.       return h + 2;
  135.     else
  136.       return h - 2;
  137.  
  138.   }
  139.   /* Method finds all the nonwall and passage exits (and their directions)
  140.    * Methods gets an IRobot object
  141.    * Methods does not return, however it changes value of pExits and nwExits (passage exits and nonwall exits))
  142.    */
  143.   private void findExits(IRobot robot, List<Integer> pExits, List<Integer> nwExits)
  144.   {
  145.     for(int i = IRobot.AHEAD; i <= IRobot.LEFT; i++)
  146.     {
  147.       if (!isWall(robot, i))
  148.       {
  149.     nwExits.add(i);
  150.     if (isPassage(robot, i))
  151.     {
  152.       pExits.add(i);
  153.     }
  154.       }
  155.     }    
  156.   }
  157.  
  158.   /* Picks random direction value from exits list
  159.    * Method gets List<int> object
  160.    * Method returns random value from the List
  161.    */
  162.   private int pickRandom(List<Integer> A)
  163.   {
  164.     Random rand = new Random();
  165.     return A.get(rand.nextInt(A.size()));
  166.   }
  167.  
  168.   // Actually deadend can be run by exploreJunction method, that way there would not be need to include special case for robot's first step
  169.   /* Method finds a direction when ANY robot is at deadend
  170.    * Methods gets a IRobot object
  171.    * Methods returns direction for robot to go (int value)
  172.    */
  173.   private int universalDeadend(IRobot robot)
  174.   {
  175.     return IRobot.BEHIND;
  176.   }
  177.  
  178.   /* Method finds a direction when ANY robot is at corridor (either straight path or corner)
  179.    * Methods gets a IRobot object
  180.    * Methods returns direction for robot to go (int value)
  181.    */
  182.   private int universalCorridor(IRobot robot)
  183.   {
  184.     for (int i = IRobot.AHEAD; i <= IRobot.LEFT; i++)
  185.     {
  186.       if ((!isWall(robot, i)) && (i != IRobot.BEHIND))
  187.     return i;
  188.     }
  189.     // If something goes wrong
  190.     return -1;
  191.   }
  192.  
  193.   /* Method finds direction that suits the Explorer conditions the best:
  194.    * 1. Pick random passage if present.
  195.    * 2. Pick random non wall exit.
  196.    * Method gets IRobot objeect
  197.    * Method returns direction for robot to go (int value)
  198.    */
  199.   private int exploreJunction(IRobot robot, List<Integer> pExits, List<Integer> nwExits)
  200.   {    
  201.     if(pExits.size() != 0)
  202.     {
  203.       return pickRandom(pExits);    
  204.     }
  205.     else
  206.     {
  207.       return pickRandom(nwExits);
  208.     }
  209.   }
  210. }
Add Comment
Please, Sign In to add comment