fix getWinner in Game.java

working on PathFinderMoveStrategy
This commit is contained in:
romanschenk37 2022-03-24 19:04:43 +01:00
parent 30162df956
commit e84bce43af
4 changed files with 21 additions and 23 deletions

View File

@ -169,14 +169,14 @@ public class Game implements GameSpecification {
*/ */
@Override @Override
public int getWinner() { public int getWinner() {
if (onlyOneCarLeft()) {
return currentCarIndex;
}
for (int i = 0; i < track.getCarCount(); i++) { for (int i = 0; i < track.getCarCount(); i++) {
if (track.getCar(i).getWinPoints() == 1) { if (track.getCar(i).getWinPoints() == 1) {
return i; return i;
} }
} }
if (onlyOneCarLeft()) {
return currentCarIndex;
}
return NO_WINNER; return NO_WINNER;
} }
@ -221,15 +221,15 @@ public class Game implements GameSpecification {
break; break;
} }
} }
if (crashPosition != null) {
track.carDoesCrash(currentCarIndex, crashPosition);
} else {
int newWinPoints = track.calculateNewWinPoints(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition()); int newWinPoints = track.calculateNewWinPoints(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition());
if(newWinPoints == 1){ if(newWinPoints == 1){
track.getCar(currentCarIndex).increaseWinPoints(); track.getCar(currentCarIndex).increaseWinPoints();
}else if(newWinPoints == 1){ }else if(newWinPoints == -1){
track.getCar(currentCarIndex).deductWinPoints(); track.getCar(currentCarIndex).deductWinPoints();
} }
if (crashPosition != null) {
track.carDoesCrash(currentCarIndex, crashPosition);
} else {
track.moveCar(currentCarIndex); track.moveCar(currentCarIndex);
} }
} }

View File

@ -284,7 +284,7 @@ public class Track implements TrackSpecification {
} }
} }
return null; return ConfigSpecification.SpaceType.WALL;
} }
/** /**

View File

@ -33,13 +33,17 @@ public class PathFinderMoveStrategy implements MoveStrategy{
for(PossibleMove previousMove : possibleMoves){ for(PossibleMove previousMove : possibleMoves){
for(PositionVector.Direction direction : allDirections){ for(PositionVector.Direction direction : allDirections){
PossibleMove newMove = new PossibleMove(previousMove, direction); PossibleMove newMove = new PossibleMove(previousMove, direction);
if(! (newMove.crashed() || newMove.drivingLoop())){ if(! (newMove.crashed() || newMove.drivingLoop() || finishedMove != null)){
if(newMove.finished()){
finishedMove = newMove;
break;
}
newMoves.add(newMove); newMoves.add(newMove);
} }
} }
} }
possibleMoves = newMoves; possibleMoves = newMoves;
finishedMove = findFinishedMove(possibleMoves);
} }
moveList = finishedMove.directions; moveList = finishedMove.directions;
@ -47,15 +51,6 @@ public class PathFinderMoveStrategy implements MoveStrategy{
} }
private PossibleMove findFinishedMove(List<PossibleMove> moveList){
for(PossibleMove move : moveList){
if(move.finished()){
return move;
}
}
return null;
}
public class PossibleMove { public class PossibleMove {
List<PositionVector.Direction> directions; List<PositionVector.Direction> directions;
@ -95,6 +90,9 @@ public class PathFinderMoveStrategy implements MoveStrategy{
return true; return true;
} }
} }
if(track.calculateNewWinPoints(positions.get(positions.size()-2), positions.get(positions.size()-1)) == -1){
return true;
}
return false; return false;
} }