worked on PathFinderMoveStrategy and Refactoring in Game.java

This commit is contained in:
romanschenk37 2022-03-24 17:09:05 +01:00
parent 4b5802a0d0
commit ad2e1c3104
2 changed files with 116 additions and 16 deletions

View File

@ -220,7 +220,12 @@ public class Game implements GameSpecification {
if (crashPosition != null) { if (crashPosition != null) {
track.carDoesCrash(currentCarIndex, crashPosition); track.carDoesCrash(currentCarIndex, crashPosition);
} else { } else {
calculateWinner(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition(), currentCarIndex); int newWinPoints = calculateNewWinPoints(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition());
if(newWinPoints == 1){
track.getCar(currentCarIndex).increaseWinPoints();
}else{
track.getCar(currentCarIndex).deductWinPoints();
}
track.moveCar(currentCarIndex); track.moveCar(currentCarIndex);
} }
} }
@ -280,43 +285,46 @@ public class Game implements GameSpecification {
private void calculateWinner(PositionVector start, PositionVector finish, int carIndex) { private int calculateNewWinPoints(PositionVector start, PositionVector finish) {
List<PositionVector> path = calculatePath(start, finish); List<PositionVector> path = calculatePath(start, finish);
for (PositionVector point : path) { for (PositionVector point : path) {
if (track.getSpaceType(point) != null) { if (track.getSpaceType(point) != null) {
switch (track.getSpaceType(point)) { switch (track.getSpaceType(point)) {
case FINISH_UP: case FINISH_UP:
if (start.getY() < finish.getY()) { if (start.getY() < finish.getY()) {
track.getCar(carIndex).increaseWinPoints(); return 1;
} else if (start.getY() > finish.getY()) { } else if (start.getY() > finish.getY()) {
track.getCar(carIndex).deductWinPoints(); return -1;
} }
break; break;
case FINISH_DOWN: case FINISH_DOWN:
if (start.getY() > finish.getY()) { if (start.getY() > finish.getY()) {
track.getCar(carIndex).increaseWinPoints(); return 1;
} else if (start.getY() < finish.getY()) { } else if (start.getY() < finish.getY()) {
track.getCar(carIndex).deductWinPoints(); return -1;
} }
break; break;
case FINISH_RIGHT: case FINISH_RIGHT:
if (start.getX() < finish.getX()) { if (start.getX() < finish.getX()) {
track.getCar(carIndex).increaseWinPoints(); return 1;
} else if (start.getX() > finish.getX()) { } else if (start.getX() > finish.getX()) {
track.getCar(carIndex).deductWinPoints(); return -1;
} }
break; break;
case FINISH_LEFT: case FINISH_LEFT:
if (start.getX() > finish.getX()) { if (start.getX() > finish.getX()) {
track.getCar(carIndex).increaseWinPoints(); return 1;
} else if (start.getX() < finish.getX()) { } else if (start.getX() < finish.getX()) {
track.getCar(carIndex).increaseWinPoints(); return -1;
} }
break; break;
default:
return 0;
} }
} }
} }
return 0;
} }

View File

@ -1,36 +1,128 @@
package ch.zhaw.pm2.racetrack.strategy; package ch.zhaw.pm2.racetrack.strategy;
import ch.zhaw.pm2.racetrack.PositionVector; import ch.zhaw.pm2.racetrack.PositionVector;
import ch.zhaw.pm2.racetrack.PositionVectorNotValid;
import ch.zhaw.pm2.racetrack.Track; import ch.zhaw.pm2.racetrack.Track;
import java.util.ArrayList; import java.util.*;
import java.util.List;
public class PathFinderMoveStrategy implements MoveStrategy{ public class PathFinderMoveStrategy implements MoveStrategy{
private Track track; private Track track;
private int carIndex; private int carIndex;
private List<PositionVector.Direction> moveList; private List<PositionVector.Direction> moveList;
private int pointer; private int pointer;
private List<PositionVector.Direction> allDirections;
public PathFinderMoveStrategy(Track track, int carIndex) { public PathFinderMoveStrategy(Track track, int carIndex) {
this.track = track; this.track = track;
this.carIndex = carIndex; this.carIndex = carIndex;
allDirections = Arrays.asList(PositionVector.Direction.values());
createMoveList(); createMoveList();
pointer = -1;
} }
private void createMoveList(){ private void createMoveList(){
moveList = new ArrayList<>(); PossibleMove finishedMove = null;
List<PossibleMove> possibleMoves= new ArrayList<>();
for(PositionVector.Direction direction : allDirections){
possibleMoves.add(new PossibleMove(null, direction));
}
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())){
newMoves.add(newMove);
}
}
}
possibleMoves = newMoves;
finishedMove = findFinishedMove(possibleMoves);
}
moveList = finishedMove.directions;
pointer = moveList.size();
}
private PossibleMove findFinishedMove(List<PossibleMove> moveList){
for(PossibleMove move : moveList){
if(move.finished()){
return move;
}
}
return null;
}
public class PossibleMove {
List<PositionVector.Direction> directions;
List<PositionVector> positions;
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);
startVelocity = previousMove.endVelocity;
}
else {
startPosition = track.getCarPos(carIndex);
startVelocity = track.getCar(carIndex).getVelocity();
}
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()));
}
public boolean finished(){
for(PositionVector finishPosition : track.getFinishLine()){
if(finishPosition.equals(positions.get(positions.size()-1))){
return true; //TODO: check direction
}
}
return false;
}
public boolean crashed() {
try {
if(track.willCrashAtPosition(carIndex, positions.get(positions.size()-1))){
return true;
}
} catch (PositionVectorNotValid e) {
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))){
return true;
}
}
return false;
}
} }
@Override @Override
public PositionVector.Direction nextMove() { public PositionVector.Direction nextMove() {
pointer += 1; pointer -= 1;
//TODO: Check if crash. if yes --> createMoveList(); //TODO: Check if crash. if yes --> createMoveList();
if (pointer < moveList.size()) { if (pointer >= 0) {
return moveList.get(pointer); return moveList.get(pointer);
} }
return null; return null;