Code Cleanup

This commit is contained in:
romanschenk37 2022-03-25 20:57:07 +01:00
parent a9366de7bc
commit 29b9daadd4
5 changed files with 50 additions and 29 deletions

View File

@ -20,11 +20,12 @@ 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;
private int currentCarIndex; private int currentCarIndex;
private final Config config;
private final UserInterface userInterface;
UserInterface userInterface; public Game(UserInterface userInterface, Config config) {
public Game(UserInterface userInterface) {
this.userInterface = userInterface; this.userInterface = userInterface;
this.config = config;
} }
/** /**
@ -32,15 +33,13 @@ public class Game implements GameSpecification {
* @return true if the initialization is completed. Returns false if there is an error. * @return true if the initialization is completed. Returns false if there is an error.
*/ */
public boolean initPhase() { public boolean initPhase() {
File folder = new File("tracks"); if (config.getTrackDirectory().listFiles().length > 0) {
File[] listOfFiles = folder.listFiles();
if (listOfFiles.length > 0) {
List<String> tracks = new ArrayList<>(); List<String> tracks = new ArrayList<>();
for (File file : listOfFiles) { for (String file : config.getTrackDirectory().list()) {
tracks.add(file.getName()); tracks.add(file);
} }
File selectedTrack = listOfFiles[userInterface.selectOption("Select Track file", tracks)]; File selectedTrack = config.getTrackDirectory().listFiles()[userInterface.selectOption("Select Track file", tracks)];
try { try {
selectTrack(selectedTrack); selectTrack(selectedTrack);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
@ -60,7 +59,7 @@ public class Game implements GameSpecification {
Car car = track.getCar(i); Car car = track.getCar(i);
MoveStrategy moveStrategy = null; MoveStrategy moveStrategy = null;
while (moveStrategy == null) { while (moveStrategy == null) {
String filePath; File selectedFile = 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) { switch (moveStrategie) {
@ -71,20 +70,38 @@ public class Game implements GameSpecification {
moveStrategy = new UserMoveStrategy(userInterface, i, track.getCarId(i)); moveStrategy = new UserMoveStrategy(userInterface, i, track.getCarId(i));
break; break;
case 2: case 2:
filePath = ".\\moves\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt"; for(File file : config.getMoveDirectory().listFiles()){
try { if(file.toString().equals(config.getMoveDirectory().toString() + "\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt")){
moveStrategy = new MoveListStrategy(filePath); selectedFile = file;
} catch (FileNotFoundException e) { }
}
if(selectedFile != null){
try {
moveStrategy = new MoveListStrategy(selectedFile);
} catch (FileNotFoundException e) {
userInterface.printInformation("There is no Move-List implemented. Choose another Strategy!");
}
} else{
userInterface.printInformation("There is no Move-List implemented. Choose another Strategy!"); userInterface.printInformation("There is no Move-List implemented. Choose another Strategy!");
} }
break; break;
case 3: case 3:
filePath = ".\\follower\\" + selectedTrack.getName().split("\\.")[0] + "_points.txt"; for(File file : config.getFollowerDirectory().listFiles()){
try { if(file.toString().equals(config.getFollowerDirectory().toString() + "\\" + selectedTrack.getName().split("\\.")[0] + "_points.txt")){
moveStrategy = new PathFollowerMoveStrategy(filePath, track.getCarPos(i)); selectedFile = file;
} catch (FileNotFoundException e) { }
}
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{
userInterface.printInformation("There is no Point-List implemented. Choose another Strategy!"); userInterface.printInformation("There is no Point-List implemented. Choose another Strategy!");
} }
break; break;
case 4: case 4:
moveStrategy = new PathFinderMoveStrategy(track, i); moveStrategy = new PathFinderMoveStrategy(track, i);
@ -102,7 +119,7 @@ public class Game implements GameSpecification {
/** /**
* The functionality was taken out of init to automate testing * The functionality was taken out of init to automate testing
* @param selectedTrack * @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); track = new Track(selectedTrack);

View File

@ -7,8 +7,9 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
UserInterface userInterface = new UserInterface("Hello and Welcome to Racetrack by Team02-\"AngryNerds\""); UserInterface userInterface = new UserInterface("Hello and Welcome to Racetrack by Team02-\"AngryNerds\"");
Config config = new Config();
while (true) { while (true) {
Game game = new Game(userInterface); Game game = new Game(userInterface, config);
String winner; String winner;
if (game.initPhase()) { if (game.initPhase()) {
winner = game.gamePhase(); winner = game.gamePhase();

View File

@ -6,9 +6,9 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@ -179,7 +179,7 @@ class CarTest {
assertEquals(moveStrategy, car.getMoveStrategy()); assertEquals(moveStrategy, car.getMoveStrategy());
try { try {
moveStrategy = new MoveListStrategy(".\\moves\\challenge-car-a.txt"); moveStrategy = new MoveListStrategy(new File(".\\moves\\challenge-car-a.txt"));
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Assertions.fail(); Assertions.fail();
} }
@ -187,7 +187,7 @@ class CarTest {
assertEquals(moveStrategy, car.getMoveStrategy()); assertEquals(moveStrategy, car.getMoveStrategy());
try { try {
moveStrategy = new PathFollowerMoveStrategy(".\\follower\\challenge_points.txt", new PositionVector(0, 0)); moveStrategy = new PathFollowerMoveStrategy(new File(".\\follower\\challenge_points.txt"), new PositionVector(0, 0));
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -32,7 +32,8 @@ class GameTest {
@BeforeEach @BeforeEach
void setup() { void setup() {
userInterface = new UserInterface("Test"); userInterface = new UserInterface("Test");
game = new Game(userInterface); Config config = new Config();
game = new Game(userInterface, config);
try { try {
track = game.selectTrack(new File(TRACK_FILE_PATH)); track = game.selectTrack(new File(TRACK_FILE_PATH));
} catch (InvalidTrackFormatException | FileNotFoundException e) { } catch (InvalidTrackFormatException | FileNotFoundException e) {
@ -95,7 +96,8 @@ class GameTest {
@BeforeEach @BeforeEach
void setup() { void setup() {
userInterface = new UserInterface("Test"); userInterface = new UserInterface("Test");
game = new Game(userInterface); Config config = new Config();
game = new Game(userInterface, config);
try { try {
track = game.selectTrack(new File(TRACK_FILE_PATH)); track = game.selectTrack(new File(TRACK_FILE_PATH));
} catch (InvalidTrackFormatException | FileNotFoundException e) { } catch (InvalidTrackFormatException | FileNotFoundException e) {
@ -169,21 +171,21 @@ class GameTest {
UP_RIGHT, UP_RIGHT,
UP_RIGHT, UP_RIGHT,
RIGHT, RIGHT,
RIGHT})); RIGHT}), new Config());
game.initPhase(); game.initPhase();
Assertions.assertEquals("a",game.gamePhase()); Assertions.assertEquals("a",game.gamePhase());
} }
@Test @Test
void crashA() { void crashA() {
game = new Game(new interFace("Test",new Integer[]{0,1,0},new PositionVector.Direction[]{UP})); game = new Game(new interFace("Test",new Integer[]{0,1,0},new PositionVector.Direction[]{UP}), new Config());
game.initPhase(); game.initPhase();
Assertions.assertEquals("b",game.gamePhase()); Assertions.assertEquals("b",game.gamePhase());
} }
@Test @Test
void passFinishLineInWrongDirection() { void passFinishLineInWrongDirection() {
game = new Game(new interFace("Test",new Integer[]{1,0,1},new PositionVector.Direction[]{LEFT,NONE,NONE,RIGHT,RIGHT})); game = new Game(new interFace("Test",new Integer[]{1,0,1},new PositionVector.Direction[]{LEFT,NONE,NONE,RIGHT,RIGHT}), new Config());
game.initPhase(); game.initPhase();
Assertions.assertEquals("a",game.gamePhase()); Assertions.assertEquals("a",game.gamePhase());
} }

View File

@ -4,6 +4,7 @@ import ch.zhaw.pm2.racetrack.strategy.MoveListStrategy;
import ch.zhaw.pm2.racetrack.strategy.MoveStrategy; import ch.zhaw.pm2.racetrack.strategy.MoveStrategy;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
public class MoveStrategyTest { public class MoveStrategyTest {
@ -18,7 +19,7 @@ public class MoveStrategyTest {
@BeforeEach @BeforeEach
void setup() { void setup() {
try { try {
moveList = new MoveListStrategy(".\\moves\\challenge-car-a.txt"); moveList = new MoveListStrategy(new File(".\\moves\\challenge-car-a.txt"));
}catch (FileNotFoundException e) { }catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }