Game #23
			
				
			
		
		
		
	| 
						 | 
				
			
			@ -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 !(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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -8,8 +8,8 @@ public class Main {
 | 
			
		|||
 | 
			
		||||
    public static void main(String[] args) throws InvalidTrackFormatException, FileNotFoundException, PositionVectorNotValid {
 | 
			
		||||
        boolean exit = false;
 | 
			
		||||
        while (!exit) {
 | 
			
		||||
        UserInterface userInterface = new UserInterface("Hello and Welcome");
 | 
			
		||||
        while (!exit) {
 | 
			
		||||
            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");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue