Game #23

Merged
schrom01 merged 43 commits from Game into main 2022-03-20 16:56:34 +01:00
3 changed files with 158 additions and 29 deletions
Showing only changes of commit 8826561963 - Show all commits

View File

@ -18,7 +18,7 @@ import static ch.zhaw.pm2.racetrack.PositionVector.Direction;
public class Game implements GameSpecification {
public static final int NO_WINNER = -1;
private Track track;
int currentCarIndex;
private int currentCarIndex;
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[] listOfFiles = folder.listFiles();
if (listOfFiles.length > 0) {
@ -36,39 +36,37 @@ public class Game implements GameSpecification {
tracks.add(file.getName());
}
File selectedTrack = listOfFiles[userInterface.selectOption("Select Track file", tracks)];
try {
track = new Track(selectedTrack);
} catch (FileNotFoundException | PositionVectorNotValid e) {
e.printStackTrace();
}
selectTrack(selectedTrack);
List<String> moveStrategies = new ArrayList<>();
moveStrategies.add("Do not move Strategy");
moveStrategies.add("User Move Strategy");
moveStrategies.add("Move List Strategy");
moveStrategies.add("Path Follow Move Strategy");
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(
"Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies);
switch (moveStrategie + 1) {
case 1:
track.getCar(i).setMoveStrategy(new DoNotMoveStrategy());
selectMoveStrategy(car, new DoNotMoveStrategy());
break;
case 2:
track.getCar(i).setMoveStrategy(new UserMoveStrategy(userInterface, i, track.getCarId(i)));
selectMoveStrategy(car, new UserMoveStrategy(userInterface, i, track.getCarId(i)));
break;
case 3:
String path = ".\\moves\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt";
try {
MoveStrategy moveStrategy = new MoveListStrategy(path);
track.getCar(i).setMoveStrategy(moveStrategy);
selectMoveStrategy(car, moveStrategy);
} catch (FileNotFoundException e) {
userInterface.printInformation("There is no MoveList implemented. Choose another Strategy!");
}
//TODO: Backslash kompatibel für Linux
break;
case 4:
track.getCar(i).setMoveStrategy(new PathFollowerMoveStrategy()); //TODO: add Arguments
//TODO: add Arguments
selectMoveStrategy(car, new PathFollowerMoveStrategy());
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.
* 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.
*
* @param carIndex The zero-based carIndex number
* @return A PositionVector containing the car's current velocity
*/
@ -192,20 +216,21 @@ public class Game implements GameSpecification {
}
public String gamePhase() throws PositionVectorNotValid {
while (CarsMoving() && getWinner() == NO_WINNER) {
while (carsMoving() && getWinner() == NO_WINNER) {
userInterface.printTrack(track);
Direction direction = null;
direction= track.getCar(currentCarIndex).getMoveStrategy().nextMove();
if(direction == null) {
Direction direction;
direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove();
if (direction == null) {
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();
}
userInterface.printTrack(track);
int indexWinner = getWinner();
if(indexWinner == NO_WINNER){
if (indexWinner == NO_WINNER) {
return null;
}
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) {
List<PositionVector> path = calculatePath(start, finish);
for (PositionVector point : path) {
if (track.getSpaceType(point) != null)
{
if (track.getSpaceType(point) != null) {
switch (track.getSpaceType(point)) {
case FINISH_UP:
if (start.getY() < finish.getY()) {
@ -333,15 +357,16 @@ public class Game implements GameSpecification {
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.
*
* @param carIndex The zero-based carIndex number
* @param position A PositionVector of the possible crash position
* @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() {
int carsLeft = 0;
for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) {
if(! track.getCar(carIndex).isCrashed()) {
for (int carIndex = 0; carIndex < track.getCarCount(); carIndex++) {
if (!track.getCar(carIndex).isCrashed()) {
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)) {
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;
}
}

View File

@ -6,7 +6,7 @@ import java.util.List;
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\"");
while (true) {
Game game = new Game(userInterface);

View File

@ -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();
}
}
}
}