From 9b6908ef472335352dcf92295ef63c2ee5386234 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 18 Mar 2022 10:00:28 +0100 Subject: [PATCH] implemented Movestrategies in Game.java --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 15 ++++++++------- src/main/java/ch/zhaw/pm2/racetrack/Main.java | 3 +-- .../pm2/racetrack/strategy/DoNotMoveStrategy.java | 5 +++-- .../pm2/racetrack/strategy/UserMoveStrategy.java | 13 +++++++++++-- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index ac0b81e..32f8787 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -57,7 +57,7 @@ public class Game implements GameSpecification { track.getCar(i).setMoveStrategy(new DoNotMoveStrategy()); //TODO: add Arguments break; case 2: - track.getCar(i).setMoveStrategy(new UserMoveStrategy()); //TODO: add Arguments + track.getCar(i).setMoveStrategy(new UserMoveStrategy(userInterface, i, track.getCarId(i))); //TODO: add Arguments break; case 3: track.getCar(i).setMoveStrategy(new MoveListStrategy()); //TODO: add Arguments @@ -174,20 +174,21 @@ public class Game implements GameSpecification { } else { track.moveCar(currentCarIndex); + calculateWinner(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition(), currentCarIndex); } } - public void gamePhase() { + public int gamePhase() { do{ userInterface.printTrack(track); - track.getCar(currentCarIndex).getMoveStrategy(); //TODO Movestrategy berücksichtigen ?? - Direction direction = userInterface.selectDirection(currentCarIndex, track.getCarId(currentCarIndex)); + Direction direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); doCarTurn(direction); - if(getWinner() != NO_WINNER) { - return; + int winner = getWinner(); + if(winner != NO_WINNER) { + return winner; } } while (!allCarsCrashed()); - + return NO_WINNER; } /** diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Main.java b/src/main/java/ch/zhaw/pm2/racetrack/Main.java index a646252..73da6d8 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Main.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Main.java @@ -9,8 +9,7 @@ public class Main { Game game = new Game(userInterface); if(game.initPhase()){ - game.gamePhase(); - int winner = game.getWinner(); + int winner = game.gamePhase(); } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/DoNotMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/DoNotMoveStrategy.java index 24b09e0..62e15e3 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/DoNotMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/DoNotMoveStrategy.java @@ -1,5 +1,7 @@ package ch.zhaw.pm2.racetrack.strategy; +import ch.zhaw.pm2.racetrack.PositionVector; + import static ch.zhaw.pm2.racetrack.PositionVector.Direction; /** @@ -9,7 +11,6 @@ public class DoNotMoveStrategy implements MoveStrategy { @Override public Direction nextMove() { - // TODO: implementation - throw new UnsupportedOperationException(); + return PositionVector.Direction.NONE; } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/UserMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/UserMoveStrategy.java index b7df019..06589ff 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/UserMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/UserMoveStrategy.java @@ -1,15 +1,24 @@ package ch.zhaw.pm2.racetrack.strategy; import ch.zhaw.pm2.racetrack.PositionVector.Direction; +import ch.zhaw.pm2.racetrack.UserInterface; /** * Let the user decide the next move. */ public class UserMoveStrategy implements MoveStrategy { + private UserInterface userInterface; + private int carIndex; + private char carID; + + public UserMoveStrategy(UserInterface userInterface, int carIndex, char carID) { + this.userInterface = userInterface; + this.carIndex = carIndex; + this.carID = carID; + } @Override public Direction nextMove() { - // TODO: implementation - throw new UnsupportedOperationException(); + return userInterface.selectDirection(carIndex, carID); } }