瀏覽代碼

really fixed boat paths

Leon Palluch 4 年之前
父節點
當前提交
2c16d0b17d
共有 2 個文件被更改,包括 97 次插入6 次删除
  1. 15 0
      src/pathgame/algorithm/Coord.java
  2. 82 6
      src/pathgame/algorithm/DijkstraMagic.java

+ 15 - 0
src/pathgame/algorithm/Coord.java

@@ -2,12 +2,19 @@ package pathgame.algorithm;
 
 public class Coord {
     private int x, y;
+    private boolean boatTile = false;
 
     Coord(int x, int y) {
         this.x = x;
         this.y = y;
     }
 
+    Coord(int x, int y, boolean boatTile) {
+        this.x = x;
+        this.y = y;
+        this.boatTile = boatTile;
+    }
+
     int getX() {
         return x;
     }
@@ -28,4 +35,12 @@ public class Coord {
         this.x = x;
         this.y = y;
     }
+
+    public boolean isBoatTile() {
+        return boatTile;
+    }
+
+    public void setBoatTile(boolean boatTile) {
+        this.boatTile = boatTile;
+    }
 }

+ 82 - 6
src/pathgame/algorithm/DijkstraMagic.java

@@ -1,5 +1,6 @@
 package pathgame.algorithm;
 
+import org.w3c.dom.Node;
 import pathgame.tilemap.TileMap;
 import pathgame.tilemap.TileType;
 import pathgame.tilemap.Tiles;
@@ -85,6 +86,8 @@ public class DijkstraMagic {
             }
         }
         if(forTown) {
+            //printPathMap();
+            //printPorts();
             makeListForPitch(fromIndex);
         }
         else {
@@ -114,10 +117,21 @@ public class DijkstraMagic {
             if(weightMap[posX][posY].hasExtraPaths()) {
                 ArrayList<ExtraPath> extraPaths = weightMap[posX][posY].getExtraPaths();
 
-                for(int i = 0; i < extraPaths.size(); i++) {
-                    ExtraPath path = extraPaths.get(i);
+                for(int p = 0; p < extraPaths.size(); p++) {
+                    ExtraPath path = extraPaths.get(p);
 
-                    adjacent(path.getDestX(), path.getDestY(), prevCost, '\0', path, i, tileQ, forTown);
+                    ArrayList<ExtraPath> foreingPaths = weightMap[path.getDestX()][path.getDestY()].getExtraPaths();
+                    int extraInd = -1;
+
+                    for(int forP = 0; forP < foreingPaths.size(); forP++) {
+                        ExtraPath fPath = foreingPaths.get(forP);
+                        if(fPath.getDestX() == posX && fPath.getDestY() == posY) {
+                            extraInd = forP;
+                            break;
+                        }
+                    }
+
+                    adjacent(path.getDestX(), path.getDestY(), prevCost, '\0', path, extraInd, tileQ, forTown);
                 }
             }
         }
@@ -195,7 +209,12 @@ public class DijkstraMagic {
         while(true) {
             char dir = weightMap[curPos.getX()][curPos.getY()].getPrevOfPath();
             int extraInd = weightMap[curPos.getX()][curPos.getY()].getPrevBoatPath();
-            coordList.add(new Coord(curPos.getX(), curPos.getY()));
+            if(forTown) {
+                coordList.add(new Coord(curPos.getX(), curPos.getY()));
+            }
+            else {
+                coordList.add(new Coord(curPos.getX(), curPos.getY(), true));
+            }
 
             if(dir == 'n') {
                 curPos.changeY(-1);
@@ -283,6 +302,11 @@ public class DijkstraMagic {
     }
 
     private void printDijkstraMap() {
+        String ANSI_RESET = "\u001B[0m";
+        String ANSI_BLUE = "\u001B[34m";
+        String ANSI_PURPLE = "\u001B[35m";
+        String ANSI_RED = "\u001B[31m";
+
         for(int y = 0; y < weightMap[0].length; y++) {
             for(int x = 0; x < weightMap.length; x++) {
                 int cost = weightMap[x][y].getCostSoFar();
@@ -290,8 +314,19 @@ public class DijkstraMagic {
                     System.out.print("- ");
                 }
                 else {
+                    if(weightMap[x][y].getType() == TileType.PORT) {
+                        System.out.print(ANSI_RED + cost + " " + ANSI_RESET);
+                    }
+                    else if (weightMap[x][y].getType() == TileType.SHALLOW_WATER) {
+                        System.out.print(ANSI_BLUE + cost + " " + ANSI_RESET);
+                    }
+                    else if (weightMap[x][y].isTown()) {
+                        System.out.print(ANSI_PURPLE + cost + " " + ANSI_RESET);
+                    }
+                    else {
+                        System.out.print(cost + " ");
+                    }
 
-                    System.out.print(cost + " ");
                 }
 
             }
@@ -304,12 +339,21 @@ public class DijkstraMagic {
     private void printDijkstraResult() {
         System.out.println();
 
+        String ANSI_RESET  = "\u001B[0m";
+        String ANSI_BLUE = "\u001B[34m";
+
         for(int origNum = 0; origNum < salesPitch.size(); origNum++) {
             for (int lNum = 0; lNum < salesPitch.get(origNum).size(); lNum++) {
                 System.out.print("Total Cost: " + salesPitch.get(origNum).get(lNum).getTotalCost() + "   ");
 
                 for (int lInd = 0; lInd < salesPitch.get(origNum).get(lNum).getPath().size(); lInd++) {
-                    System.out.print(salesPitch.get(origNum).get(lNum).getPath().get(lInd).getX() + "/" + salesPitch.get(origNum).get(lNum).getPath().get(lInd).getY() + " ");
+                    Coord c = salesPitch.get(origNum).get(lNum).getPath().get(lInd);
+                    if(c.isBoatTile()) {
+                        System.out.print(ANSI_BLUE + c.getX() + "/" + c.getY() + " " + ANSI_RESET);
+                    }
+                    else {
+                        System.out.print(c.getX() + "/" + c.getY() + " ");
+                    }
                 }
                 System.out.println();
             }
@@ -320,6 +364,38 @@ public class DijkstraMagic {
         System.out.println();
     }
 
+    private void printPorts() {
+        for(int y = 0; y < weightMap[0].length; y++) {
+            for(int x = 0; x < weightMap.length; x++) {
+                if(weightMap[x][y].hasExtraPaths()) {
+                    ArrayList<ExtraPath> extraPaths = weightMap[x][y].getExtraPaths();
+
+                    System.out.println(x + "/" + y);
+                    for (int i = 0; i < extraPaths.size(); i++) {
+                        System.out.println("port id: " + i + ", " + extraPaths.get(i).getDestX() + "/" + extraPaths.get(i).getDestY());
+                    }
+                    System.out.println();
+                }
+            }
+        }
+    }
+
+    private void printPathMap() {
+        for(int y = 0; y < weightMap[0].length; y++) {
+            for(int x = 0; x < weightMap.length; x++) {
+                if(weightMap[x][y].hasExtraPaths()) {
+                    System.out.print(weightMap[x][y].getPrevBoatPath() + " ");
+                }
+                else {
+                    System.out.print(weightMap[x][y].getPrevOfPath() + " ");
+                }
+            }
+            System.out.println();
+        }
+
+        System.out.println();
+    }
+
     private void printWeightMap() {
         String ANSI_RESET = "\u001B[0m";
         String ANSI_BLUE = "\u001B[34m";