|
|
|
@@ -2,7 +2,6 @@ package ch.zhaw.pm2.racetrack;
|
|
|
|
|
|
|
|
|
|
import ch.zhaw.pm2.racetrack.given.GameSpecification;
|
|
|
|
|
import ch.zhaw.pm2.racetrack.strategy.*;
|
|
|
|
|
import org.hamcrest.core.IsInstanceOf;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
@@ -29,7 +28,8 @@ public class Game implements GameSpecification {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method will initialize the game. Therefore it interacts with the user via UserInterface
|
|
|
|
|
* This method will initialize the game. Therefore, it interacts with the user via UserInterface
|
|
|
|
|
*
|
|
|
|
|
* @return true if the initialization is completed. Returns false if there is an error.
|
|
|
|
|
*/
|
|
|
|
|
public boolean initPhase() {
|
|
|
|
@@ -112,13 +112,15 @@ public class Game implements GameSpecification {
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
userInterface.printInformation("No Trackfile found!");
|
|
|
|
|
userInterface.printInformation("No track file found!");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The functionality was taken out of init to automate testing
|
|
|
|
|
* Selects the desired track and returns it.
|
|
|
|
|
* The functionality was taken out of init to automate testing.
|
|
|
|
|
*
|
|
|
|
|
* @param selectedTrack the Track which was selected by user
|
|
|
|
|
*/
|
|
|
|
|
Track selectTrack(File selectedTrack) throws InvalidTrackFormatException, FileNotFoundException {
|
|
|
|
@@ -127,6 +129,7 @@ public class Game implements GameSpecification {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets a desired move strategy for a desired car.
|
|
|
|
|
* The functionality was taken out of init to automate testing
|
|
|
|
|
*
|
|
|
|
|
* @param car to set the MoveStrategy
|
|
|
|
@@ -148,7 +151,7 @@ public class Game implements GameSpecification {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the id of the specified car.
|
|
|
|
|
* Gets the id of the specified car.
|
|
|
|
|
*
|
|
|
|
|
* @param carIndex The zero-based carIndex number
|
|
|
|
|
* @return A char containing the id of the car
|
|
|
|
@@ -221,7 +224,7 @@ public class Game implements GameSpecification {
|
|
|
|
|
* <p>The calling method must check the winner state and decide how to go on. If the winner is different
|
|
|
|
|
* than {@link Game#NO_WINNER}, or the current car is already marked as crashed the method returns immediately.</p>
|
|
|
|
|
*
|
|
|
|
|
* @param acceleration A Direction containing the current cars acceleration vector (-1,0,1) in x and y direction
|
|
|
|
|
* @param acceleration A Direction containing the current car's acceleration vector (-1,0,1) in x and y direction
|
|
|
|
|
* for this turn
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
@@ -253,7 +256,8 @@ public class Game implements GameSpecification {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method implements the gameflow in a while loop. If there is a winner. The method will return its carid.
|
|
|
|
|
* This method is in charge of changing the players and checking if a winner is found if there is no winner null will be returned.
|
|
|
|
|
* If a winner is found the game will return the char of the winning player.
|
|
|
|
|
*
|
|
|
|
|
* @return the ID of the winning car return null if there is no winner.
|
|
|
|
|
*/
|
|
|
|
@@ -317,51 +321,6 @@ public class Game implements GameSpecification {
|
|
|
|
|
return track.calculatePointsOnPath(startPosition, endPosition);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
private void calculateWinner(PositionVector start, PositionVector finish, int carIndex) {
|
|
|
|
|
List<PositionVector> path = calculatePath(start, finish);
|
|
|
|
|
for (PositionVector point : path) {
|
|
|
|
|
switch (track.getSpaceType(point)) {
|
|
|
|
|
case FINISH_UP:
|
|
|
|
|
if (start.getY() < finish.getY()) {
|
|
|
|
|
track.getCar(carIndex).increaseWinPoints();
|
|
|
|
|
} else if (start.getY() > finish.getY()) {
|
|
|
|
|
track.getCar(carIndex).deductWinPoints();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case FINISH_DOWN:
|
|
|
|
|
if (start.getY() > finish.getY()) {
|
|
|
|
|
track.getCar(carIndex).increaseWinPoints();
|
|
|
|
|
} else if (start.getY() < finish.getY()) {
|
|
|
|
|
track.getCar(carIndex).deductWinPoints();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case FINISH_RIGHT:
|
|
|
|
|
if (start.getX() < finish.getX()) {
|
|
|
|
|
track.getCar(carIndex).increaseWinPoints();
|
|
|
|
|
} else if (start.getX() > finish.getX()) {
|
|
|
|
|
track.getCar(carIndex).deductWinPoints();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case FINISH_LEFT:
|
|
|
|
|
if (start.getX() > finish.getX()) {
|
|
|
|
|
track.getCar(carIndex).increaseWinPoints();
|
|
|
|
|
} else if (start.getX() < finish.getX()) {
|
|
|
|
|
track.getCar(carIndex).increaseWinPoints();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Does indicate if a car would have a crash with a WALL space or another car at the given position.
|
|
|
|
|
*
|
|
|
|
@@ -374,6 +333,11 @@ public class Game implements GameSpecification {
|
|
|
|
|
return track.willCrashAtPosition(carIndex, position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if there is just one player left in the game being able to make moves.
|
|
|
|
|
*
|
|
|
|
|
* @return true if there is just one car left false there are more than one car left in game
|
|
|
|
|
*/
|
|
|
|
|
public boolean onlyOneCarLeft() {
|
|
|
|
|
int carsLeft = 0;
|
|
|
|
|
for (int carIndex = 0; carIndex < track.getCarCount(); carIndex++) {
|
|
|
|
@@ -384,6 +348,11 @@ public class Game implements GameSpecification {
|
|
|
|
|
return !(carsLeft > 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if all cars have implemented the do not move strategy, so the game can determine it will end in a draw.
|
|
|
|
|
*
|
|
|
|
|
* @return true if all players have implemented do not move false otherwise
|
|
|
|
|
*/
|
|
|
|
|
public boolean carsMoving() {
|
|
|
|
|
for (int carIndex = 0; carIndex < track.getCarCount(); carIndex++) {
|
|
|
|
|
if (!(track.getCar(carIndex).isCrashed() || track.getCar(carIndex).getMoveStrategy().getClass() == DoNotMoveStrategy.class)) {
|
|
|
|
|