Strategy #31
|
@ -3,7 +3,9 @@ package ch.zhaw.pm2.racetrack.strategy;
|
|||
import ch.zhaw.pm2.racetrack.PositionVector;
|
||||
import ch.zhaw.pm2.racetrack.Track;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class PathFinderMoveStrategy implements MoveStrategy{
|
||||
private Track track;
|
||||
|
@ -11,6 +13,7 @@ public class PathFinderMoveStrategy implements MoveStrategy{
|
|||
private List<PositionVector.Direction> moveList;
|
||||
private int pointer;
|
||||
private List<PositionVector.Direction> allDirections;
|
||||
private List<State> calculatedStates;
|
||||
|
||||
|
||||
public PathFinderMoveStrategy(Track track, int carIndex) {
|
||||
|
@ -23,22 +26,29 @@ public class PathFinderMoveStrategy implements MoveStrategy{
|
|||
|
||||
|
||||
private void createMoveList(){
|
||||
calculatedStates = new ArrayList<>();
|
||||
PossibleMove finishedMove = null;
|
||||
List<PossibleMove> possibleMoves= new ArrayList<>();
|
||||
for(PositionVector.Direction direction : allDirections){
|
||||
possibleMoves.add(new PossibleMove(null, direction));
|
||||
PossibleMove newMove = new PossibleMove(null, direction);
|
||||
if(! newMove.crashed()){
|
||||
possibleMoves.add(newMove);
|
||||
}
|
||||
|
||||
}
|
||||
while(finishedMove == null){
|
||||
List<PossibleMove> newMoves = new ArrayList<>();
|
||||
for(PossibleMove previousMove : possibleMoves){
|
||||
for(PositionVector.Direction direction : allDirections){
|
||||
PossibleMove newMove = new PossibleMove(previousMove, direction);
|
||||
if(! (newMove.crashed() || newMove.drivingLoop() || finishedMove != null)){
|
||||
State newState = new State(newMove.endPosition, newMove.endVelocity);
|
||||
if(! (newMove.crashed() || alreadyCalculated(newState) || finishedMove != null)){
|
||||
if(newMove.finished()){
|
||||
finishedMove = newMove;
|
||||
break;
|
||||
} else {
|
||||
calculatedStates.add(newState);
|
||||
newMoves.add(newMove);
|
||||
}
|
||||
newMoves.add(newMove);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -46,26 +56,54 @@ public class PathFinderMoveStrategy implements MoveStrategy{
|
|||
|
||||
}
|
||||
|
||||
|
||||
moveList = finishedMove.directions;
|
||||
|
||||
|
||||
}
|
||||
|
||||
private boolean alreadyCalculated(State state){
|
||||
for(State calculatedState: calculatedStates){
|
||||
if(state.equals(calculatedState)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public class State{
|
||||
PositionVector position;
|
||||
PositionVector velocity;
|
||||
|
||||
public State(PositionVector position, PositionVector velocity){
|
||||
this.position = position;
|
||||
this.velocity = velocity;
|
||||
}
|
||||
|
||||
public boolean equals(State compareState){
|
||||
if(compareState.position.equals(position) && compareState.velocity.equals(velocity)){
|
||||
return true;
|
||||
} else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class PossibleMove {
|
||||
List<PositionVector.Direction> directions;
|
||||
List<PositionVector> positions;
|
||||
PositionVector startPosition;
|
||||
PositionVector endPosition;
|
||||
PositionVector endVelocity;
|
||||
|
||||
|
||||
public PossibleMove(PossibleMove previousMove, PositionVector.Direction nextDirection){
|
||||
PositionVector startVelocity;
|
||||
PositionVector startPosition;
|
||||
positions = new ArrayList<>();
|
||||
|
||||
directions = new ArrayList<>();
|
||||
if(previousMove != null){
|
||||
positions.addAll(previousMove.positions);
|
||||
directions.addAll(previousMove.directions);
|
||||
startPosition = positions.get(positions.size()-1);
|
||||
startPosition = previousMove.endPosition;
|
||||
startVelocity = previousMove.endVelocity;
|
||||
}
|
||||
else {
|
||||
|
@ -74,34 +112,28 @@ public class PathFinderMoveStrategy implements MoveStrategy{
|
|||
}
|
||||
directions.add(nextDirection);
|
||||
endVelocity = new PositionVector(startVelocity.getX() + nextDirection.vector.getX(), startVelocity.getY() + nextDirection.vector.getY());
|
||||
positions.add(new PositionVector(startPosition.getX() + endVelocity.getX(), startPosition.getY() + endVelocity.getY()));
|
||||
endPosition = new PositionVector(startPosition.getX() + endVelocity.getX(), startPosition.getY() + endVelocity.getY());
|
||||
}
|
||||
|
||||
public boolean finished(){
|
||||
if(track.calculateNewWinPoints(positions.get(positions.size()-2), positions.get(positions.size()-1)) == 1){
|
||||
if(track.calculateNewWinPoints(startPosition, endPosition) == 1){
|
||||
return true;
|
||||
}
|
||||
else return false;
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean crashed() {
|
||||
for(PositionVector point : track.calculatePointsOnPath(positions.get(positions.size()-2), positions.get(positions.size()-1))) {
|
||||
if (track.willCrashAtPosition(carIndex, point)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if(track.calculateNewWinPoints(positions.get(positions.size()-2), positions.get(positions.size()-1)) == -1){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean drivingLoop(){
|
||||
for(int i = 0; i < positions.size()-1; i++){
|
||||
if(positions.get(i).equals(positions.get(positions.size()-1))){
|
||||
List<PositionVector> points = track.calculatePointsOnPath(startPosition, endPosition);
|
||||
for(PositionVector point : points) {
|
||||
if (track.willCrashAtPosition(carIndex, point)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if(track.calculateNewWinPoints(startPosition, endPosition) == -1){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue