Strategy #31

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

View File

@ -224,10 +224,10 @@ public class Game implements GameSpecification {
if (crashPosition != null) {
track.carDoesCrash(currentCarIndex, crashPosition);
} else {
int newWinPoints = calculateNewWinPoints(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition());
int newWinPoints = track.calculateNewWinPoints(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition());
if(newWinPoints == 1){
track.getCar(currentCarIndex).increaseWinPoints();
}else{
}else if(newWinPoints == 1){
track.getCar(currentCarIndex).deductWinPoints();
}
track.moveCar(currentCarIndex);
@ -293,55 +293,6 @@ public class Game implements GameSpecification {
}
/**
* This method will check if a car is passing the finishline.
* If the car is passing the finishline in the wrong direction, the car will lose a winpoint.
* If the car is passing the finishline in the correct direction, the car will gain a winpoint.
* @param start the startposition of the car
* @param finish the expected finishpositon of the car after the move
* @param carIndex of the current player.
*/
private int calculateNewWinPoints(PositionVector start, PositionVector finish) {
List<PositionVector> path = calculatePath(start, finish);
for (PositionVector point : path) {
if (track.getSpaceType(point) != null) {
switch (track.getSpaceType(point)) {
case FINISH_UP:
if (start.getY() < finish.getY()) {
return 1;
} else if (start.getY() > finish.getY()) {
return -1;
}
break;
case FINISH_DOWN:
if (start.getY() > finish.getY()) {
return 1;
} else if (start.getY() < finish.getY()) {
return -1;
}
break;
case FINISH_RIGHT:
if (start.getX() < finish.getX()) {
return 1;
} else if (start.getX() > finish.getX()) {
return -1;
}
break;
case FINISH_LEFT:
if (start.getX() > finish.getX()) {
return 1;
} else if (start.getX() < finish.getX()) {
return -1;
}
break;
default:
return 0;
}
}
}
return 0;
}
/**

View File

@ -423,10 +423,17 @@ public class Track implements TrackSpecification {
return pathList;
}
/**
* This method will check if a car is passing the finishline.
* If the car is passing the finishline in the wrong direction, the car will lose a winpoint.
* If the car is passing the finishline in the correct direction, the car will gain a winpoint.
* @param start the startposition of the car
* @param finish the expected finishpositon of the car after the move
* @return Number of new Winpoints for the current player.
*/
public int calculateNewWinPoints(PositionVector start, PositionVector finish) {
List<PositionVector> path = calculatePointsOnPath(start, finish);
for (PositionVector point : path) {
if (getSpaceType(point) != null) {
switch (getSpaceType(point)) {
case FINISH_UP:
if (start.getY() < finish.getY()) {
@ -456,11 +463,9 @@ public class Track implements TrackSpecification {
return -1;
}
break;
default:
return 0;
}
}
}
return 0;
}

View File

@ -18,6 +18,7 @@ public class PathFinderMoveStrategy implements MoveStrategy{
this.carIndex = carIndex;
allDirections = Arrays.asList(PositionVector.Direction.values());
createMoveList();
pointer = -1;
}
@ -32,7 +33,6 @@ public class PathFinderMoveStrategy implements MoveStrategy{
for(PossibleMove previousMove : possibleMoves){
for(PositionVector.Direction direction : allDirections){
PossibleMove newMove = new PossibleMove(previousMove, direction);
if(! (newMove.crashed() || newMove.drivingLoop())){
newMoves.add(newMove);
}
@ -44,8 +44,6 @@ public class PathFinderMoveStrategy implements MoveStrategy{
moveList = finishedMove.directions;
pointer = moveList.size();
}
@ -92,7 +90,7 @@ public class PathFinderMoveStrategy implements MoveStrategy{
}
public boolean crashed() {
for(PositionVector point : track.calculatePointsOnPath(positions.get(positions.size()-1), positions.get(positions.size()-2))) {
for(PositionVector point : track.calculatePointsOnPath(positions.get(positions.size()-2), positions.get(positions.size()-1))) {
if (track.willCrashAtPosition(carIndex, point)) {
return true;
}
@ -115,9 +113,8 @@ public class PathFinderMoveStrategy implements MoveStrategy{
@Override
public PositionVector.Direction nextMove() {
pointer -= 1;
//TODO: Check if crash. if yes --> createMoveList();
if (pointer >= 0) {
pointer += 1;
if (pointer < moveList.size()) {
return moveList.get(pointer);
}
return null;