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;
private Track track;
private int currentCarIndex;
private final Config config;
private final UserInterface userInterface;
UserInterface userInterface;
public Game(UserInterface userInterface) {
public Game(UserInterface userInterface, Config config) {
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.
*/
public boolean initPhase() {
File folder = new File("tracks");
File[] listOfFiles = folder.listFiles();
if (listOfFiles.length > 0) {
if (config.getTrackDirectory().listFiles().length > 0) {
List<String> tracks = new ArrayList<>();
for (File file : listOfFiles) {
tracks.add(file.getName());
for (String file : config.getTrackDirectory().list()) {
tracks.add(file);
}
File selectedTrack = listOfFiles[userInterface.selectOption("Select Track file", tracks)];
File selectedTrack = config.getTrackDirectory().listFiles()[userInterface.selectOption("Select Track file", tracks)];
try {
selectTrack(selectedTrack);
} catch (FileNotFoundException e) {
@ -60,7 +59,7 @@ public class Game implements GameSpecification {
Car car = track.getCar(i);
MoveStrategy moveStrategy = null;
while (moveStrategy == null) {
String filePath;
File selectedFile = null;
int moveStrategie = userInterface.selectOption(
"Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies);
switch (moveStrategie) {
@ -71,20 +70,38 @@ public class Game implements GameSpecification {
moveStrategy = new UserMoveStrategy(userInterface, i, track.getCarId(i));
break;
case 2:
filePath = ".\\moves\\" + 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){
try {
moveStrategy = new MoveListStrategy(filePath);
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!");
}
break;
case 3:
filePath = ".\\follower\\" + 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){
try {
moveStrategy = new PathFollowerMoveStrategy(filePath, track.getCarPos(i));
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!");
}
break;
case 4:
moveStrategy = new PathFinderMoveStrategy(track, i);
@ -102,7 +119,7 @@ public class Game implements GameSpecification {
/**
* 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 = new Track(selectedTrack);

View File

@ -7,8 +7,9 @@ public class Main {
public static void main(String[] args) {
UserInterface userInterface = new UserInterface("Hello and Welcome to Racetrack by Team02-\"AngryNerds\"");
Config config = new Config();
while (true) {
Game game = new Game(userInterface);
Game game = new Game(userInterface, config);
String winner;
if (game.initPhase()) {
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.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@ -179,7 +179,7 @@ class CarTest {
assertEquals(moveStrategy, car.getMoveStrategy());
try {
moveStrategy = new MoveListStrategy(".\\moves\\challenge-car-a.txt");
moveStrategy = new MoveListStrategy(new File(".\\moves\\challenge-car-a.txt"));
} catch (FileNotFoundException e) {
Assertions.fail();
}
@ -187,7 +187,7 @@ class CarTest {
assertEquals(moveStrategy, car.getMoveStrategy());
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) {
e.printStackTrace();
}

View File

@ -32,7 +32,8 @@ class GameTest {
@BeforeEach
void setup() {
userInterface = new UserInterface("Test");
game = new Game(userInterface);
Config config = new Config();
game = new Game(userInterface, config);
try {
track = game.selectTrack(new File(TRACK_FILE_PATH));
} catch (InvalidTrackFormatException | FileNotFoundException e) {
@ -95,7 +96,8 @@ class GameTest {
@BeforeEach
void setup() {
userInterface = new UserInterface("Test");
game = new Game(userInterface);
Config config = new Config();
game = new Game(userInterface, config);
try {
track = game.selectTrack(new File(TRACK_FILE_PATH));
} catch (InvalidTrackFormatException | FileNotFoundException e) {
@ -169,21 +171,21 @@ class GameTest {
UP_RIGHT,
UP_RIGHT,
RIGHT,
RIGHT}));
RIGHT}), new Config());
game.initPhase();
Assertions.assertEquals("a",game.gamePhase());
}
@Test
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();
Assertions.assertEquals("b",game.gamePhase());
}
@Test
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();
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 org.junit.jupiter.api.*;
import java.io.File;
import java.io.FileNotFoundException;
public class MoveStrategyTest {
@ -18,7 +19,7 @@ public class MoveStrategyTest {
@BeforeEach
void setup() {
try {
moveList = new MoveListStrategy(".\\moves\\challenge-car-a.txt");
moveList = new MoveListStrategy(new File(".\\moves\\challenge-car-a.txt"));
}catch (FileNotFoundException e) {
e.printStackTrace();
}