fixes in Game.java and Track.java

This commit is contained in:
romanschenk37
2022-03-18 16:13:37 +01:00
parent fd4513e0d0
commit 3684b5589e
3 changed files with 25 additions and 9 deletions
+18 -7
View File
@@ -125,6 +125,9 @@ public class Game implements GameSpecification {
*/
@Override
public int getWinner() {
if (onlyOneCarLeft()) {
return currentCarIndex;
}
List<Car> cars = track.getCars();
for (Car car : cars) {
if (car.getWinPoints() == 1) {
@@ -184,15 +187,13 @@ public class Game implements GameSpecification {
}
public int gamePhase() throws PositionVectorNotValid {
while (getWinner() == NO_WINNER) {
while (CarsMoving() && getWinner() == NO_WINNER) {
userInterface.printTrack(track);
Direction direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove();
doCarTurn(direction);
if (allCarsCrashed()) {
return NO_WINNER;
}
switchToNextActiveCar();
}
userInterface.printTrack(track);
return getWinner();
}
@@ -336,12 +337,22 @@ public class Game implements GameSpecification {
return track.willCrashAtPosition(carIndex, position);
}
public boolean allCarsCrashed() { //TODO: Finish game when only one car left? or if all cars crashed?
public boolean onlyOneCarLeft() {
int carsLeft = 0;
for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) {
if(! track.getCar(carIndex).isCrashed()) {
return false;
carsLeft++;
}
}
return true;
return !(carsLeft > 1);
}
public boolean CarsMoving() {
for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) {
if(! (track.getCar(carIndex).isCrashed() || track.getCar(carIndex).getMoveStrategy().getClass() == DoNotMoveStrategy.class)) {
return true;
}
}
return false;
}
}