From cf1a8d703e3deac19a614b6a9dbbef6315eb7b0c Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Mon, 21 Mar 2022 01:26:26 +0100 Subject: [PATCH] continued class PathFinderStrategy --- .../strategy/PathFinderStrategy.java | 83 +++++++++++++------ 1 file changed, 57 insertions(+), 26 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java index 23c8c9b..8f08461 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java @@ -1,5 +1,7 @@ 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; @@ -10,53 +12,74 @@ import java.util.List; public class PathFinderStrategy implements MoveStrategy{ private ArrayList possiblePaths; - private Track track; - List directions = Arrays.asList(PositionVector.Direction.values()); + 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(Track track, int carIndex) { - this.track = track; + public PathFinderStrategy(Game game, int carIndex) { + this.game = game; possiblePaths = new ArrayList<>(); + calculatePaths(); + try { + getDirections(); + } catch (InvalidTrackFormatException e) { + e.printStackTrace(); + } } - private void test(){ - ArrayList temporary = new ArrayList<>(); - Iterator it = possiblePaths.iterator(); - - while(it.hasNext()) { - tryOutPaths current = it.next(); - if (!current.isFeasible()) { - it.remove(); + private void calculatePaths() { + for (int i = 0; i < 50; i++) { + if(foundPath){ + break; } - else { - for (PositionVector.Direction direction : directions) { - temporary.add(current); - if (temporary.get(temporary.size() - 1).takeDirection(direction)){ - 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); } - //ab hier zu löschen - for (tryOutPaths paths : possiblePaths){ - for (PositionVector.Direction direction : directions) { - temporary.add(paths); - temporary.get(temporary.size() - 1).takeDirection(direction); + } + + private void getDirections() throws InvalidTrackFormatException { + if(foundPath){ + for (tryOutPaths path: possiblePaths) { + if (path.isFinished()){ + directionToTake = path.getDirectionsTaken(); + } } + } else { + throw new InvalidTrackFormatException(); } - //bis hier löschen - possiblePaths.clear(); - possiblePaths.addAll(temporary); } @Override public PositionVector.Direction nextMove() { - return null; + 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){ @@ -81,5 +104,13 @@ public class PathFinderStrategy implements MoveStrategy{ return false; } } + + public ArrayList getDirectionsTaken(){ + return directionsTaken; + } + + public boolean isFinished(){ + return finished; + } } }