added PathFinderMoveStrategy in Game.

This commit is contained in:
romanschenk37 2022-03-24 13:59:46 +01:00
parent a689df5fe1
commit 4b5802a0d0
3 changed files with 49 additions and 121 deletions

View File

@ -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);

View File

@ -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<PositionVector.Direction> 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;
}
}

View File

@ -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<tryOutPaths> possiblePaths;
private Game game;
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(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<tryOutPaths> temporary = new ArrayList<>();
Iterator<tryOutPaths> 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<PositionVector.Direction> 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<PositionVector.Direction> getDirectionsTaken(){
return directionsTaken;
}
public boolean isFinished(){
return finished;
}
}
}