Facebook
From Atika, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 169
  1. /**
  2.  * In this class we  determine the path that Freddy the Frog must follow to go from the
  3.  * starting cell to the end cell, where Franny is.
  4.  * @author Atika Hussain
  5.  
  6.  **/
  7. public class FrogPath {
  8.  
  9.  private Pond pond;
  10.  
  11.  public FrogPath(String filename) {
  12.   try {
  13.    pond = new Pond(filename);
  14.   }
  15.  
  16.   catch (Exception e) {
  17.    System.out.println("Error");
  18.   }
  19.  
  20.  }
  21.  
  22. /**
  23.  * Finds the best route for Freddy to get to Franny by checking priority cells
  24.  * @param currCell
  25.  * @return returns the lowest value from priority Queue
  26.  **/
  27.  public Hexagon findBest(Hexagon currCell) {
  28.  
  29.   ArrayUniquePriorityQueue<Hexagon> priorityQueue = new ArrayUniquePriorityQueue<>();
  30.  
  31.          for (int i = 0; i < 6; i++) {
  32.              Hexagon neighbor = currCell.getNeighbour(i);
  33.              
  34.              if(neighbor != null && correctCell(neighbor)) {
  35.                  priorityQueue.add(neighbor, priorityFinder(neighbor));
  36.              }
  37.          }
  38.      
  39.      
  40.       if ( currCell.isStart() || currCell.isLilyPadCell() ) {
  41.        
  42.        for(int j = 0; j< 6; j++) {
  43.         Hexagon neighbor = currCell.getNeighbour(j);
  44.        
  45.         if(neighbor != null) {
  46.          for (int k = 0; k < 6; k++) {
  47.                  Hexagon newCell = neighbor.getNeighbour(k);
  48.                  
  49.                  if (newCell != null && correctCell(newCell)) {
  50.                   priorityQueue.add(newCell, calculatePriority(newCell, currCell));
  51.                  }
  52.                }
  53.          
  54.            }
  55.        }
  56.       }
  57.          
  58.       if(priorityQueue.isEmpty()) {
  59.        return null;
  60.  
  61.       }  
  62.      
  63.      return priorityQueue.removeMin() ;
  64.  }
  65.  
  66.  
  67. /**
  68.  * This helper method checks if Freddy can enter the current cell and whether its a valid cell or not.
  69.  * @param currCell
  70.  * @return true is the if the cell is correct
  71.  **/
  72.  
  73.  private boolean correctCell(Hexagon currCell) {
  74.      if (currCell == pond.getStart()) {
  75.          return false;
  76.      }
  77.        
  78.      if (currCell.isReedsCell()) {
  79.             return true;
  80.      }
  81.      
  82.      for (int i = 0; i < 6; i++) {
  83.          try {
  84.              if (currCell.getNeighbour(i).isAlligator()) return false;
  85.          }
  86.          catch (Exception e) {
  87.  
  88.          }
  89.  
  90.      if (currCell.isAlligator() || currCell.isMudCell() || currCell.isMarked()) {
  91.       return false;
  92.       }
  93.      
  94.      }
  95.      
  96.         return true;
  97.  
  98.     }
  99.  
  100.  
  101. /**
  102.  * This helper method find the priority of each cell and returns the priority of the current cell
  103.  * @param currCell
  104.  * @return it returns the priority based on the cell
  105.  **/
  106.  private double priorityFinder(Hexagon currCell) {
  107.  
  108.      int numFlies = getNumFlies(currCell);
  109.      double priority = getBasePriority(numFlies);
  110.  
  111.      if (currCell.isEnd()) {
  112.          priority = 3.0;
  113.      }
  114.      
  115.      else if (currCell.isReedsCell()) {
  116.          priority = 5.0;
  117.      }
  118.      
  119.      else if (currCell.isLilyPadCell()) {
  120.          priority = 4.0;
  121.      }
  122.      
  123.      else if (currCell.isWaterCell()) {
  124.          priority = 6.0;
  125.      }
  126.      
  127.      else if (currCell.isReedsCell() && nearAlligator(currCell)) {
  128.          priority = 10.0;
  129.      }
  130.  
  131.      return priority;
  132.      
  133.  }
  134.  
  135. /**
  136.  * This helper method find the number of flies in a cell
  137.  * @param currCell
  138.  * @return the number of flies
  139.  **/
  140.  
  141.  private int getNumFlies(Hexagon currCell) {
  142.  
  143.      int numFlies = 0;
  144.      
  145.      if (currCell instanceof FoodHexagon) {
  146.      
  147.           FoodHexagon foodHexag currCell;
  148.          numFlies = foodHexagon.getNumFlies();
  149.      }
  150.      
  151.      return numFlies;
  152.  }
  153.  
  154. /**
  155.  * This helper method returns the priority of each cell given for the
  156.  * number of flies in that cell
  157.  * @param numFlies
  158.  * @return basePriority based on the numebr of cells
  159.  **/
  160.  private double getBasePriority(int numFlies) {
  161.  
  162.      double basePriority = 0.0;
  163.      
  164.      if (numFlies == 3) {
  165.          basePriority = 0.0;
  166.      }
  167.      
  168.      else if (numFlies == 2) {
  169.          basePriority = 1.0;
  170.      }
  171.      
  172.      else if (numFlies == 1) {
  173.          basePriority = 3.0;
  174.      }
  175.  
  176.      return basePriority;
  177.  }
  178.  
  179.  
  180.  
  181. /**
  182.  * This helper method adds priorities for the cells which are straight
  183.  * @param cell , currCell
  184.  * @return addPriority based on if the cell is straight
  185.  **/
  186.  
  187.  private double calculatePriority(Hexagon cell, Hexagon currCell) {
  188.  
  189.         double addPriority = priorityFinder(cell);
  190.  
  191.   int condition;
  192.  
  193.   if (Straight(cell, currCell)) {
  194.        c
  195.   }
  196.  
  197.   else {
  198.        c
  199.   }
  200.  
  201.       if (c 0) {
  202.          addPriority += 0.5;
  203.         }
  204.      
  205.          else if (c 1) {
  206.          addPriority +=1.0;
  207.         }
  208.        
  209.         return addPriority;
  210.        
  211.         }
  212.  
  213.  /**
  214.   * this helper method checks the straight cell
  215.   * @param cell, currCell
  216.   * @return
  217.   **/
  218.  
  219.  private boolean Straight(Hexagon cell, Hexagon currCell) {
  220.    
  221.   int check = 0;
  222.  
  223.   for (int i = 0; i < 6; i++) {
  224.    
  225.       for (int j = 0; j < 6; j++) {
  226.        
  227.           if (cell.getNeighbour(i) == currCell.getNeighbour(j)) {
  228.               check++;
  229.              
  230.               if (check > 1) {
  231.                   break;
  232.               }
  233.           }
  234.       }
  235.      
  236.       if (check > 1) {
  237.           break;
  238.       }
  239.   }
  240.  
  241.   return check == 1;
  242.  
  243.   }
  244.  
  245. /**
  246.  * This helper method checks for reed cells near alligator
  247.  * @return true or false if the adjacent cell to alligators is a reed cell
  248.  **/
  249.  
  250.  
  251.   public boolean nearAlligator(Hexagon currCell) {
  252.  
  253.    boolean nearAlligator = false;
  254.    
  255.       for (int i = 0; i < 6 && !nearAlligator; i++) {
  256.           Hexagon neighbor = currCell.getNeighbour(i);
  257.          
  258.           if (neighbor != null && neighbor.isAlligator()) {
  259.               nearAlligator = true;
  260.           }
  261.       }
  262.      
  263.       return nearAlligator;
  264.  
  265.   }
  266.    
  267.  
  268. /**
  269.  * This method keeps track of the cells that the frog has visited in its
  270.  * path from the starting cell toward the end cell.
  271.  * @return
  272.  **/
  273.  
  274.     public String findPath() {
  275.      ArrayStack<Hexagon> S = new ArrayStack<>();
  276.      
  277.      int fliesEaten = 0;
  278.  
  279.      
  280.      Hexagon start = pond.getStart();
  281.      
  282.      
  283.      S.push(start);
  284.      start.markInStack();
  285.  
  286.  
  287.      StringBuilder pathBuilder = new StringBuilder();
  288.  
  289.      while (!S.isEmpty()) {
  290.      
  291.          Hexagon curr = S.peek();
  292.          pathBuilder.append(curr.getID()).append(" ");
  293.  
  294.          if (curr.isEnd()) {
  295.              break;
  296.          }
  297.  
  298.          
  299.          if (curr instanceof FoodHexagon) {
  300.          
  301.           int numFlies = ((FoodHexagon)curr).getNumFlies();
  302.           if (numFlies > 0) {
  303.            
  304.            fliesEaten = fliesEaten + numFlies;
  305.            ((FoodHexagon)curr).removeFlies();
  306.           }
  307.          }
  308.  
  309.          Hexagon next = findBest(curr);
  310.  
  311.          if (next == null) {
  312.              S.pop();
  313.              curr.markOutStack();
  314.              
  315.          } else {
  316.              S.push(next);
  317.              next.markInStack();
  318.  
  319.          }
  320.      }
  321.  
  322.      if (S.isEmpty()) return "No solution";
  323.      
  324.         return pathBuilder.toString() + "ate " + fliesEaten + " flies";
  325.  
  326.     }
  327.  
  328.  
  329.    
  330.  
  331.  
  332.     public static void main (String[] args) {
  333.       if (args.length != 1) {
  334.       System.out.println("No map file specified in the arguments");
  335.       return;
  336.       }
  337.       FrogPath fp = new FrogPath(args[0]);
  338.       Hexagon.TIME_DELAY = 500; // Change this time delay as desired.
  339.       String result = fp.findPath();
  340.       System.out.println(result);
  341.      }
  342. }
  343.