Jelajahi Sumber

added boat stuff to algorithm

Leon Palluch 4 tahun lalu
induk
melakukan
66b576103c

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

@@ -23,4 +23,9 @@ public class Coord {
     public void changeY(int y) {
         this.y += y;
     }
+
+    public void setCoord(int x, int y) {
+        this.x = x;
+        this.y = y;
+    }
 }

+ 184 - 238
src/pathgame/algorithm/DijkstraMagic.java

@@ -1,33 +1,38 @@
 package pathgame.algorithm;
 
-
-import pathgame.gameplay.PlayerAbilities;
 import pathgame.tilemap.TileMap;
+import pathgame.tilemap.TileType;
 import pathgame.tilemap.Tiles;
 
 import java.util.ArrayList;
 import pathgame.gameplay.Player;
 
 public class DijkstraMagic {
-    private int townID = Tiles.TOWN.getId();
-    private int startID = Tiles.HOME_TOWN.getId();
+    private int TOWN_ID = Tiles.TOWN.getId();
+    private int START_ID = Tiles.HOME_TOWN.getId();
+    private int PORT_ID = Tiles.PORT.getId();
+    private int WATER_WEIGHT;
     private ArrayList<Coord> towns = new ArrayList<>();
+    private ArrayList<Coord> ports = new ArrayList<>();
     private TileMap map;
     private Node2D[][] weightMap;
     private ArrayList<ArrayList<SaleRoute>> salesPitch = new ArrayList<>();
 
-    public DijkstraMagic(TileMap map) {
+    public DijkstraMagic(TileMap map, Player player) {
         this.map = map;
 
         weightMap = new Node2D[map.getWidth()][map.getHeight()];
-        //weightMap = new Node2D[5][15];
 
-        setup();
+        setup(player);
 
         printWeightMap();
 
+        for(int i = 0; i < ports.size(); i++) {
+            doMagic(i, false);
+        }
+
         for(int i = 0; i < towns.size(); i++) {
-            doMagicforTown(i);
+            doMagic(i, true);
         }
 
         printDijkstraResult();
@@ -37,12 +42,18 @@ public class DijkstraMagic {
         return salesPitch;
     }
 
-    private void doMagicforTown(int fromIndex) {
-        if(fromIndex != 0) {
-            resetWeightMap();
+    private void doMagic(int fromIndex, boolean forTown) {
+        resetWeightMap();
+
+        Coord origin;
+
+        if(forTown) {
+            origin = towns.get(fromIndex);
+        }
+        else {
+            origin = ports.get(fromIndex);
         }
 
-        Coord origin = towns.get(fromIndex);
         int posX = origin.getX(), posY = origin.getY();
 
         weightMap[posX][posY].setCostSoFar(0);
@@ -64,111 +75,171 @@ public class DijkstraMagic {
                 }
             }
 
-            dijkstraOnTile(tileQ.get(leastIndex).getX(), tileQ.get(leastIndex).getY(), tileQ, leastIndex);
-        }
+            Coord c = tileQ.get(leastIndex);
 
-        makeListForPitch(fromIndex);
+            if(weightMap[c.getX()][c.getY()].isBlocked() && forTown) {
+                tileQ.remove(leastIndex);
+            }
+            else {
+                dijkstraForTownOrPort(tileQ.get(leastIndex).getX(), tileQ.get(leastIndex).getY(), tileQ, leastIndex, forTown);
+            }
+        }
+        if(forTown) {
+            makeListForPitch(fromIndex);
+        }
+        else {
+            addPortsPaths(fromIndex);
+        }
 
         //printDijkstraMap();
     }
 
-    private void makeListForPitch(int fromIndex) {
-        ArrayList<SaleRoute> listForPitch = new ArrayList<>();
-
-        for(int i = fromIndex+1; i < towns.size(); i++) {
-            ArrayList<Coord> listForRoute = new ArrayList<>();
-
-            Coord curPos = new Coord(towns.get(i).getX(), towns.get(i).getY());
+    private void dijkstraForTownOrPort(int posX, int posY, ArrayList<Coord> tileQ, int qIndex, boolean forTown) {
+        int prevCost = weightMap[posX][posY].getCostSoFar();
 
-            int totalCost = weightMap[curPos.getX()][curPos.getY()].getCostSoFar();
+        if(posX < weightMap.length-1) {
+            adjacent(posX+1, posY, prevCost, 'w', null, -1, tileQ, forTown);
+        }
+        if(posX > 0) {
+            adjacent(posX-1, posY, prevCost, 'e', null, -1, tileQ, forTown);
+        }
+        if(posY < weightMap[0].length-1) {
+            adjacent(posX, posY+1, prevCost, 'n', null, -1, tileQ, forTown);
+        }
+        if(posY > 0) {
+            adjacent(posX, posY-1, prevCost, 's', null, -1, tileQ, forTown);
+        }
 
-            while(true) {
-                char dir = weightMap[curPos.getX()][curPos.getY()].getPrevOfPath();
+        if(forTown) {
+            if(weightMap[posX][posY].hasExtraPaths()) {
+                ArrayList<ExtraPath> extraPaths = weightMap[posX][posY].getExtraPaths();
 
-                listForRoute.add(new Coord(curPos.getX(), curPos.getY()));
+                for(int i = 0; i < extraPaths.size(); i++) {
+                    ExtraPath path = extraPaths.get(i);
 
-                if(dir == 'n') {
-                    curPos.changeY(-1);
-                }
-                else if(dir == 'e') {
-                    curPos.changeX(1);
-                }
-                else if(dir == 's') {
-                    curPos.changeY(1);
-                }
-                else if(dir == 'w') {
-                    curPos.changeX(-1);
-                }
-                else {
-                    break;
+                    adjacent(path.getDestX(), path.getDestY(), prevCost, '\0', path, i, tileQ, forTown);
                 }
             }
-
-            listForPitch.add(new SaleRoute(listForRoute, totalCost));
         }
 
-        salesPitch.add(listForPitch);
+        tileQ.remove(qIndex);
     }
 
-    private void dijkstraOnTile(int posX, int posY, ArrayList<Coord> tileQ, int qIndex) {
-        int prevCost = weightMap[posX][posY].getCostSoFar();
+    private void adjacent(int posX, int posY, int prevCost, char dir, ExtraPath boatPath, int extraInd, ArrayList<Coord> tileQ, boolean forTown) {
+        if(forTown) {
+            if(!weightMap[posX][posY].isBlocked()) {
+                adjacentCalc(posX, posY, prevCost, dir, boatPath, extraInd, tileQ, forTown);
+            }
+        }
+        else {
+            TileType type = weightMap[posX][posY].getType();
 
-        if(posX < weightMap.length-1) {
-            checkNeighbor(posX + 1, posY, prevCost, 'w');
+            if(type == TileType.SHALLOW_WATER || type == TileType.DEEP_WATER || type == TileType.PORT) {
+                adjacentCalc(posX, posY, prevCost, dir, null, -1, tileQ, forTown);
+            }
         }
-        if(posX > 0) {
-            checkNeighbor(posX - 1, posY, prevCost, 'e');
+    }
+
+    private void adjacentCalc(int posX, int posY, int prevCost, char dir, ExtraPath extraPath, int extraInd, ArrayList<Coord> tileQ, boolean forTown) {
+        int newCost = prevCost;
+        if(forTown) {
+            if(extraPath == null) {
+                newCost += weightMap[posX][posY].getWeight();
+            }
+            else {
+                newCost += extraPath.getPathWeight();
+            }
+
         }
-        if(posY < weightMap[0].length-1) {
-            checkNeighbor(posX, posY + 1, prevCost, 'n');
+        else {
+            newCost += WATER_WEIGHT;
         }
-        if(posY > 0) {
-            checkNeighbor(posX, posY - 1, prevCost, 's');
+
+        if(newCost < weightMap[posX][posY].getCostSoFar()) {
+            weightMap[posX][posY].setCostSoFar(newCost);
+            if(extraPath == null) {
+                weightMap[posX][posY].setPrevOfPath(dir);
+            }
+            else {
+                weightMap[posX][posY].setPrevBoatPath(0);
+            }
         }
 
-        if(weightMap[posX][posY].hasExtraPaths()) {
-            //TODO: implement extra path stuff
+        if(!weightMap[posX][posY].isQAdded()) {
+            weightMap[posX][posY].setQAdded(true);
+            tileQ.add(new Coord(posX, posY));
         }
+    }
 
-        tileQ.remove(qIndex);
+    private void addPortsPaths(int fromIndex) {
+        Coord origin = ports.get(fromIndex);
 
-        if(posX < weightMap.length-1) {
-            if(!weightMap[posX+1][posY].isQAdded()) {
-                weightMap[posX+1][posY].setQAdded(true);
-                tileQ.add(new Coord(posX+1, posY));
+        for(int i = fromIndex+1; i < ports.size(); i++) {
+            Coord nextPort = ports.get(i);
+
+            if(weightMap[nextPort.getX()][nextPort.getY()].getCostSoFar() < Integer.MAX_VALUE) {
+                //System.out.println("connecting ports " + fromIndex + " and " + i);
+
+                int pathWeight = weightMap[nextPort.getX()][nextPort.getY()].getCostSoFar();
+                ArrayList<Coord> pathCoords = new ArrayList<>();
+
+                makePathCoords(false, new Coord(nextPort.getX(), nextPort.getY()), pathCoords);
+
+                weightMap[origin.getX()][origin.getY()].addExtraPath(nextPort, pathWeight, pathCoords);
+                weightMap[nextPort.getX()][nextPort.getY()].addExtraPath(origin, pathWeight, pathCoords);
             }
         }
-        if(posX > 0) {
-            if(!weightMap[posX-1][posY].isQAdded()) {
-                weightMap[posX-1][posY].setQAdded(true);
-                tileQ.add(new Coord(posX-1, posY));
+    }
+
+    private void makePathCoords(boolean forTown, Coord curPos, ArrayList<Coord> coordList) {
+        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(dir == 'n') {
+                curPos.changeY(-1);
             }
-        }
-        if(posY < weightMap[0].length-1) {
-            if(!weightMap[posX][posY+1].isQAdded()) {
-                weightMap[posX][posY+1].setQAdded(true);
-                tileQ.add(new Coord(posX, posY+1));
+            else if(dir == 'e') {
+                curPos.changeX(1);
             }
-        }
-        if(posY > 0) {
-            if(!weightMap[posX][posY-1].isQAdded()) {
-                weightMap[posX][posY-1].setQAdded(true);
-                tileQ.add(new Coord(posX, posY-1));
+            else if(dir == 's') {
+                curPos.changeY(1);
+            }
+            else if(dir == 'w') {
+                curPos.changeX(-1);
+            }
+            else if(extraInd != -1 && forTown) {
+                ExtraPath extraPath = weightMap[curPos.getX()][curPos.getY()].getExtraPaths().get(extraInd);
+
+                curPos.setCoord(extraPath.getDestX(), extraPath.getDestY());
+                coordList.addAll(extraPath.getPathCoords());
+            }
+            else {
+                break;
             }
         }
 
-        if(weightMap[posX][posY].hasExtraPaths()) {
-            //TODO: implement extra path stuff
+        if(!forTown) {
+            coordList.remove(0);
+            coordList.remove(coordList.size()-1);
         }
     }
 
-    private void checkNeighbor(int posX, int posY, int prevCost, char origin) {
-        int newCost = prevCost + weightMap[posX][posY].getWeight();
+    private void makeListForPitch(int fromIndex) {
+        ArrayList<SaleRoute> listForPitch = new ArrayList<>();
 
-        if(newCost < weightMap[posX][posY].getCostSoFar()) {
-            weightMap[posX][posY].setCostSoFar(newCost);
-            weightMap[posX][posY].setPrevOfPath(origin);
+        for(int i = fromIndex+1; i < towns.size(); i++) {
+            ArrayList<Coord> listForRoute = new ArrayList<>();
+            Coord curPos = new Coord(towns.get(i).getX(), towns.get(i).getY());
+            int totalCost = weightMap[curPos.getX()][curPos.getY()].getCostSoFar();
+
+            makePathCoords(true, curPos, listForRoute);
+
+            listForPitch.add(new SaleRoute(listForRoute, totalCost));
         }
+
+        salesPitch.add(listForPitch);
     }
 
     private void resetWeightMap() {
@@ -176,176 +247,38 @@ public class DijkstraMagic {
             for (int y = 0; y < weightMap[0].length; y++) {
                 weightMap[x][y].setCostSoFar(Integer.MAX_VALUE);
                 weightMap[x][y].setQAdded(false);
-                weightMap[x][y].setPrevOfPath('0');
+                weightMap[x][y].setPrevOfPath('\0');
+                weightMap[x][y].setPrevBoatPath(-1);
             }
         }
     }
 
-    private void setup() {
+    private void setup(Player player) {
         //find towns
-        //mock implementation
-        /*
-        //test start
-        towns.add(new Coord(0, 0));
-        towns.add(new Coord(2, 3));
-        towns.add(new Coord(4, 1));
-        towns.add(new Coord(0, 4));
-        towns.add(new Coord(4, 7));
-        towns.add(new Coord(2, 5));
-        towns.add(new Coord(3, 9));
-        towns.add(new Coord(1, 8));
-        //new test
-        towns.add(new Coord(1, 3));
-        towns.add(new Coord(1, 5));
-
-        towns.add(new Coord(1, 14));
-        towns.add(new Coord(0, 10));
-        //towns.add(new Coord(2, 8));
-        //towns.add(new Coord(0, 2));
-        //towns.add(new Coord(0, 5));
-        //towns.add(new Coord(0, 14));
-        //towns.add(new Coord(1, 0));
-        //towns.add(new Coord(1, 7));
-        //towns.add(new Coord(1, 9));
-        //towns.add(new Coord(1, 11));*/
-
-
-
-        /*weightMap[0][0] = new Node2D(1);
-        weightMap[1][0] = new Node2D(1);
-        weightMap[2][0] = new Node2D(1);
-        weightMap[3][0] = new Node2D(5);
-        weightMap[4][0] = new Node2D(5);
-
-        weightMap[0][1] = new Node2D(1);
-        weightMap[1][1] = new Node2D(10);
-        weightMap[2][1] = new Node2D(1);
-        weightMap[3][1] = new Node2D(5);
-        weightMap[4][1] = new Node2D(1);
-
-        weightMap[0][2] = new Node2D(1);
-        weightMap[1][2] = new Node2D(10);
-        weightMap[2][2] = new Node2D(1);
-        weightMap[3][2] = new Node2D(1);
-        weightMap[4][2] = new Node2D(1);
-
-        weightMap[0][3] = new Node2D(1);
-        weightMap[1][3] = new Node2D(1);
-        weightMap[2][3] = new Node2D(1);
-        weightMap[3][3] = new Node2D(1);
-        weightMap[4][3] = new Node2D(5);
-
-        weightMap[0][4] = new Node2D(1);
-        weightMap[1][4] = new Node2D(1);
-        weightMap[2][4] = new Node2D(1);
-        weightMap[3][4] = new Node2D(5);
-        weightMap[4][4] = new Node2D(5);
-
-        weightMap[0][5] = new Node2D(1);
-        weightMap[1][5] = new Node2D(1);
-        weightMap[2][5] = new Node2D(1);
-        weightMap[3][5] = new Node2D(5);
-        weightMap[4][5] = new Node2D(5);
-
-        weightMap[0][6] = new Node2D(1);
-        weightMap[1][6] = new Node2D(10);
-        weightMap[2][6] = new Node2D(1);
-        weightMap[3][6] = new Node2D(5);
-        weightMap[4][6] = new Node2D(1);
-
-        weightMap[0][7] = new Node2D(1);
-        weightMap[1][7] = new Node2D(1);
-        weightMap[2][7] = new Node2D(1);
-        weightMap[3][7] = new Node2D(1);
-        weightMap[4][7] = new Node2D(1);
-
-        weightMap[0][8] = new Node2D(1);
-        weightMap[1][8] = new Node2D(1);
-        weightMap[2][8] = new Node2D(1);
-        weightMap[3][8] = new Node2D(1);
-        weightMap[4][8] = new Node2D(5);
-
-        weightMap[0][9] = new Node2D(1);
-        weightMap[1][9] = new Node2D(1);
-        weightMap[2][9] = new Node2D(1);
-        weightMap[3][9] = new Node2D(1);
-        weightMap[4][9] = new Node2D(5);
-
-        weightMap[0][10] = new Node2D(1);
-        weightMap[1][10] = new Node2D(5);
-        weightMap[2][10] = new Node2D(1);
-        weightMap[3][10] = new Node2D(5);
-        weightMap[4][10] = new Node2D(5);
-
-        weightMap[0][11] = new Node2D(1);
-        weightMap[1][11] = new Node2D(1);
-        weightMap[2][11] = new Node2D(1);
-        weightMap[3][11] = new Node2D(5);
-        weightMap[4][11] = new Node2D(1);
-
-        weightMap[0][12] = new Node2D(1);
-        weightMap[1][12] = new Node2D(10);
-        weightMap[2][12] = new Node2D(1);
-        weightMap[3][12] = new Node2D(1);
-        weightMap[4][12] = new Node2D(1);
-
-        weightMap[0][13] = new Node2D(1);
-        weightMap[1][13] = new Node2D(10);
-        weightMap[2][13] = new Node2D(1);
-        weightMap[3][13] = new Node2D(1);
-        weightMap[4][13] = new Node2D(5);
-
-        weightMap[0][14] = new Node2D(1);
-        weightMap[1][14] = new Node2D(1);
-        weightMap[2][14] = new Node2D(1);
-        weightMap[3][14] = new Node2D(5);
-        weightMap[4][14] = new Node2D(5);*/
-
-        //test end
-
-        /*towns.add(new Coord(6, 10));
-        towns.add(new Coord(30, 29));
-        towns.add(new Coord(41, 20));
-        towns.add(new Coord(42, 24));
-        towns.add(new Coord(26, 41));
-
-        towns.add(new Coord(39, 26));
-        towns.add(new Coord(16, 8));
-        towns.add(new Coord(6, 27));
-        towns.add(new Coord(26, 7));
-        towns.add(new Coord(28, 13));
-
-        towns.add(new Coord(49, 4));
-        towns.add(new Coord(34, 47));
-        towns.add(new Coord(20, 20));
-        towns.add(new Coord(43, 32));
-        towns.add(new Coord(43, 21));
-
-        towns.add(new Coord(3, 43));
-        towns.add(new Coord(34, 8));
-        towns.add(new Coord(30, 17));
-        towns.add(new Coord(35, 49));
-        towns.add(new Coord(28, 10));*/
-
 
         for(int x = 0; x < map.getWidth(); x++) {
             for(int y = 0; y < map.getHeight(); y++) {
-
-                //actual implementation - find towns; townID needs to be set to correct value
-
-                if(map.getTile(x, y).getId() == townID || map.getTile(x, y).getId() == startID) {
+                if(map.getTile(x, y).getId() == TOWN_ID || map.getTile(x, y).getId() == START_ID) {
                     towns.add(new Coord(x, y));
                 }
+                else if(map.getTile(x, y).getId() == PORT_ID) {
+                    ports.add(new Coord(x, y));
+                }
 
-                //translate map to weightMap
-                //TODO: boats
-
-                Player p = new Player();
-                p.setAbilities(PlayerAbilities.NORMAL);
+                weightMap[x][y] = new Node2D(map.getTile(x, y).getEnergyCost(player));
+                weightMap[x][y].setType(map.getTile(x, y).getType());
 
-                weightMap[x][y] = new Node2D(map.getTile(x, y).getEnergyCost(p));
+                if(map.getTile(x, y).isBlockingMovement(player)) {
+                    weightMap[x][y].setBlocked(true);
+                }
             }
         }
+
+        player.switchSailing();
+
+        WATER_WEIGHT = Tiles.SHALLOW_WATER.getEnergyCost(player);
+
+        player.switchSailing();
     }
 
     private void printDijkstraMap() {
@@ -389,7 +322,20 @@ public class DijkstraMagic {
     private void printWeightMap() {
         for(int y = 0; y < weightMap[0].length; y++) {
             for(int x = 0; x < weightMap.length; x++) {
-                System.out.print(weightMap[x][y].getWeight() + " ");
+                int cost = weightMap[x][y].getWeight();
+                TileType type = weightMap[x][y].getType();
+                if(type == TileType.PORT) {
+                    System.out.print("P ");
+                }
+                else if(type == TileType.SHALLOW_WATER) {
+                    System.out.print("s ");
+                }
+                else if(type == TileType.DEEP_WATER) {
+                    System.out.print("d ");
+                }
+                else {
+                    System.out.print(cost + " ");
+                }
             }
             System.out.println();
         }

+ 39 - 2
src/pathgame/algorithm/ExtraPath.java

@@ -1,7 +1,44 @@
 package pathgame.algorithm;
 
+import java.util.ArrayList;
+
 public class ExtraPath {
-    private int weight;
+    private int pathWeight;
     private int destX, destY;
-    private Node2D destNode;
+    private ArrayList<Coord> pathCoords;
+
+    public ExtraPath(int destX, int destY, int pathWeight, ArrayList<Coord> pathCoords) {
+        this.destX = destX;
+        this.destY = destY;
+        this.pathWeight = pathWeight;
+        this.pathCoords = pathCoords;
+    }
+
+    public int getDestX() {
+        return destX;
+    }
+
+    public void setDestX(int destX) {
+        this.destX = destX;
+    }
+
+    public int getDestY() {
+        return destY;
+    }
+
+    public void setDestY(int destY) {
+        this.destY = destY;
+    }
+
+    public int getPathWeight() {
+        return pathWeight;
+    }
+
+    public void setPathWeight(int pathWeight) {
+        this.pathWeight = pathWeight;
+    }
+
+    public ArrayList<Coord> getPathCoords() {
+        return pathCoords;
+    }
 }

+ 32 - 2
src/pathgame/algorithm/Node2D.java

@@ -1,13 +1,19 @@
 package pathgame.algorithm;
 
+import pathgame.tilemap.TileType;
+
 import java.util.ArrayList;
 
 public class Node2D {
     private int weight;
     private int costSoFar = Integer.MAX_VALUE;
     private char prevOfPath = '\0';
+    private int prevBoatPath = -1;
+    private TileType type = TileType.LAND;
     private boolean isQAdded = false;
     private boolean hasExtraPaths = false;
+    private boolean isBlocked = false;
+
     private ArrayList<ExtraPath> extraPaths = new ArrayList<>();
 
     public Node2D(int weight) {
@@ -42,9 +48,9 @@ public class Node2D {
         this.prevOfPath = prevOfPath;
     }
 
-    public void addExtraPath(ExtraPath path) {
+    public void addExtraPath(Coord dest, int pathWeight, ArrayList<Coord> pathCoords) {
         hasExtraPaths = true;
-        extraPaths.add(path);
+        extraPaths.add(new ExtraPath(dest.getX(), dest.getY(), pathWeight, pathCoords));
     }
 
     public boolean isQAdded() {
@@ -54,4 +60,28 @@ public class Node2D {
     public void setQAdded(boolean QAdded) {
         isQAdded = QAdded;
     }
+
+    public boolean isBlocked() {
+        return isBlocked;
+    }
+
+    public void setBlocked(boolean blocked) {
+        isBlocked = blocked;
+    }
+
+    public TileType getType() {
+        return type;
+    }
+
+    public void setType(TileType type) {
+        this.type = type;
+    }
+
+    public int getPrevBoatPath() {
+        return prevBoatPath;
+    }
+
+    public void setPrevBoatPath(int prevBoatPath) {
+        this.prevBoatPath = prevBoatPath;
+    }
 }

+ 72 - 121
src/pathgame/algorithm/TravellingSalesAlg.java

@@ -1,17 +1,18 @@
 package pathgame.algorithm;
 
+import pathgame.gameplay.Player;
+import pathgame.logging.Logger;
 import pathgame.tilemap.TileMap;
 
 import java.util.ArrayList;
 import java.util.Collections;
 
 public class TravellingSalesAlg {
-    public static int calcSalesPathLen(TileMap map) {
-        DijkstraMagic dijkstra = new DijkstraMagic(map);
+    public static int calcSalesPathLen(TileMap map, Player player) {
+        DijkstraMagic dijkstra = new DijkstraMagic(map, player);
 
         ArrayList<ArrayList<SaleRoute>> salesPitch = dijkstra.getSalesPitch();
 
-
         //make minimum spanning tree
         ArrayList<TreeEdge> MSTree = makeMSTree(salesPitch);
 
@@ -25,7 +26,7 @@ public class TravellingSalesAlg {
         //cut short
         cutShort(eulerTour);
 
-        //TODO: write to logging file
+        Logger.onAlgoDone(salesPitch, eulerTour);
 
         //calculate the total weight of the tour using the edge table (salesPitch)
         int tourWeight = calcTourWeight(eulerTour, salesPitch);
@@ -36,73 +37,6 @@ public class TravellingSalesAlg {
         //return bruteForce(salesPitch);
     }
 
-    private static int bruteForce(ArrayList<ArrayList<SaleRoute>> salesPitch) {
-        Permutation permutation = new Permutation(salesPitch.size(), 1);
-        OddDegreeList nodeList = new OddDegreeList();
-        for(int i = 0; i < salesPitch.size()+1; i++) {
-            nodeList.add(i);
-        }
-
-        while(!permutation.isOverFlowing()) {
-            int newSum = bruteSumPermut(permutation, salesPitch, nodeList);
-            if(newSum < permutation.getMinCost()) {
-                permutation.setMinCost(newSum);
-            }
-            /*permutation.printPermut();
-            System.out.println(newSum);
-            System.out.println();*/
-            permutation.increaseLowest();
-        }
-        permutation.makePermutMinimal();
-        permutation.printPermut();
-        System.out.println("min cost: " + permutation.getMinCost());
-
-
-        return 0;
-    }
-
-    private static int bruteSumPermut(Permutation permutation, ArrayList<ArrayList<SaleRoute>> salesPitch, OddDegreeList nodeList) {
-        int sum = 0;
-        nodeList.resetUsed();
-
-        int first = -1;
-        int last = -1;
-
-        for(int i = 0; i < permutation.size(); i++) {
-            int pos = nodeList.getUnused(permutation.getValAtPos(i));
-            //System.out.println("pos: " + pos);
-
-            if(first == -1) {
-                first = pos;
-            }
-            else {
-                //System.out.println(last + " - " + pos);
-                sum += bruteWeight(last, pos, salesPitch);
-                //System.out.println(sum);
-            }
-            last = pos;
-
-            nodeList.makePosUsed(pos);
-        }
-        //System.out.println(last + " - " + nodeList.getUnused(0));
-        int pos = nodeList.getUnused(0);
-        sum += bruteWeight(last, pos, salesPitch);
-
-        //System.out.println(pos + " - " + first);
-        sum += bruteWeight(pos, first, salesPitch);
-
-        return sum;
-    }
-
-    private static int bruteWeight(int first, int sec, ArrayList<ArrayList<SaleRoute>> salesPitch) {
-        if(first < sec) {
-            return salesPitch.get(first).get(sec-first-1).getTotalCost();
-        }
-        else {
-            return salesPitch.get(sec).get(first-sec-1).getTotalCost();
-        }
-    }
-
     private static int calcTourWeight(ArrayList<Integer> tour, ArrayList<ArrayList<SaleRoute>> salesPitch) {
         int totalWeight = 0;
 
@@ -116,16 +50,9 @@ public class TravellingSalesAlg {
             else {
                 startNode = tour.get(i+1);
                 endNode = tour.get(i) - startNode - 1;
-
             }
-
-            //System.out.println(startNode + "/" + endNode + ": " + salesPitch.get(startNode).get(endNode).getTotalCost());
-
             totalWeight += salesPitch.get(startNode).get(endNode).getTotalCost();
         }
-
-        //System.out.println(totalWeight);
-
         return totalWeight;
     }
 
@@ -150,31 +77,13 @@ public class TravellingSalesAlg {
             else {
                 counter++;
             }
-
         }
-
-        /*System.out.println("cut tour");
-
-        for(int i = 0; i < eulerTour.size(); i++) {
-            if(i != 0) {
-                System.out.print(" - ");
-            }
-            System.out.print(eulerTour.get(i));
-        }
-        System.out.println();*/
-
     }
 
     private static ArrayList<Integer> getEulerTour(ArrayList<TreeEdge> graph) {
         ArrayList<Integer> tour = new ArrayList<>();
 
         while (graph.size() > 0) {
-            /*System.out.println();
-            for(int i = 0; i < graph.size(); i++) {
-                System.out.println("'" + graph.get(i).getSrc() + " : " + graph.get(i).getDest());
-            }
-            System.out.println();*/
-
             if(tour.size() == 0) {
                 tour = getSubtour(graph, graph.get(0).getSrc());
             }
@@ -201,16 +110,6 @@ public class TravellingSalesAlg {
                 ArrayList<Integer> subTour = getSubtour(graph, start);
 
                 mergeTours(tour, subTour);
-
-                /*System.out.println("merged tour");
-
-                for(int i = 0; i < tour.size(); i++) {
-                    if(i != 0) {
-                        System.out.print(" - ");
-                    }
-                    System.out.print(tour.get(i));
-                }
-                System.out.println();*/
             }
         }
 
@@ -233,14 +132,6 @@ public class TravellingSalesAlg {
             tour.add(next);
         }
 
-        /*for(int i = 0; i < tour.size(); i++) {
-            if(i != 0) {
-                System.out.print(" - ");
-            }
-            System.out.print(tour.get(i));
-        }
-        System.out.println();*/
-
         return tour;
     }
 
@@ -369,13 +260,6 @@ public class TravellingSalesAlg {
             }
 
             permut.increaseAtPos(permutPos);
-
-            /*permut.increaseAtPos(3);
-            permut.printPermut();
-            permut.increaseAtPos(2);
-            permut.printPermut();
-            permut.increaseAtPos(2);
-            permut.printPermut();*/
         }
     }
 
@@ -449,4 +333,71 @@ public class TravellingSalesAlg {
             tree.get(i).setChecked(false);
         }
     }
+
+    /*private static int bruteForce(ArrayList<ArrayList<SaleRoute>> salesPitch) {
+        Permutation permutation = new Permutation(salesPitch.size(), 1);
+        OddDegreeList nodeList = new OddDegreeList();
+        for(int i = 0; i < salesPitch.size()+1; i++) {
+            nodeList.add(i);
+        }
+
+        while(!permutation.isOverFlowing()) {
+            int newSum = bruteSumPermut(permutation, salesPitch, nodeList);
+            if(newSum < permutation.getMinCost()) {
+                permutation.setMinCost(newSum);
+            }
+            /*permutation.printPermut();
+            System.out.println(newSum);
+            System.out.println();*/
+            /*permutation.increaseLowest();
+        }
+        permutation.makePermutMinimal();
+        permutation.printPermut();
+        System.out.println("min cost: " + permutation.getMinCost());
+
+
+        return 0;
+    }
+
+    private static int bruteSumPermut(Permutation permutation, ArrayList<ArrayList<SaleRoute>> salesPitch, OddDegreeList nodeList) {
+        int sum = 0;
+        nodeList.resetUsed();
+
+        int first = -1;
+        int last = -1;
+
+        for(int i = 0; i < permutation.size(); i++) {
+            int pos = nodeList.getUnused(permutation.getValAtPos(i));
+            //System.out.println("pos: " + pos);
+
+            if(first == -1) {
+                first = pos;
+            }
+            else {
+                //System.out.println(last + " - " + pos);
+                sum += bruteWeight(last, pos, salesPitch);
+                //System.out.println(sum);
+            }
+            last = pos;
+
+            nodeList.makePosUsed(pos);
+        }
+        //System.out.println(last + " - " + nodeList.getUnused(0));
+        int pos = nodeList.getUnused(0);
+        sum += bruteWeight(last, pos, salesPitch);
+
+        //System.out.println(pos + " - " + first);
+        sum += bruteWeight(pos, first, salesPitch);
+
+        return sum;
+    }
+
+    private static int bruteWeight(int first, int sec, ArrayList<ArrayList<SaleRoute>> salesPitch) {
+        if(first < sec) {
+            return salesPitch.get(first).get(sec-first-1).getTotalCost();
+        }
+        else {
+            return salesPitch.get(sec).get(first-sec-1).getTotalCost();
+        }
+    }*/
 }

+ 2 - 2
src/pathgame/gameplay/Level.java

@@ -52,9 +52,9 @@ public final class Level
         player.reset(map.getHomeX(), map.getHomeY());
         player.setObjectivesAmount(map.getNumberOfTowns());
         
-        TravellingSalesAlg.calcSalesPathLen(map);
+        int algoEnergy = TravellingSalesAlg.calcSalesPathLen(map, player);
         
-        player.setEnergySupply(1000); // ToDo: insert value of algorithm
+        player.setEnergySupply(algoEnergy);
         
         Logger.onLevelReset(level, player, map);
     }

+ 34 - 0
src/pathgame/logging/Logger.java

@@ -1,8 +1,11 @@
 package pathgame.logging;
 
+import pathgame.algorithm.SaleRoute;
 import pathgame.gameplay.Player;
 import pathgame.tilemap.TileMap;
 
+import java.util.ArrayList;
+
 public class Logger
 {
     public static void onLevelReset(int level, Player p, TileMap map)
@@ -24,4 +27,35 @@ public class Logger
     {
         System.out.println("Win");
     }
+
+    public static void onAlgoDone(ArrayList<ArrayList<SaleRoute>> salesPitch, ArrayList<Integer> eulerTour) {
+        System.out.println("algorithm done");
+        /*TODO: follow the eulerTour, getting salesPitch.get(x).get(y).get(path)
+          TODO: and getting the x and y value of each coord in that list and logging them*/
+        /*x and y correspond indirectly to values of the eulerTour
+        * salesPitch is structured like so:
+        *          [town0]  [town1]  [town2]  [town3] ...
+        * [town0]     -     x:0/y:0  x:0/y:1  x:0/y:2
+        * [town1]     -        -     x:1/y:0  x:1/y:1
+        * [town2]     -        -        -     x:2/y:0
+        * [town3]     -        -        -        -
+        * ...*/
+        /*so to get the path between node 0 and 1 use salesPitch.get(0).get(0).get(path);
+        * (x is equal to [townRowIndex], y is equal to [townColIndex] - x - 1 (!!!))
+        * note that paths between x and y are equal to paths between y and x, but only one can be found in salesPitch
+        * so the path between 0 - 1 and 1 - 0 are the same, but only 0 - 1 can be accessed
+        * make sure that x <= y to not go out of bounds*/
+
+        /*Example eulerTour: 3, 0, 1, 2, 3
+        * Therefore, the paths logged need to be:
+        * salesPitch.get(0).get(2).get(path)
+        * salesPitch.get(0).get(0).get(path)
+        * salesPitch.get(1).get(0).get(path)
+        * salesPitch.get(2).get(0).get(path)
+        * */
+
+        /*NOTE: for visual representation, algorithm coordinates don't have to be logged in order
+        * for the final representation to be correct. If the end-output is a list of coordinates, though,
+        * it may be necessary to order the coordinates to form a continuous path*/
+    }
 }