Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.scene.control.Cell;
- public class ShortestPath {
- CityMap cityMap;
- public ShortestPath(CityMap theMap) {
- cityMap = theMap;
- }
- public void findShortestPath() {
- OrderedCircularArray<MapCell> list = new OrderedCircularArray<MapCell>();
- MapCell beginning = cityMap.getStart();
- list.insert(beginning,0);
- beginning.markInList();
- boolean destinationFound = false;
- while(!list.isEmpty() && !beginning.isDestination()) {
- beginning = list.getSmallest();
- beginning.markOutList();
- int pathWay = 0;
- if (beginning.isDestination()) {
- destinationFound = true;
- System.out.println("The path found traversed " + pathWay + " cells.");
- break;
- }
- else {
- for (int i = 0; i <= 3; i++) {
- if (nextCell(beginning) != null) {
- int distance = 1 + beginning.getDistanceToStart();
- MapCell neighbor = nextCell(beginning);
- if (neighbor.getDistanceToStart() > distance) {
- neighbor.setDistanceToStart(distance);
- neighbor.setPredecessor(beginning);
- }
- pathWay = neighbor.getDistanceToStart();
- if ((neighbor.isMarkedInList()) && (pathWay < list.getValue(neighbor))) {
- list.changeValue(neighbor, pathWay);
- }
- if (!neighbor.isMarkedInList()) {
- list.insert(neighbor, pathWay);
- neighbor.markInList();
- }
- }
- }
- }
- }
- }
- private MapCell nextCell(MapCell cell) {
- // if(cell.isNorthRoad() || cell.isSouthRoad() || cell.isEastRoad() || cell.isWestRoad()) {
- //
- // }
- for(int i = 0; i<=3; i++) {
- if(cell.getNeighbour(i).isMarked()) {
- return cell.getNeighbour(i);
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement