From 882656196324cbaa3ca95ec641c7efb171a9cd3e Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Sat, 19 Mar 2022 13:20:19 +0100 Subject: [PATCH 1/2] GameTest startet ++ Game and Main clean up --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 81 +++++++++----- src/main/java/ch/zhaw/pm2/racetrack/Main.java | 2 +- .../java/ch/zhaw/pm2/racetrack/GameTest.java | 104 ++++++++++++++++++ 3 files changed, 158 insertions(+), 29 deletions(-) create mode 100644 src/test/java/ch/zhaw/pm2/racetrack/GameTest.java diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 810ad78..413fef3 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -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 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 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; } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Main.java b/src/main/java/ch/zhaw/pm2/racetrack/Main.java index 4013616..aec65bd 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Main.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Main.java @@ -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); diff --git a/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java b/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java new file mode 100644 index 0000000..03cb0f3 --- /dev/null +++ b/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java @@ -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(); + } + } + + } +} From b467a795961b3b5262682c1be7532ed561136e26 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Sun, 20 Mar 2022 13:44:54 +0100 Subject: [PATCH 2/2] Changed mainClass in build.gradle to Main --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 669126f..9b73dff 100644 --- a/build.gradle +++ b/build.gradle @@ -44,7 +44,7 @@ version = '2022.1' application { // Define the main class for the application. - mainClass = 'ch.zhaw.pm2.racetrack.ConsoleApp' + mainClass = 'ch.zhaw.pm2.racetrack.Main' } run {