added java docs

This commit is contained in:
Leonardo Brandenberger
2022-03-25 21:48:40 +01:00
parent 64996f4b93
commit 73fd298fe5
4 changed files with 47 additions and 69 deletions
+33 -64
View File
@@ -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() {
@@ -70,35 +70,35 @@ public class Game implements GameSpecification {
moveStrategy = new UserMoveStrategy(userInterface, i, track.getCarId(i));
break;
case 2:
for(File file : config.getMoveDirectory().listFiles()){
if(file.toString().equals(config.getMoveDirectory().toString() + "\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt")){
for (File file : config.getMoveDirectory().listFiles()) {
if (file.toString().equals(config.getMoveDirectory().toString() + "\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt")) {
selectedFile = file;
}
}
if(selectedFile != null){
if (selectedFile != null) {
try {
moveStrategy = new MoveListStrategy(selectedFile);
} catch (FileNotFoundException e) {
userInterface.printInformation("There is no Move-List implemented. Choose another Strategy!");
}
} else{
} else {
userInterface.printInformation("There is no Move-List implemented. Choose another Strategy!");
}
break;
case 3:
for(File file : config.getFollowerDirectory().listFiles()){
if(file.toString().equals(config.getFollowerDirectory().toString() + "\\" + selectedTrack.getName().split("\\.")[0] + "_points.txt")){
for (File file : config.getFollowerDirectory().listFiles()) {
if (file.toString().equals(config.getFollowerDirectory().toString() + "\\" + selectedTrack.getName().split("\\.")[0] + "_points.txt")) {
selectedFile = file;
}
}
if(selectedFile != null){
if (selectedFile != null) {
try {
moveStrategy = new PathFollowerMoveStrategy(selectedFile, track.getCarPos(currentCarIndex));
} catch (FileNotFoundException e) {
userInterface.printInformation("There is no Point-List implemented. Choose another Strategy!");
}
} else{
} else {
userInterface.printInformation("There is no Point-List implemented. Choose another Strategy!");
}
@@ -112,21 +112,24 @@ 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 {
Track selectTrack(File selectedTrack) throws InvalidTrackFormatException, FileNotFoundException {
track = new Track(selectedTrack);
return track;
}
/**
* 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
@@ -240,9 +243,9 @@ public class Game implements GameSpecification {
}
}
int newWinPoints = track.calculateNewWinPoints(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition());
if(newWinPoints == 1){
if (newWinPoints == 1) {
track.getCar(currentCarIndex).increaseWinPoints();
}else if(newWinPoints == -1){
} else if (newWinPoints == -1) {
track.getCar(currentCarIndex).deductWinPoints();
}
if (crashPosition != null) {
@@ -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.
*/
@@ -265,7 +269,7 @@ public class Game implements GameSpecification {
Direction direction;
direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove();
if (direction == null) {
if(track.getCar(currentCarIndex).getMoveStrategy() instanceof UserMoveStrategy){
if (track.getCar(currentCarIndex).getMoveStrategy() instanceof UserMoveStrategy) {
quit = true;
direction = Direction.NONE;
} else {
@@ -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)) {
@@ -1,10 +1,18 @@
package ch.zhaw.pm2.racetrack;
/**
* Class for Exception when invalid Fileformat is used.
* Class for Exception when invalid file format is used.
*/
public class InvalidFileFormatException extends Exception {
public InvalidFileFormatException(){super();}
public InvalidFileFormatException() {
super();
}
/**
* Constructor that is used when an error message is given with the exception.
*
* @param errorMessage is the message to be displayed
*/
public InvalidFileFormatException(String errorMessage) {
super(errorMessage);
}
@@ -7,6 +7,7 @@ public class InvalidTrackFormatException extends Exception {
public InvalidTrackFormatException(String errorMessage) {
super(errorMessage);
}
public InvalidTrackFormatException() {
super();
}