Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. import javafx.scene.control.Cell;
  2.  
  3. public class ShortestPath {
  4.    
  5.     CityMap cityMap;
  6.    
  7.     public ShortestPath(CityMap theMap) {
  8.         cityMap = theMap;
  9.     }
  10.    
  11.     public void findShortestPath() {
  12.         OrderedCircularArray<MapCell> list = new OrderedCircularArray<MapCell>();
  13.         MapCell beginning = cityMap.getStart();
  14.         list.insert(beginning,0);
  15.         beginning.markInList();
  16.        
  17.         boolean destinationFound = false;
  18.        
  19.         while(!list.isEmpty() && !beginning.isDestination()) {
  20.             beginning = list.getSmallest();
  21.             beginning.markOutList();
  22.             int pathWay = 0;
  23.            
  24.             if (beginning.isDestination()) {
  25.                 destinationFound = true;
  26.                 System.out.println("The path found traversed " + pathWay + " cells.");
  27.                 break;
  28.             }
  29.             else {
  30.                 for (int i = 0; i <= 3; i++) {
  31.                     if (nextCell(beginning) != null) {
  32.                         int distance = 1 + beginning.getDistanceToStart();
  33.                         MapCell neighbor = nextCell(beginning);
  34.                         if (neighbor.getDistanceToStart() > distance) {
  35.                             neighbor.setDistanceToStart(distance);
  36.                             neighbor.setPredecessor(beginning);
  37.                         }
  38.                         pathWay = neighbor.getDistanceToStart();
  39.                         if ((neighbor.isMarkedInList()) && (pathWay < list.getValue(neighbor))) {
  40.                             list.changeValue(neighbor, pathWay);
  41.                                                                    
  42.                         }
  43.                         if (!neighbor.isMarkedInList()) {
  44.                             list.insert(neighbor, pathWay);
  45.                             neighbor.markInList();
  46.                         }
  47.                     }
  48.                 }
  49.             }
  50.         }
  51.     }
  52.    
  53.     private MapCell nextCell(MapCell cell) {
  54. //      if(cell.isNorthRoad() || cell.isSouthRoad() || cell.isEastRoad() || cell.isWestRoad()) {
  55. //         
  56. //      }
  57.         for(int i = 0; i<=3; i++) {
  58.             if(cell.getNeighbour(i).isMarked()) {
  59.                 return cell.getNeighbour(i);
  60.             }
  61.         }
  62.         return null;
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement