Game #23
|
@ -18,7 +18,7 @@ import static ch.zhaw.pm2.racetrack.PositionVector.Direction;
|
||||||
public class Game implements GameSpecification {
|
public class Game implements GameSpecification {
|
||||||
public static final int NO_WINNER = -1;
|
public static final int NO_WINNER = -1;
|
||||||
private Track track;
|
private Track track;
|
||||||
int currentCarIndex;
|
private int currentCarIndex;
|
||||||
|
|
||||||
UserInterface userInterface;
|
UserInterface userInterface;
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ public class Game implements GameSpecification {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean initPhase() throws InvalidTrackFormatException, FileNotFoundException {
|
public boolean initPhase() throws InvalidTrackFormatException {
|
||||||
File folder = new File("tracks");
|
File folder = new File("tracks");
|
||||||
File[] listOfFiles = folder.listFiles();
|
File[] listOfFiles = folder.listFiles();
|
||||||
if (listOfFiles.length > 0) {
|
if (listOfFiles.length > 0) {
|
||||||
|
@ -36,39 +36,37 @@ public class Game implements GameSpecification {
|
||||||
tracks.add(file.getName());
|
tracks.add(file.getName());
|
||||||
}
|
}
|
||||||
File selectedTrack = listOfFiles[userInterface.selectOption("Select Track file", tracks)];
|
File selectedTrack = listOfFiles[userInterface.selectOption("Select Track file", tracks)];
|
||||||
try {
|
selectTrack(selectedTrack);
|
||||||
track = new Track(selectedTrack);
|
|
||||||
} catch (FileNotFoundException | PositionVectorNotValid e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
List<String> moveStrategies = new ArrayList<>();
|
List<String> moveStrategies = new ArrayList<>();
|
||||||
moveStrategies.add("Do not move Strategy");
|
moveStrategies.add("Do not move Strategy");
|
||||||
moveStrategies.add("User Move Strategy");
|
moveStrategies.add("User Move Strategy");
|
||||||
moveStrategies.add("Move List Strategy");
|
moveStrategies.add("Move List Strategy");
|
||||||
moveStrategies.add("Path Follow Move Strategy");
|
moveStrategies.add("Path Follow Move Strategy");
|
||||||
for (int i = 0; i < track.getCarCount(); i++) {
|
for (int i = 0; i < track.getCarCount(); i++) {
|
||||||
while(track.getCar(i).getMoveStrategy() == null) {
|
Car car = track.getCar(i);
|
||||||
|
while (car.getMoveStrategy() == null) {
|
||||||
int moveStrategie = userInterface.selectOption(
|
int moveStrategie = userInterface.selectOption(
|
||||||
"Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies);
|
"Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies);
|
||||||
switch (moveStrategie + 1) {
|
switch (moveStrategie + 1) {
|
||||||
case 1:
|
case 1:
|
||||||
track.getCar(i).setMoveStrategy(new DoNotMoveStrategy());
|
selectMoveStrategy(car, new DoNotMoveStrategy());
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
track.getCar(i).setMoveStrategy(new UserMoveStrategy(userInterface, i, track.getCarId(i)));
|
selectMoveStrategy(car, new UserMoveStrategy(userInterface, i, track.getCarId(i)));
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
String path = ".\\moves\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt";
|
String path = ".\\moves\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt";
|
||||||
try {
|
try {
|
||||||
MoveStrategy moveStrategy = new MoveListStrategy(path);
|
MoveStrategy moveStrategy = new MoveListStrategy(path);
|
||||||
track.getCar(i).setMoveStrategy(moveStrategy);
|
selectMoveStrategy(car, moveStrategy);
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
userInterface.printInformation("There is no MoveList implemented. Choose another Strategy!");
|
userInterface.printInformation("There is no MoveList implemented. Choose another Strategy!");
|
||||||
}
|
}
|
||||||
//TODO: Backslash kompatibel für Linux
|
//TODO: Backslash kompatibel für Linux
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
track.getCar(i).setMoveStrategy(new PathFollowerMoveStrategy()); //TODO: add Arguments
|
//TODO: add Arguments
|
||||||
|
selectMoveStrategy(car, new PathFollowerMoveStrategy());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,6 +78,31 @@ public class Game implements GameSpecification {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The functionality was taken out of init to automate testing
|
||||||
|
*
|
||||||
|
* @param selectedTrack
|
||||||
|
*/
|
||||||
|
Track selectTrack(File selectedTrack) {
|
||||||
|
try {
|
||||||
|
track = new Track(selectedTrack);
|
||||||
|
return track;
|
||||||
|
} catch (FileNotFoundException | PositionVectorNotValid | InvalidTrackFormatException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The functionality was taken out of init to automate testing
|
||||||
|
*
|
||||||
|
* @param car to set the MoveStrategy
|
||||||
|
* @param strategy The movestrategy to set
|
||||||
|
*/
|
||||||
|
void selectMoveStrategy(Car car, MoveStrategy strategy) {
|
||||||
|
car.setMoveStrategy(strategy);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the index of the current active car.
|
* Return the index of the current active car.
|
||||||
* Car indexes are zero-based, so the first car is 0, and the last car is getCarCount() - 1.
|
* Car indexes are zero-based, so the first car is 0, and the last car is getCarCount() - 1.
|
||||||
|
@ -115,6 +138,7 @@ public class Game implements GameSpecification {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the velocity of the specified car.
|
* Get the velocity of the specified car.
|
||||||
|
*
|
||||||
* @param carIndex The zero-based carIndex number
|
* @param carIndex The zero-based carIndex number
|
||||||
* @return A PositionVector containing the car's current velocity
|
* @return A PositionVector containing the car's current velocity
|
||||||
*/
|
*/
|
||||||
|
@ -192,20 +216,21 @@ public class Game implements GameSpecification {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String gamePhase() throws PositionVectorNotValid {
|
public String gamePhase() throws PositionVectorNotValid {
|
||||||
while (CarsMoving() && getWinner() == NO_WINNER) {
|
while (carsMoving() && getWinner() == NO_WINNER) {
|
||||||
userInterface.printTrack(track);
|
userInterface.printTrack(track);
|
||||||
Direction direction = null;
|
Direction direction;
|
||||||
direction= track.getCar(currentCarIndex).getMoveStrategy().nextMove();
|
direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove();
|
||||||
if(direction == null) {
|
if (direction == null) {
|
||||||
track.getCar(currentCarIndex).setMoveStrategy(new DoNotMoveStrategy());
|
track.getCar(currentCarIndex).setMoveStrategy(new DoNotMoveStrategy());
|
||||||
direction= track.getCar(currentCarIndex).getMoveStrategy().nextMove();
|
direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); //TODO: Entfernen?
|
||||||
}
|
}else {
|
||||||
doCarTurn(direction);
|
doCarTurn(direction);
|
||||||
|
}
|
||||||
switchToNextActiveCar();
|
switchToNextActiveCar();
|
||||||
}
|
}
|
||||||
userInterface.printTrack(track);
|
userInterface.printTrack(track);
|
||||||
int indexWinner = getWinner();
|
int indexWinner = getWinner();
|
||||||
if(indexWinner == NO_WINNER){
|
if (indexWinner == NO_WINNER) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return String.valueOf(track.getCar(indexWinner).getID());
|
return String.valueOf(track.getCar(indexWinner).getID());
|
||||||
|
@ -302,8 +327,7 @@ public class Game implements GameSpecification {
|
||||||
private void calculateWinner(PositionVector start, PositionVector finish, int carIndex) {
|
private void calculateWinner(PositionVector start, PositionVector finish, int carIndex) {
|
||||||
List<PositionVector> path = calculatePath(start, finish);
|
List<PositionVector> path = calculatePath(start, finish);
|
||||||
for (PositionVector point : path) {
|
for (PositionVector point : path) {
|
||||||
if (track.getSpaceType(point) != null)
|
if (track.getSpaceType(point) != null) {
|
||||||
{
|
|
||||||
switch (track.getSpaceType(point)) {
|
switch (track.getSpaceType(point)) {
|
||||||
case FINISH_UP:
|
case FINISH_UP:
|
||||||
if (start.getY() < finish.getY()) {
|
if (start.getY() < finish.getY()) {
|
||||||
|
@ -342,6 +366,7 @@ public class Game implements GameSpecification {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Does indicate if a car would have a crash with a WALL space or another car at the given position.
|
* Does indicate if a car would have a crash with a WALL space or another car at the given position.
|
||||||
|
*
|
||||||
* @param carIndex The zero-based carIndex number
|
* @param carIndex The zero-based carIndex number
|
||||||
* @param position A PositionVector of the possible crash position
|
* @param position A PositionVector of the possible crash position
|
||||||
* @return A boolean indicator if the car would crash with a WALL or another car.
|
* @return A boolean indicator if the car would crash with a WALL or another car.
|
||||||
|
@ -353,17 +378,17 @@ public class Game implements GameSpecification {
|
||||||
|
|
||||||
public boolean onlyOneCarLeft() {
|
public boolean onlyOneCarLeft() {
|
||||||
int carsLeft = 0;
|
int carsLeft = 0;
|
||||||
for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) {
|
for (int carIndex = 0; carIndex < track.getCarCount(); carIndex++) {
|
||||||
if(! track.getCar(carIndex).isCrashed()) {
|
if (!track.getCar(carIndex).isCrashed()) {
|
||||||
carsLeft++;
|
carsLeft++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return !(carsLeft > 1);
|
return !(carsLeft > 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean CarsMoving() {
|
public boolean carsMoving() {
|
||||||
for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) {
|
for (int carIndex = 0; carIndex < track.getCarCount(); carIndex++) {
|
||||||
if(! (track.getCar(carIndex).isCrashed() || track.getCar(carIndex).getMoveStrategy().getClass() == DoNotMoveStrategy.class)) {
|
if (!(track.getCar(carIndex).isCrashed() || track.getCar(carIndex).getMoveStrategy().getClass() == DoNotMoveStrategy.class)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import java.util.List;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) throws InvalidTrackFormatException, FileNotFoundException, PositionVectorNotValid {
|
public static void main(String[] args) throws InvalidTrackFormatException, PositionVectorNotValid {
|
||||||
UserInterface userInterface = new UserInterface("Hello and Welcome to Racetrack by Team02-\"AngryNerds\"");
|
UserInterface userInterface = new UserInterface("Hello and Welcome to Racetrack by Team02-\"AngryNerds\"");
|
||||||
while (true) {
|
while (true) {
|
||||||
Game game = new Game(userInterface);
|
Game game = new Game(userInterface);
|
||||||
|
|
|
@ -0,0 +1,104 @@
|
||||||
|
package ch.zhaw.pm2.racetrack;
|
||||||
|
|
||||||
|
import ch.zhaw.pm2.racetrack.strategy.UserMoveStrategy;
|
||||||
|
import org.junit.jupiter.api.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import static ch.zhaw.pm2.racetrack.Game.NO_WINNER;
|
||||||
|
|
||||||
|
class GameTest {
|
||||||
|
private UserInterface userInterface;
|
||||||
|
private Game game;
|
||||||
|
private Track track;
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Test correct Setup")
|
||||||
|
class Setup {
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
userInterface = new UserInterface("Test");
|
||||||
|
game = new Game(userInterface);
|
||||||
|
track = game.selectTrack(new File(".\\tracks\\challenge.txt"));
|
||||||
|
game.selectMoveStrategy(track.getCar(0),new UserMoveStrategy(new UserInterface("Testing"),0, track.getCarId(0)));
|
||||||
|
game.selectMoveStrategy(track.getCar(1),new UserMoveStrategy(new UserInterface("Testing"),1, track.getCarId(1)));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getCurrentCarIndex() {
|
||||||
|
Assertions.assertEquals(0,game.getCurrentCarIndex());
|
||||||
|
game.switchToNextActiveCar();
|
||||||
|
Assertions.assertEquals(1,game.getCurrentCarIndex());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getCarId() {
|
||||||
|
Assertions.assertEquals('a',game.getCarId(0));
|
||||||
|
Assertions.assertEquals('b',game.getCarId(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getCarPosition() {
|
||||||
|
Assertions.assertEquals(new PositionVector(24,22),game.getCarPosition(0));
|
||||||
|
Assertions.assertEquals(new PositionVector(24,24),game.getCarPosition(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getCarVelocity() {
|
||||||
|
Assertions.assertEquals(new PositionVector(0,0),game.getCarVelocity(0));
|
||||||
|
Assertions.assertEquals(new PositionVector(0,0),game.getCarVelocity(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getWinner() {
|
||||||
|
Assertions.assertEquals(NO_WINNER,game.getWinner());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void onlyOneCarLeft() {
|
||||||
|
Assertions.assertFalse(game.onlyOneCarLeft());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void carsMoving() {
|
||||||
|
Assertions.assertTrue(game.carsMoving());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Basic manipulation")
|
||||||
|
class manipulation {
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
userInterface = new UserInterface("Test");
|
||||||
|
game = new Game(userInterface);
|
||||||
|
track = game.selectTrack(new File(".\\tracks\\challenge.txt"));
|
||||||
|
game.selectMoveStrategy(track.getCar(0),new UserMoveStrategy(new UserInterface("Testing"),0, track.getCarId(0)));
|
||||||
|
game.selectMoveStrategy(track.getCar(1),new UserMoveStrategy(new UserInterface("Testing"),1, track.getCarId(1)));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void carTurnCorrect() {
|
||||||
|
try {
|
||||||
|
game.doCarTurn(PositionVector.Direction.RIGHT);
|
||||||
|
Assertions.assertEquals(new PositionVector(1,0),game.getCarVelocity(0));
|
||||||
|
} catch (PositionVectorNotValid positionVectorNotValid) {
|
||||||
|
positionVectorNotValid.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void carCrash() {
|
||||||
|
try {
|
||||||
|
game.doCarTurn(PositionVector.Direction.UP);
|
||||||
|
Assertions.assertTrue(game.onlyOneCarLeft());
|
||||||
|
} catch (PositionVectorNotValid positionVectorNotValid) {
|
||||||
|
positionVectorNotValid.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue