diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 265cd15..e5f0ed1 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -42,6 +42,7 @@ public class Game implements GameSpecification { moveStrategies.add("User Move Strategy"); moveStrategies.add("Move List Strategy"); moveStrategies.add("Path Follow Move Strategy"); + moveStrategies.add("Path Finder Move Strategy"); for (int i = 0; i < track.getCarCount(); i++) { Car car = track.getCar(i); MoveStrategy moveStrategy = null; @@ -49,14 +50,14 @@ public class Game implements GameSpecification { String filePath; int moveStrategie = userInterface.selectOption( "Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies); - switch (moveStrategie + 1) { - case 1: + switch (moveStrategie) { + case 0: moveStrategy = new DoNotMoveStrategy(); break; - case 2: + case 1: moveStrategy = new UserMoveStrategy(userInterface, i, track.getCarId(i)); break; - case 3: + case 2: filePath = ".\\moves\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt"; try { moveStrategy = new MoveListStrategy(filePath); @@ -65,7 +66,7 @@ public class Game implements GameSpecification { } //TODO: Backslash kompatibel für Linux break; - case 4: + case 3: filePath = ".\\follower\\" + selectedTrack.getName().split("\\.")[0] + "_points.txt"; try { moveStrategy = new PathFollowerMoveStrategy(filePath, track.getCarPos(i)); @@ -73,6 +74,9 @@ public class Game implements GameSpecification { userInterface.printInformation("There is no Point-List implemented. Choose another Strategy!"); } break; + case 4: + moveStrategy = new PathFinderMoveStrategy(track, i); + break; } } selectMoveStrategy(car, moveStrategy); diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java new file mode 100644 index 0000000..04e79d8 --- /dev/null +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java @@ -0,0 +1,40 @@ +package ch.zhaw.pm2.racetrack.strategy; + +import ch.zhaw.pm2.racetrack.PositionVector; +import ch.zhaw.pm2.racetrack.Track; + +import java.util.ArrayList; +import java.util.List; + +public class PathFinderMoveStrategy implements MoveStrategy{ + private Track track; + private int carIndex; + private List moveList; + private int pointer; + + + public PathFinderMoveStrategy(Track track, int carIndex) { + this.track = track; + this.carIndex = carIndex; + createMoveList(); + pointer = -1; + } + + private void createMoveList(){ + moveList = new ArrayList<>(); + } + + + + @Override + public PositionVector.Direction nextMove() { + pointer += 1; + //TODO: Check if crash. if yes --> createMoveList(); + if (pointer < moveList.size()) { + return moveList.get(pointer); + } + return null; + } + + +} diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java deleted file mode 100644 index 8f08461..0000000 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java +++ /dev/null @@ -1,116 +0,0 @@ -package ch.zhaw.pm2.racetrack.strategy; - -import ch.zhaw.pm2.racetrack.Game; -import ch.zhaw.pm2.racetrack.InvalidTrackFormatException; -import ch.zhaw.pm2.racetrack.PositionVector; -import ch.zhaw.pm2.racetrack.Track; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; - -public class PathFinderStrategy implements MoveStrategy{ - private ArrayList possiblePaths; - private Game game; - private boolean foundPath = false; - private List directions = Arrays.asList(PositionVector.Direction.values()); - private ArrayList directionToTake = new ArrayList<>(); - private int currentMove = 0; - - public PathFinderStrategy(Game game, int carIndex) { - this.game = game; - possiblePaths = new ArrayList<>(); - calculatePaths(); - try { - getDirections(); - } catch (InvalidTrackFormatException e) { - e.printStackTrace(); - } - } - - private void calculatePaths() { - for (int i = 0; i < 50; i++) { - if(foundPath){ - break; - } - - ArrayList temporary = new ArrayList<>(); - Iterator it = possiblePaths.iterator(); - - while (it.hasNext()) { - tryOutPaths current = it.next(); - if (!current.isFeasible()) { - it.remove(); - } else { - for (PositionVector.Direction direction : directions) { - temporary.add(current); - if (temporary.get(temporary.size() - 1).takeDirection(direction)) { - foundPath = true; - } - } - } - } - possiblePaths.clear(); - possiblePaths.addAll(temporary); - } - } - - private void getDirections() throws InvalidTrackFormatException { - if(foundPath){ - for (tryOutPaths path: possiblePaths) { - if (path.isFinished()){ - directionToTake = path.getDirectionsTaken(); - } - } - } else { - throw new InvalidTrackFormatException(); - } - } - - @Override - public PositionVector.Direction nextMove() { - PositionVector.Direction direction = directionToTake.get(currentMove); - currentMove++; - return direction; - } - - public class tryOutPaths { - ArrayList directionsTaken = new ArrayList<>(); - PositionVector currentPosition; - Track track; - - private boolean feasible; - private boolean finished; - public tryOutPaths(Track track){ - this.track = track; - } - - public boolean isFeasible(){ - return feasible; - } - - public boolean takeDirection(PositionVector.Direction direction) { - if(directionsTaken.size() >= 50){ - feasible = false; - return false; - } - else if(finished){ - return true; - } - else { - //check if possible eventuell hier?? - directionsTaken.add(direction); - return false; - } - } - - public ArrayList getDirectionsTaken(){ - return directionsTaken; - } - - public boolean isFinished(){ - return finished; - } - } -}