From 7e5d349f3f71f76febabe946314db6217f330b04 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 11 Mar 2022 12:01:55 +0100 Subject: [PATCH] changes in Methods in Game.java --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 49 ++++++++++++++----- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 85363e8..2136aed 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -25,12 +25,12 @@ public class Game implements GameSpecification { UserInterface userInterface; - public Game(String welcometext) { - userInterface = new UserInterface(welcometext); + public Game(UserInterface userInterface) { + this.userInterface = userInterface; } - public boolean initphase() throws InvalidTrackFormatException, FileNotFoundException { + public boolean initPhase() throws InvalidTrackFormatException, FileNotFoundException { File folder = new File("tracks"); File[] listOfFiles = folder.listFiles(); if(listOfFiles.length > 0) { @@ -154,7 +154,33 @@ public class Game implements GameSpecification { @Override public void doCarTurn(Direction acceleration) { // TODO: implementation - throw new UnsupportedOperationException(); + track.getCar(currentCarIndex).accelerate(acceleration); + + PositionVector crashPosition = null; + List 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(); // 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(); // Absolute distance (x & y axis) between end- and starting position @@ -248,14 +274,15 @@ public class Game implements GameSpecification { */ @Override public boolean willCarCrash(int carIndex, PositionVector position) { + return track.willCrashAtPosition(position); //TODO: add carIndex + } - List positionList = calculatePath(track.getCarPos(carIndex),position); - for(PositionVector location : positionList) { - if(track.willCrashAtPosition(location)) { - return true; + public boolean allCarsCrashed() { + for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) { + if(! track.getCar(carIndex).isCrashed()) { + return false; } } - return false; - + return true; } }