Strategy #31

Merged
schrom01 merged 24 commits from Strategy into main 2022-03-25 09:24:21 +01:00
1 changed files with 57 additions and 26 deletions
Showing only changes of commit cf1a8d703e - Show all commits

View File

@ -1,5 +1,7 @@
package ch.zhaw.pm2.racetrack.strategy; 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.PositionVector;
import ch.zhaw.pm2.racetrack.Track; import ch.zhaw.pm2.racetrack.Track;
@ -10,53 +12,74 @@ import java.util.List;
public class PathFinderStrategy implements MoveStrategy{ public class PathFinderStrategy implements MoveStrategy{
private ArrayList<tryOutPaths> possiblePaths; private ArrayList<tryOutPaths> possiblePaths;
private Track track; private Game game;
List<PositionVector.Direction> directions = Arrays.asList(PositionVector.Direction.values()); private boolean foundPath = false;
private List<PositionVector.Direction> directions = Arrays.asList(PositionVector.Direction.values());
private ArrayList<PositionVector.Direction> directionToTake = new ArrayList<>();
private int currentMove = 0;
public PathFinderStrategy(Track track, int carIndex) { public PathFinderStrategy(Game game, int carIndex) {
this.track = track; this.game = game;
possiblePaths = new ArrayList<>(); possiblePaths = new ArrayList<>();
calculatePaths();
try {
getDirections();
} catch (InvalidTrackFormatException e) {
e.printStackTrace();
}
} }
private void test(){ private void calculatePaths() {
ArrayList<tryOutPaths> temporary = new ArrayList<>(); for (int i = 0; i < 50; i++) {
Iterator<tryOutPaths> it = possiblePaths.iterator(); if(foundPath){
break;
while(it.hasNext()) {
tryOutPaths current = it.next();
if (!current.isFeasible()) {
it.remove();
} }
else {
for (PositionVector.Direction direction : directions) { ArrayList<tryOutPaths> temporary = new ArrayList<>();
temporary.add(current); Iterator<tryOutPaths> it = possiblePaths.iterator();
if (temporary.get(temporary.size() - 1).takeDirection(direction)){
break; 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) { private void getDirections() throws InvalidTrackFormatException {
temporary.add(paths); if(foundPath){
temporary.get(temporary.size() - 1).takeDirection(direction); for (tryOutPaths path: possiblePaths) {
if (path.isFinished()){
directionToTake = path.getDirectionsTaken();
}
} }
} else {
throw new InvalidTrackFormatException();
} }
//bis hier löschen
possiblePaths.clear();
possiblePaths.addAll(temporary);
} }
@Override @Override
public PositionVector.Direction nextMove() { public PositionVector.Direction nextMove() {
return null; PositionVector.Direction direction = directionToTake.get(currentMove);
currentMove++;
return direction;
} }
public class tryOutPaths { public class tryOutPaths {
ArrayList<PositionVector.Direction> directionsTaken = new ArrayList<>(); ArrayList<PositionVector.Direction> directionsTaken = new ArrayList<>();
PositionVector currentPosition; PositionVector currentPosition;
Track track; Track track;
private boolean feasible; private boolean feasible;
private boolean finished; private boolean finished;
public tryOutPaths(Track track){ public tryOutPaths(Track track){
@ -81,5 +104,13 @@ public class PathFinderStrategy implements MoveStrategy{
return false; return false;
} }
} }
public ArrayList<PositionVector.Direction> getDirectionsTaken(){
return directionsTaken;
}
public boolean isFinished(){
return finished;
}
} }
} }