added PathFinderMoveStrategy in Game.
This commit is contained in:
parent
a689df5fe1
commit
4b5802a0d0
|
@ -42,6 +42,7 @@ public class Game implements GameSpecification {
|
||||||
moveStrategies.add("User Move Strategy");
|
moveStrategies.add("User Move Strategy");
|
||||||
moveStrategies.add("Move List Strategy");
|
moveStrategies.add("Move List Strategy");
|
||||||
moveStrategies.add("Path Follow Move Strategy");
|
moveStrategies.add("Path Follow Move Strategy");
|
||||||
|
moveStrategies.add("Path Finder Move Strategy");
|
||||||
for (int i = 0; i < track.getCarCount(); i++) {
|
for (int i = 0; i < track.getCarCount(); i++) {
|
||||||
Car car = track.getCar(i);
|
Car car = track.getCar(i);
|
||||||
MoveStrategy moveStrategy = null;
|
MoveStrategy moveStrategy = null;
|
||||||
|
@ -49,14 +50,14 @@ public class Game implements GameSpecification {
|
||||||
String filePath;
|
String filePath;
|
||||||
int moveStrategie = userInterface.selectOption(
|
int moveStrategie = userInterface.selectOption(
|
||||||
"Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies);
|
"Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies);
|
||||||
switch (moveStrategie + 1) {
|
switch (moveStrategie) {
|
||||||
case 1:
|
case 0:
|
||||||
moveStrategy = new DoNotMoveStrategy();
|
moveStrategy = new DoNotMoveStrategy();
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 1:
|
||||||
moveStrategy = new UserMoveStrategy(userInterface, i, track.getCarId(i));
|
moveStrategy = new UserMoveStrategy(userInterface, i, track.getCarId(i));
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 2:
|
||||||
filePath = ".\\moves\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt";
|
filePath = ".\\moves\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt";
|
||||||
try {
|
try {
|
||||||
moveStrategy = new MoveListStrategy(filePath);
|
moveStrategy = new MoveListStrategy(filePath);
|
||||||
|
@ -65,7 +66,7 @@ public class Game implements GameSpecification {
|
||||||
}
|
}
|
||||||
//TODO: Backslash kompatibel für Linux
|
//TODO: Backslash kompatibel für Linux
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 3:
|
||||||
filePath = ".\\follower\\" + selectedTrack.getName().split("\\.")[0] + "_points.txt";
|
filePath = ".\\follower\\" + selectedTrack.getName().split("\\.")[0] + "_points.txt";
|
||||||
try {
|
try {
|
||||||
moveStrategy = new PathFollowerMoveStrategy(filePath, track.getCarPos(i));
|
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!");
|
userInterface.printInformation("There is no Point-List implemented. Choose another Strategy!");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case 4:
|
||||||
|
moveStrategy = new PathFinderMoveStrategy(track, i);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
selectMoveStrategy(car, moveStrategy);
|
selectMoveStrategy(car, moveStrategy);
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue