Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.12 KB | None | 0 0
  1. public class ShortestPath
  2. {
  3.  
  4. /**
  5. * Instance variable that represents the city map upon which the program will
  6. * try to find a shortest path from starting cell to destination cell
  7. */
  8. CityMap cityMap;
  9.  
  10. /**
  11. * Constructor for the class that receives input of a reference to a CityMap object representing the city map
  12. * @param theMap
  13. */
  14. public ShortestPath (CityMap theMap)
  15. {
  16. cityMap = theMap;
  17. }
  18.  
  19. /**
  20. *
  21. */
  22. public void findShortestPath()
  23. {
  24. OrderedCircularArray pathList = new OrderedCircularArray();
  25. MapCell current = cityMap.getStart();
  26.  
  27. int distance = 0;
  28. pathList.insert(current, distance);
  29. current.markInList();
  30.  
  31. boolean destinationFound = false;
  32. while (!pathList.isEmpty() && destinationFound == false) // while list is not empty and destination has not been reached
  33. {
  34. MapCell smallest = (MapCell) pathList.getSmallest(); // cast type MapCell
  35. smallest.markOutList(); // initially marks starting destination cell out of list
  36.  
  37. if (current.isDestination())
  38. {
  39. break;
  40. }
  41.  
  42.  
  43. // inserts all possible next cell paths into pathList in order of index
  44. for (int i = 0; i < 4; i++) // loops to check each possible cell path given current cell position
  45. {
  46. if (nextCell(smallest) == null)
  47. {
  48. continue;
  49. }
  50.  
  51. else
  52. {
  53. int startDist = 1 + distance; // distance from current cell to starting cell
  54.  
  55. if (nextCell(smallest).getDistanceToStart() > startDist)
  56. {
  57. startDist = nextCell(smallest).getDistanceToStart();
  58. nextCell(smallest).setPredecessor(smallest); // sets current cell to be predecessor of neighbouring cell
  59. }
  60.  
  61.  
  62. int currDist = nextCell(smallest).getDistanceToStart(); // distance from current neighbouring cell to starting cell
  63. if (nextCell(smallest).isMarked() && currDist < pathList.getValue(smallest))
  64. {
  65. pathList.changeValue(nextCell(smallest), currDist);
  66. }
  67.  
  68. if (!nextCell(smallest).isMarked())
  69. {
  70. pathList.insert(nextCell(smallest), currDist); // adds current neighbour to the ordered list
  71. nextCell(smallest).markInList(); // marks neighbouring cell that is shortest distance from start cell in the list
  72. }
  73. }
  74. }
  75. }
  76. }
  77.  
  78.  
  79. /**
  80. * Method that returns the first unmarked neighbouring map cell that is viable for continuing the path given the current cell
  81. * @param cell
  82. * @return value of the next viable cell
  83. */
  84. private MapCell nextCell(MapCell cell)
  85. {
  86. MapCell returnValue = null;
  87.  
  88. if (cell.isNorthRoad())
  89. {
  90. if (cell.getNeighbour(0) != null && !cell.getNeighbour(0).isMarked()) // ensures neighbour cell is not null or marked
  91. {
  92. if (cell.getNeighbour(0).isNorthRoad() || // another North road is a viable cell path
  93. cell.getNeighbour(0).isIntersection() || // an intersection is a viable cell path
  94. cell.getNeighbour(0).isDestination()) // the destination cell is a viable cell path
  95. {
  96. returnValue = cell.getNeighbour(0); // returns cell value if found
  97. }
  98. }
  99. }
  100.  
  101. else if (cell.isEastRoad())
  102. {
  103. if (cell.getNeighbour(1) != null && !cell.getNeighbour(1).isMarked())
  104. {
  105. if (cell.getNeighbour(1).isEastRoad() ||
  106. cell.getNeighbour(1).isIntersection() ||
  107. cell.getNeighbour(1).isDestination())
  108. {
  109. returnValue = cell.getNeighbour(1);
  110. }
  111. }
  112. }
  113.  
  114. else if (cell.isSouthRoad())
  115. {
  116. if (cell.getNeighbour(2) != null && !cell.getNeighbour(2).isMarked())
  117. {
  118. if (cell.getNeighbour(2).isSouthRoad() ||
  119. cell.getNeighbour(2).isIntersection() ||
  120. cell.getNeighbour(2).isDestination())
  121. {
  122. returnValue = cell.getNeighbour(2);
  123. }
  124. }
  125. }
  126.  
  127. else if (cell.isWestRoad())
  128. {
  129. if (cell.getNeighbour(3) != null && !cell.getNeighbour(3).isMarked())
  130. {
  131. if (cell.getNeighbour(3).isWestRoad() ||
  132. cell.getNeighbour(3).isIntersection() ||
  133. cell.getNeighbour(3).isDestination())
  134. {
  135. returnValue = cell.getNeighbour(3);
  136. }
  137. }
  138. }
  139.  
  140. /**
  141. * Evaluates the case where cell is an intersection or starting cell and reviews if neighbouring cells are either a North, East, South, or West road
  142. */
  143. else if (cell.isIntersection() || cell.isStart())
  144. {
  145.  
  146. if (cell.getNeighbour(0) != null &&
  147. !cell.getNeighbour(0).isMarked() && !cell.getNeighbour(0).isBlock())
  148. {
  149. if (cell.getNeighbour(0).isNorthRoad() || cell.getNeighbour(0).isIntersection())
  150. {
  151. returnValue = cell.getNeighbour(0); // returns cell value if found
  152. }
  153. }
  154.  
  155. else if (cell.getNeighbour(1) != null &&
  156. !cell.getNeighbour(1).isMarked() && !cell.getNeighbour(1).isBlock())
  157. {
  158. if (cell.getNeighbour(1).isEastRoad() || cell.getNeighbour(1).isIntersection())
  159. {
  160. returnValue = cell.getNeighbour(1); // returns cell value if found
  161. }
  162. }
  163.  
  164. else if (cell.getNeighbour(2) != null &&
  165. !cell.getNeighbour(2).isMarked() && !cell.getNeighbour(2).isBlock())
  166. {
  167. if (cell.getNeighbour(2).isSouthRoad() || cell.getNeighbour(2).isIntersection())
  168. {
  169. returnValue = cell.getNeighbour(2); // returns cell value if found
  170. }
  171. }
  172.  
  173. else if (cell.getNeighbour(3) != null &&
  174. !cell.getNeighbour(3).isMarked() && !cell.getNeighbour(3).isBlock())
  175. {
  176. if (cell.getNeighbour(3).isWestRoad() || cell.getNeighbour(3).isIntersection())
  177. {
  178. returnValue = cell.getNeighbour(3); // returns cell value if found
  179. }
  180. }
  181. }
  182. return returnValue;
  183.  
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement