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

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;
}
}

View File

@ -8,8 +8,8 @@ public class Main {
public static void main(String[] args) throws InvalidTrackFormatException, FileNotFoundException, PositionVectorNotValid {
boolean exit = false;
UserInterface userInterface = new UserInterface("Hello and Welcome");
while (!exit) {
UserInterface userInterface = new UserInterface("Hello and Welcome");
Game game = new Game(userInterface);
int winner = 0;
if (game.initPhase()) {
@ -23,6 +23,7 @@ public class Main {
exit = true;
}
}
userInterface.printInformation("Thank you and goodbye");
}
}

View File

@ -225,7 +225,11 @@ public class Track implements TrackSpecification {
isPositionVectorOnTrack(positionVector);
char charAtPosition = track.get(positionVector.getY()).charAt(positionVector.getX());
if (getCarId(carIndex) == charAtPosition) return false;
return (charAtPosition == ConfigSpecification.SpaceType.WALL.value);
return !(charAtPosition == ConfigSpecification.SpaceType.TRACK.value ||
charAtPosition == ConfigSpecification.SpaceType.FINISH_RIGHT.value ||
charAtPosition == ConfigSpecification.SpaceType.FINISH_LEFT.value ||
charAtPosition == ConfigSpecification.SpaceType.FINISH_UP.value ||
charAtPosition == ConfigSpecification.SpaceType.FINISH_DOWN.value);
}
/**