changes in Methods in Game.java

This commit is contained in:
romanschenk37 2022-03-11 12:01:55 +01:00
parent e159274085
commit 7e5d349f3f
1 changed files with 38 additions and 11 deletions

View File

@ -25,12 +25,12 @@ public class Game implements GameSpecification {
UserInterface userInterface; UserInterface userInterface;
public Game(String welcometext) { public Game(UserInterface userInterface) {
userInterface = new UserInterface(welcometext); this.userInterface = userInterface;
} }
public boolean initphase() throws InvalidTrackFormatException, FileNotFoundException { public boolean initPhase() throws InvalidTrackFormatException, FileNotFoundException {
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) {
@ -154,7 +154,33 @@ public class Game implements GameSpecification {
@Override @Override
public void doCarTurn(Direction acceleration) { public void doCarTurn(Direction acceleration) {
// TODO: implementation // TODO: implementation
throw new UnsupportedOperationException(); track.getCar(currentCarIndex).accelerate(acceleration);
PositionVector crashPosition = null;
List<PositionVector> positionList = calculatePath(track.getCarPos(currentCarIndex),track.getCar(currentCarIndex).nextPosition());
//TODO: check if Method calculatePath contains endposition
for(PositionVector location : positionList) { //todo: check if order must be reversed
if(willCarCrash(currentCarIndex, location)) {
crashPosition = location;
}
}
if(crashPosition != null) {
track.carDoesCrash(currentCarIndex, crashPosition);
}
else {
track.getCar(currentCarIndex).move();
}
}
public void gamePhase() {
do{
userInterface.printTrack(track);
doCarTurn(userInterface.selectDirection(currentCarIndex, track.getCarId(currentCarIndex)));
if(getWinner() != NO_WINNER) {
return;
}
} while (!allCarsCrashed());
} }
/** /**
@ -190,7 +216,7 @@ public class Game implements GameSpecification {
int y = startPosition.getY(); int y = startPosition.getY();
// Relative Distance (x & y axis) between end- and starting position // Relative Distance (x & y axis) between end- and starting position
int diffX = endPosition.getX() - startPosition.getY(); int diffX = endPosition.getX() - startPosition.getX();
int diffY = endPosition.getY() - startPosition.getY(); int diffY = endPosition.getY() - startPosition.getY();
// Absolute distance (x & y axis) between end- and starting position // Absolute distance (x & y axis) between end- and starting position
@ -248,14 +274,15 @@ public class Game implements GameSpecification {
*/ */
@Override @Override
public boolean willCarCrash(int carIndex, PositionVector position) { public boolean willCarCrash(int carIndex, PositionVector position) {
return track.willCrashAtPosition(position); //TODO: add carIndex
}
List<PositionVector> positionList = calculatePath(track.getCarPos(carIndex),position); public boolean allCarsCrashed() {
for(PositionVector location : positionList) { for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) {
if(track.willCrashAtPosition(location)) { if(! track.getCar(carIndex).isCrashed()) {
return false;
}
}
return true; return true;
} }
}
return false;
}
} }