From 0b423b759e0355d5735fe4e3473ba458cc95e492 Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Sun, 20 Mar 2022 03:26:00 +0100 Subject: [PATCH 01/15] started method PathFinderStrategy, (much work needed still no real solution in mind) --- .../strategy/PathFinderStrategy.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java new file mode 100644 index 0000000..084e690 --- /dev/null +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java @@ -0,0 +1,76 @@ +package ch.zhaw.pm2.racetrack.strategy; + +import ch.zhaw.pm2.racetrack.PositionVector; +import ch.zhaw.pm2.racetrack.Track; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +public class PathFinderStrategy implements MoveStrategy{ + private ArrayList possiblePaths; + private Track track; + List directions = Arrays.asList(PositionVector.Direction.values()); + + public PathFinderStrategy(Track track, int carIndex) { + this.track = track; + possiblePaths = new ArrayList<>(); + } + + private void test(){ + ArrayList temporary = new ArrayList<>(); + Iterator it = possiblePaths.iterator(); + while(it.hasNext()) { + tryOutPaths current = it.next(); + if (!current.isFeasible()) { + it.remove(); + } + else { + for (PositionVector.Direction direction : directions) { + temporary.add(current); + temporary.get(temporary.size() - 1).takeDirection(direction); + } + } + } + //ab hier zu löschen + for (tryOutPaths paths : possiblePaths){ + for (PositionVector.Direction direction : directions) { + temporary.add(paths); + temporary.get(temporary.size() - 1).takeDirection(direction); + } + } + //bis hier löschen + possiblePaths.clear(); + possiblePaths.addAll(temporary); + } + + @Override + public PositionVector.Direction nextMove() { + return null; + } + public class tryOutPaths { + ArrayList directionsTaken = new ArrayList<>(); + PositionVector currentPosition; + Track track; + private boolean feasible; + private boolean finished; + public tryOutPaths(Track track){ + this.track = track; + } + public boolean isFeasible(){ + return feasible; + } + public void takeDirection(PositionVector.Direction direction) { + if(directionsTaken.size() >= 50){ + feasible = false; + } + else if(finished){ + } + else { + //check if possible eventuell hier?? + directionsTaken.add(direction); + } + } + } +} -- 2.40.1 From 7c841abfc676db124f8186d5eb5ea4d072d3a6de Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Sun, 20 Mar 2022 16:33:03 +0100 Subject: [PATCH 02/15] continued class PathFinderStrategy --- .../pm2/racetrack/strategy/PathFinderStrategy.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java index 084e690..23c8c9b 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java @@ -21,6 +21,7 @@ public class PathFinderStrategy implements MoveStrategy{ private void test(){ ArrayList temporary = new ArrayList<>(); Iterator it = possiblePaths.iterator(); + while(it.hasNext()) { tryOutPaths current = it.next(); if (!current.isFeasible()) { @@ -29,7 +30,9 @@ public class PathFinderStrategy implements MoveStrategy{ else { for (PositionVector.Direction direction : directions) { temporary.add(current); - temporary.get(temporary.size() - 1).takeDirection(direction); + if (temporary.get(temporary.size() - 1).takeDirection(direction)){ + break; + } } } } @@ -49,6 +52,7 @@ public class PathFinderStrategy implements MoveStrategy{ public PositionVector.Direction nextMove() { return null; } + public class tryOutPaths { ArrayList directionsTaken = new ArrayList<>(); PositionVector currentPosition; @@ -58,18 +62,23 @@ public class PathFinderStrategy implements MoveStrategy{ public tryOutPaths(Track track){ this.track = track; } + public boolean isFeasible(){ return feasible; } - public void takeDirection(PositionVector.Direction direction) { + + public boolean takeDirection(PositionVector.Direction direction) { if(directionsTaken.size() >= 50){ feasible = false; + return false; } else if(finished){ + return true; } else { //check if possible eventuell hier?? directionsTaken.add(direction); + return false; } } } -- 2.40.1 From cf1a8d703e3deac19a614b6a9dbbef6315eb7b0c Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Mon, 21 Mar 2022 01:26:26 +0100 Subject: [PATCH 03/15] continued class PathFinderStrategy --- .../strategy/PathFinderStrategy.java | 83 +++++++++++++------ 1 file changed, 57 insertions(+), 26 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java index 23c8c9b..8f08461 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java @@ -1,5 +1,7 @@ package ch.zhaw.pm2.racetrack.strategy; +import ch.zhaw.pm2.racetrack.Game; +import ch.zhaw.pm2.racetrack.InvalidTrackFormatException; import ch.zhaw.pm2.racetrack.PositionVector; import ch.zhaw.pm2.racetrack.Track; @@ -10,53 +12,74 @@ import java.util.List; public class PathFinderStrategy implements MoveStrategy{ private ArrayList possiblePaths; - private Track track; - List directions = Arrays.asList(PositionVector.Direction.values()); + private Game game; + private boolean foundPath = false; + private List directions = Arrays.asList(PositionVector.Direction.values()); + private ArrayList directionToTake = new ArrayList<>(); + private int currentMove = 0; - public PathFinderStrategy(Track track, int carIndex) { - this.track = track; + public PathFinderStrategy(Game game, int carIndex) { + this.game = game; possiblePaths = new ArrayList<>(); + calculatePaths(); + try { + getDirections(); + } catch (InvalidTrackFormatException e) { + e.printStackTrace(); + } } - private void test(){ - ArrayList temporary = new ArrayList<>(); - Iterator it = possiblePaths.iterator(); - - while(it.hasNext()) { - tryOutPaths current = it.next(); - if (!current.isFeasible()) { - it.remove(); + private void calculatePaths() { + for (int i = 0; i < 50; i++) { + if(foundPath){ + break; } - else { - for (PositionVector.Direction direction : directions) { - temporary.add(current); - if (temporary.get(temporary.size() - 1).takeDirection(direction)){ - break; + + ArrayList temporary = new ArrayList<>(); + Iterator it = possiblePaths.iterator(); + + while (it.hasNext()) { + tryOutPaths current = it.next(); + if (!current.isFeasible()) { + it.remove(); + } else { + for (PositionVector.Direction direction : directions) { + temporary.add(current); + if (temporary.get(temporary.size() - 1).takeDirection(direction)) { + foundPath = true; + } } } } + possiblePaths.clear(); + possiblePaths.addAll(temporary); } - //ab hier zu löschen - for (tryOutPaths paths : possiblePaths){ - for (PositionVector.Direction direction : directions) { - temporary.add(paths); - temporary.get(temporary.size() - 1).takeDirection(direction); + } + + private void getDirections() throws InvalidTrackFormatException { + if(foundPath){ + for (tryOutPaths path: possiblePaths) { + if (path.isFinished()){ + directionToTake = path.getDirectionsTaken(); + } } + } else { + throw new InvalidTrackFormatException(); } - //bis hier löschen - possiblePaths.clear(); - possiblePaths.addAll(temporary); } @Override public PositionVector.Direction nextMove() { - return null; + PositionVector.Direction direction = directionToTake.get(currentMove); + currentMove++; + return direction; } public class tryOutPaths { ArrayList directionsTaken = new ArrayList<>(); PositionVector currentPosition; Track track; + private boolean feasible; private boolean finished; public tryOutPaths(Track track){ @@ -81,5 +104,13 @@ public class PathFinderStrategy implements MoveStrategy{ return false; } } + + public ArrayList getDirectionsTaken(){ + return directionsTaken; + } + + public boolean isFinished(){ + return finished; + } } } -- 2.40.1 From 4b5802a0d085a0b52aab390d8c6f0d362edb03e4 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Thu, 24 Mar 2022 13:59:46 +0100 Subject: [PATCH 04/15] added PathFinderMoveStrategy in Game. --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 14 ++- .../strategy/PathFinderMoveStrategy.java | 40 ++++++ .../strategy/PathFinderStrategy.java | 116 ------------------ 3 files changed, 49 insertions(+), 121 deletions(-) create mode 100644 src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java delete mode 100644 src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 265cd15..e5f0ed1 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -42,6 +42,7 @@ public class Game implements GameSpecification { moveStrategies.add("User Move Strategy"); moveStrategies.add("Move List Strategy"); moveStrategies.add("Path Follow Move Strategy"); + moveStrategies.add("Path Finder Move Strategy"); for (int i = 0; i < track.getCarCount(); i++) { Car car = track.getCar(i); MoveStrategy moveStrategy = null; @@ -49,14 +50,14 @@ public class Game implements GameSpecification { String filePath; int moveStrategie = userInterface.selectOption( "Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies); - switch (moveStrategie + 1) { - case 1: + switch (moveStrategie) { + case 0: moveStrategy = new DoNotMoveStrategy(); break; - case 2: + case 1: moveStrategy = new UserMoveStrategy(userInterface, i, track.getCarId(i)); break; - case 3: + case 2: filePath = ".\\moves\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt"; try { moveStrategy = new MoveListStrategy(filePath); @@ -65,7 +66,7 @@ public class Game implements GameSpecification { } //TODO: Backslash kompatibel für Linux break; - case 4: + case 3: filePath = ".\\follower\\" + selectedTrack.getName().split("\\.")[0] + "_points.txt"; try { moveStrategy = new PathFollowerMoveStrategy(filePath, track.getCarPos(i)); @@ -73,6 +74,9 @@ public class Game implements GameSpecification { userInterface.printInformation("There is no Point-List implemented. Choose another Strategy!"); } break; + case 4: + moveStrategy = new PathFinderMoveStrategy(track, i); + break; } } selectMoveStrategy(car, moveStrategy); diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java new file mode 100644 index 0000000..04e79d8 --- /dev/null +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java @@ -0,0 +1,40 @@ +package ch.zhaw.pm2.racetrack.strategy; + +import ch.zhaw.pm2.racetrack.PositionVector; +import ch.zhaw.pm2.racetrack.Track; + +import java.util.ArrayList; +import java.util.List; + +public class PathFinderMoveStrategy implements MoveStrategy{ + private Track track; + private int carIndex; + private List moveList; + private int pointer; + + + public PathFinderMoveStrategy(Track track, int carIndex) { + this.track = track; + this.carIndex = carIndex; + createMoveList(); + pointer = -1; + } + + private void createMoveList(){ + moveList = new ArrayList<>(); + } + + + + @Override + public PositionVector.Direction nextMove() { + pointer += 1; + //TODO: Check if crash. if yes --> createMoveList(); + if (pointer < moveList.size()) { + return moveList.get(pointer); + } + return null; + } + + +} diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java deleted file mode 100644 index 8f08461..0000000 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderStrategy.java +++ /dev/null @@ -1,116 +0,0 @@ -package ch.zhaw.pm2.racetrack.strategy; - -import ch.zhaw.pm2.racetrack.Game; -import ch.zhaw.pm2.racetrack.InvalidTrackFormatException; -import ch.zhaw.pm2.racetrack.PositionVector; -import ch.zhaw.pm2.racetrack.Track; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; - -public class PathFinderStrategy implements MoveStrategy{ - private ArrayList possiblePaths; - private Game game; - private boolean foundPath = false; - private List directions = Arrays.asList(PositionVector.Direction.values()); - private ArrayList directionToTake = new ArrayList<>(); - private int currentMove = 0; - - public PathFinderStrategy(Game game, int carIndex) { - this.game = game; - possiblePaths = new ArrayList<>(); - calculatePaths(); - try { - getDirections(); - } catch (InvalidTrackFormatException e) { - e.printStackTrace(); - } - } - - private void calculatePaths() { - for (int i = 0; i < 50; i++) { - if(foundPath){ - break; - } - - ArrayList temporary = new ArrayList<>(); - Iterator it = possiblePaths.iterator(); - - while (it.hasNext()) { - tryOutPaths current = it.next(); - if (!current.isFeasible()) { - it.remove(); - } else { - for (PositionVector.Direction direction : directions) { - temporary.add(current); - if (temporary.get(temporary.size() - 1).takeDirection(direction)) { - foundPath = true; - } - } - } - } - possiblePaths.clear(); - possiblePaths.addAll(temporary); - } - } - - private void getDirections() throws InvalidTrackFormatException { - if(foundPath){ - for (tryOutPaths path: possiblePaths) { - if (path.isFinished()){ - directionToTake = path.getDirectionsTaken(); - } - } - } else { - throw new InvalidTrackFormatException(); - } - } - - @Override - public PositionVector.Direction nextMove() { - PositionVector.Direction direction = directionToTake.get(currentMove); - currentMove++; - return direction; - } - - public class tryOutPaths { - ArrayList directionsTaken = new ArrayList<>(); - PositionVector currentPosition; - Track track; - - private boolean feasible; - private boolean finished; - public tryOutPaths(Track track){ - this.track = track; - } - - public boolean isFeasible(){ - return feasible; - } - - public boolean takeDirection(PositionVector.Direction direction) { - if(directionsTaken.size() >= 50){ - feasible = false; - return false; - } - else if(finished){ - return true; - } - else { - //check if possible eventuell hier?? - directionsTaken.add(direction); - return false; - } - } - - public ArrayList getDirectionsTaken(){ - return directionsTaken; - } - - public boolean isFinished(){ - return finished; - } - } -} -- 2.40.1 From ad2e1c31049457e8b950da38a8a6faccdf4adb11 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Thu, 24 Mar 2022 17:09:05 +0100 Subject: [PATCH 05/15] worked on PathFinderMoveStrategy and Refactoring in Game.java --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 28 +++-- .../strategy/PathFinderMoveStrategy.java | 104 +++++++++++++++++- 2 files changed, 116 insertions(+), 16 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index e5f0ed1..c8383df 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -220,7 +220,12 @@ public class Game implements GameSpecification { if (crashPosition != null) { track.carDoesCrash(currentCarIndex, crashPosition); } else { - calculateWinner(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition(), currentCarIndex); + int newWinPoints = calculateNewWinPoints(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition()); + if(newWinPoints == 1){ + track.getCar(currentCarIndex).increaseWinPoints(); + }else{ + track.getCar(currentCarIndex).deductWinPoints(); + } track.moveCar(currentCarIndex); } } @@ -280,43 +285,46 @@ public class Game implements GameSpecification { - private void calculateWinner(PositionVector start, PositionVector finish, int carIndex) { + private int calculateNewWinPoints(PositionVector start, PositionVector finish) { List path = calculatePath(start, finish); for (PositionVector point : path) { if (track.getSpaceType(point) != null) { switch (track.getSpaceType(point)) { case FINISH_UP: if (start.getY() < finish.getY()) { - track.getCar(carIndex).increaseWinPoints(); + return 1; } else if (start.getY() > finish.getY()) { - track.getCar(carIndex).deductWinPoints(); + return -1; } break; case FINISH_DOWN: if (start.getY() > finish.getY()) { - track.getCar(carIndex).increaseWinPoints(); + return 1; } else if (start.getY() < finish.getY()) { - track.getCar(carIndex).deductWinPoints(); + return -1; } break; case FINISH_RIGHT: if (start.getX() < finish.getX()) { - track.getCar(carIndex).increaseWinPoints(); + return 1; } else if (start.getX() > finish.getX()) { - track.getCar(carIndex).deductWinPoints(); + return -1; } break; case FINISH_LEFT: if (start.getX() > finish.getX()) { - track.getCar(carIndex).increaseWinPoints(); + return 1; } else if (start.getX() < finish.getX()) { - track.getCar(carIndex).increaseWinPoints(); + return -1; } break; + default: + return 0; } } } + return 0; } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java index 04e79d8..049abbc 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java @@ -1,36 +1,128 @@ package ch.zhaw.pm2.racetrack.strategy; import ch.zhaw.pm2.racetrack.PositionVector; +import ch.zhaw.pm2.racetrack.PositionVectorNotValid; import ch.zhaw.pm2.racetrack.Track; -import java.util.ArrayList; -import java.util.List; +import java.util.*; public class PathFinderMoveStrategy implements MoveStrategy{ private Track track; private int carIndex; private List moveList; private int pointer; + private List allDirections; public PathFinderMoveStrategy(Track track, int carIndex) { this.track = track; this.carIndex = carIndex; + allDirections = Arrays.asList(PositionVector.Direction.values()); createMoveList(); - pointer = -1; } + private void createMoveList(){ - moveList = new ArrayList<>(); + PossibleMove finishedMove = null; + List possibleMoves= new ArrayList<>(); + for(PositionVector.Direction direction : allDirections){ + possibleMoves.add(new PossibleMove(null, direction)); + } + while(finishedMove == null){ + List newMoves = new ArrayList<>(); + for(PossibleMove previousMove : possibleMoves){ + for(PositionVector.Direction direction : allDirections){ + PossibleMove newMove = new PossibleMove(previousMove, direction); + + if(! (newMove.crashed() || newMove.drivingLoop())){ + newMoves.add(newMove); + } + } + } + possibleMoves = newMoves; + finishedMove = findFinishedMove(possibleMoves); + } + + moveList = finishedMove.directions; + + pointer = moveList.size(); + + + } + + private PossibleMove findFinishedMove(List moveList){ + for(PossibleMove move : moveList){ + if(move.finished()){ + return move; + } + } + return null; + } + + + public class PossibleMove { + List directions; + List positions; + PositionVector endVelocity; + + public PossibleMove(PossibleMove previousMove, PositionVector.Direction nextDirection){ + PositionVector startVelocity; + PositionVector startPosition; + positions = new ArrayList<>(); + directions = new ArrayList<>(); + if(previousMove != null){ + positions.addAll(previousMove.positions); + directions.addAll(previousMove.directions); + startPosition = positions.get(positions.size()-1); + startVelocity = previousMove.endVelocity; + } + else { + startPosition = track.getCarPos(carIndex); + startVelocity = track.getCar(carIndex).getVelocity(); + } + directions.add(nextDirection); + endVelocity = new PositionVector(startVelocity.getX() + nextDirection.vector.getX(), startVelocity.getY() + nextDirection.vector.getY()); + positions.add(new PositionVector(startPosition.getX() + endVelocity.getX(), startPosition.getY() + endVelocity.getY())); + } + + public boolean finished(){ + for(PositionVector finishPosition : track.getFinishLine()){ + if(finishPosition.equals(positions.get(positions.size()-1))){ + return true; //TODO: check direction + } + } + return false; + } + + public boolean crashed() { + try { + if(track.willCrashAtPosition(carIndex, positions.get(positions.size()-1))){ + return true; + } + } catch (PositionVectorNotValid e) { + return true; + } + return false; + } + + public boolean drivingLoop(){ + for(int i = 0; i < positions.size()-1; i++){ + if(positions.get(i).equals(positions.get(positions.size()-1))){ + return true; + } + } + return false; + } + } @Override public PositionVector.Direction nextMove() { - pointer += 1; + pointer -= 1; //TODO: Check if crash. if yes --> createMoveList(); - if (pointer < moveList.size()) { + if (pointer >= 0) { return moveList.get(pointer); } return null; -- 2.40.1 From 82911fcfb6323a8159d5f120abd0de4a7b7f0081 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Thu, 24 Mar 2022 17:45:15 +0100 Subject: [PATCH 06/15] merge Game.java --- .../racetrack/strategy/PathFinderMoveStrategy.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java index 12d07a9..51026d0 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java @@ -1,7 +1,6 @@ package ch.zhaw.pm2.racetrack.strategy; import ch.zhaw.pm2.racetrack.PositionVector; -import ch.zhaw.pm2.racetrack.PositionVectorNotValid; import ch.zhaw.pm2.racetrack.Track; import java.util.*; @@ -93,14 +92,11 @@ public class PathFinderMoveStrategy implements MoveStrategy{ } public boolean crashed() { - try { - for(PositionVector point : track.calculatePointsOnPath(positions.get(positions.size()-1), positions.get(positions.size()-2))) - if(track.willCrashAtPosition(carIndex, point)){ - return true; + for(PositionVector point : track.calculatePointsOnPath(positions.get(positions.size()-1), positions.get(positions.size()-2))) { + if (track.willCrashAtPosition(carIndex, point)) { + return true; + } } - } catch (PositionVectorNotValid e) { - return true; - } return false; } -- 2.40.1 From 7b6fc70e055a97a805105a073cc65e256dc674af Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Thu, 24 Mar 2022 17:53:35 +0100 Subject: [PATCH 07/15] removed PositionVectorNotValidException.java --- .../java/ch/zhaw/pm2/racetrack/GameTest.java | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java b/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java index a956fac..4aca662 100644 --- a/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java +++ b/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java @@ -112,22 +112,14 @@ class GameTest { @Test void carTurnCorrect() { - try { - game.doCarTurn(RIGHT); - Assertions.assertEquals(new PositionVector(1, 0), game.getCarVelocity(0)); - } catch (PositionVectorNotValidException positionVectorNotValidException) { - positionVectorNotValidException.printStackTrace(); - } + game.doCarTurn(RIGHT); + Assertions.assertEquals(new PositionVector(1, 0), game.getCarVelocity(0)); } @Test void carCrash() { - try { - game.doCarTurn(PositionVector.Direction.UP); - Assertions.assertTrue(game.onlyOneCarLeft()); - } catch (PositionVectorNotValidException positionVectorNotValidException) { - positionVectorNotValidException.printStackTrace(); - } + game.doCarTurn(PositionVector.Direction.UP); + Assertions.assertTrue(game.onlyOneCarLeft()); } } -- 2.40.1 From d777b875495ec5647bdb6dcec2e04bfa5012becd Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Thu, 24 Mar 2022 18:18:07 +0100 Subject: [PATCH 08/15] fix in Method calculateNewWinPoints --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 53 +------------- .../java/ch/zhaw/pm2/racetrack/Track.java | 73 ++++++++++--------- .../strategy/PathFinderMoveStrategy.java | 11 +-- 3 files changed, 45 insertions(+), 92 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 4e8f8a8..27b7152 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -224,10 +224,10 @@ public class Game implements GameSpecification { if (crashPosition != null) { track.carDoesCrash(currentCarIndex, crashPosition); } else { - int newWinPoints = calculateNewWinPoints(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition()); + int newWinPoints = track.calculateNewWinPoints(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition()); if(newWinPoints == 1){ track.getCar(currentCarIndex).increaseWinPoints(); - }else{ + }else if(newWinPoints == 1){ track.getCar(currentCarIndex).deductWinPoints(); } track.moveCar(currentCarIndex); @@ -293,55 +293,6 @@ public class Game implements GameSpecification { } - /** - * This method will check if a car is passing the finishline. - * If the car is passing the finishline in the wrong direction, the car will lose a winpoint. - * If the car is passing the finishline in the correct direction, the car will gain a winpoint. - * @param start the startposition of the car - * @param finish the expected finishpositon of the car after the move - * @param carIndex of the current player. - */ - private int calculateNewWinPoints(PositionVector start, PositionVector finish) { - List path = calculatePath(start, finish); - for (PositionVector point : path) { - if (track.getSpaceType(point) != null) { - switch (track.getSpaceType(point)) { - case FINISH_UP: - if (start.getY() < finish.getY()) { - return 1; - } else if (start.getY() > finish.getY()) { - return -1; - } - break; - case FINISH_DOWN: - if (start.getY() > finish.getY()) { - return 1; - } else if (start.getY() < finish.getY()) { - return -1; - } - break; - case FINISH_RIGHT: - if (start.getX() < finish.getX()) { - return 1; - } else if (start.getX() > finish.getX()) { - return -1; - } - break; - case FINISH_LEFT: - if (start.getX() > finish.getX()) { - return 1; - } else if (start.getX() < finish.getX()) { - return -1; - } - break; - default: - return 0; - } - - } - } - return 0; - } /** diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index 56cc5f4..0c5fe2f 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -423,44 +423,49 @@ public class Track implements TrackSpecification { return pathList; } + /** + * This method will check if a car is passing the finishline. + * If the car is passing the finishline in the wrong direction, the car will lose a winpoint. + * If the car is passing the finishline in the correct direction, the car will gain a winpoint. + * @param start the startposition of the car + * @param finish the expected finishpositon of the car after the move + * @return Number of new Winpoints for the current player. + */ public int calculateNewWinPoints(PositionVector start, PositionVector finish) { List path = calculatePointsOnPath(start, finish); for (PositionVector point : path) { - if (getSpaceType(point) != null) { - switch (getSpaceType(point)) { - case FINISH_UP: - if (start.getY() < finish.getY()) { - return 1; - } else if (start.getY() > finish.getY()) { - return -1; - } - break; - case FINISH_DOWN: - if (start.getY() > finish.getY()) { - return 1; - } else if (start.getY() < finish.getY()) { - return -1; - } - break; - case FINISH_RIGHT: - if (start.getX() < finish.getX()) { - return 1; - } else if (start.getX() > finish.getX()) { - return -1; - } - break; - case FINISH_LEFT: - if (start.getX() > finish.getX()) { - return 1; - } else if (start.getX() < finish.getX()) { - return -1; - } - break; - default: - return 0; - } - + switch (getSpaceType(point)) { + case FINISH_UP: + if (start.getY() < finish.getY()) { + return 1; + } else if (start.getY() > finish.getY()) { + return -1; + } + break; + case FINISH_DOWN: + if (start.getY() > finish.getY()) { + return 1; + } else if (start.getY() < finish.getY()) { + return -1; + } + break; + case FINISH_RIGHT: + if (start.getX() < finish.getX()) { + return 1; + } else if (start.getX() > finish.getX()) { + return -1; + } + break; + case FINISH_LEFT: + if (start.getX() > finish.getX()) { + return 1; + } else if (start.getX() < finish.getX()) { + return -1; + } + break; } + + } return 0; } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java index 51026d0..bb020ae 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java @@ -18,6 +18,7 @@ public class PathFinderMoveStrategy implements MoveStrategy{ this.carIndex = carIndex; allDirections = Arrays.asList(PositionVector.Direction.values()); createMoveList(); + pointer = -1; } @@ -32,7 +33,6 @@ public class PathFinderMoveStrategy implements MoveStrategy{ for(PossibleMove previousMove : possibleMoves){ for(PositionVector.Direction direction : allDirections){ PossibleMove newMove = new PossibleMove(previousMove, direction); - if(! (newMove.crashed() || newMove.drivingLoop())){ newMoves.add(newMove); } @@ -44,8 +44,6 @@ public class PathFinderMoveStrategy implements MoveStrategy{ moveList = finishedMove.directions; - pointer = moveList.size(); - } @@ -92,7 +90,7 @@ public class PathFinderMoveStrategy implements MoveStrategy{ } public boolean crashed() { - for(PositionVector point : track.calculatePointsOnPath(positions.get(positions.size()-1), positions.get(positions.size()-2))) { + for(PositionVector point : track.calculatePointsOnPath(positions.get(positions.size()-2), positions.get(positions.size()-1))) { if (track.willCrashAtPosition(carIndex, point)) { return true; } @@ -115,9 +113,8 @@ public class PathFinderMoveStrategy implements MoveStrategy{ @Override public PositionVector.Direction nextMove() { - pointer -= 1; - //TODO: Check if crash. if yes --> createMoveList(); - if (pointer >= 0) { + pointer += 1; + if (pointer < moveList.size()) { return moveList.get(pointer); } return null; -- 2.40.1 From 19eb41066c208fadbef8521fce51478ecfd9fa93 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Thu, 24 Mar 2022 18:45:59 +0100 Subject: [PATCH 09/15] GameTest.java correct --- src/test/java/ch/zhaw/pm2/racetrack/GameTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java b/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java index 4aca662..78b580b 100644 --- a/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java +++ b/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java @@ -133,7 +133,7 @@ class GameTest { @Test void winner() { - game = new Game(new interFace("Test",new Integer[]{0,2,1},new PositionVector.Direction[]{RIGHT, + game = new Game(new interFace("Test",new Integer[]{0,2,0},new PositionVector.Direction[]{RIGHT, RIGHT, RIGHT, NONE, @@ -178,7 +178,7 @@ class GameTest { @Test void crashA() { - game = new Game(new interFace("Test",new Integer[]{0,2,2},new PositionVector.Direction[]{UP})); + game = new Game(new interFace("Test",new Integer[]{0,1,0},new PositionVector.Direction[]{UP})); game.initPhase(); Assertions.assertEquals("b",game.gamePhase()); } -- 2.40.1 From 1a3a96218cc297b2587c09b7b786ee79633e73e1 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Thu, 24 Mar 2022 18:49:07 +0100 Subject: [PATCH 10/15] refactor Track --- src/main/java/ch/zhaw/pm2/racetrack/Track.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index 0c5fe2f..1012840 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -275,7 +275,6 @@ public class Track implements TrackSpecification { */ @Override public Config.SpaceType getSpaceType(PositionVector position) { - //isPositionVectorOnTrack(position); Should be used but we are not allowed to change method head. We don't use function anyway char charAtPosition = track.get(position.getY()).charAt(position.getX()); ConfigSpecification.SpaceType[] spaceTypes = ConfigSpecification.SpaceType.values(); for (ConfigSpecification.SpaceType spaceType : spaceTypes) { @@ -284,7 +283,7 @@ public class Track implements TrackSpecification { } } - return null; + return ConfigSpecification.SpaceType.WALL; } /** -- 2.40.1 From e84bce43afe8614f3b38624f6daa8d08fae713c0 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Thu, 24 Mar 2022 19:04:43 +0100 Subject: [PATCH 11/15] fix getWinner in Game.java working on PathFinderMoveStrategy --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 18 +++++++-------- .../java/ch/zhaw/pm2/racetrack/Track.java | 2 +- .../strategy/PathFinderMoveStrategy.java | 22 +++++++++---------- tracks/quarter-mile.txt | 2 +- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 27b7152..c644e6e 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -169,14 +169,14 @@ public class Game implements GameSpecification { */ @Override public int getWinner() { - if (onlyOneCarLeft()) { - return currentCarIndex; - } for (int i = 0; i < track.getCarCount(); i++) { if (track.getCar(i).getWinPoints() == 1) { return i; } } + if (onlyOneCarLeft()) { + return currentCarIndex; + } return NO_WINNER; } @@ -221,15 +221,15 @@ public class Game implements GameSpecification { break; } } + int newWinPoints = track.calculateNewWinPoints(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition()); + if(newWinPoints == 1){ + track.getCar(currentCarIndex).increaseWinPoints(); + }else if(newWinPoints == -1){ + track.getCar(currentCarIndex).deductWinPoints(); + } if (crashPosition != null) { track.carDoesCrash(currentCarIndex, crashPosition); } else { - int newWinPoints = track.calculateNewWinPoints(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition()); - if(newWinPoints == 1){ - track.getCar(currentCarIndex).increaseWinPoints(); - }else if(newWinPoints == 1){ - track.getCar(currentCarIndex).deductWinPoints(); - } track.moveCar(currentCarIndex); } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index 0c5fe2f..af8773b 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -284,7 +284,7 @@ public class Track implements TrackSpecification { } } - return null; + return ConfigSpecification.SpaceType.WALL; } /** diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java index bb020ae..b9339a5 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java @@ -33,13 +33,17 @@ public class PathFinderMoveStrategy implements MoveStrategy{ for(PossibleMove previousMove : possibleMoves){ for(PositionVector.Direction direction : allDirections){ PossibleMove newMove = new PossibleMove(previousMove, direction); - if(! (newMove.crashed() || newMove.drivingLoop())){ + if(! (newMove.crashed() || newMove.drivingLoop() || finishedMove != null)){ + if(newMove.finished()){ + finishedMove = newMove; + break; + } newMoves.add(newMove); } } } possibleMoves = newMoves; - finishedMove = findFinishedMove(possibleMoves); + } moveList = finishedMove.directions; @@ -47,15 +51,6 @@ public class PathFinderMoveStrategy implements MoveStrategy{ } - private PossibleMove findFinishedMove(List moveList){ - for(PossibleMove move : moveList){ - if(move.finished()){ - return move; - } - } - return null; - } - public class PossibleMove { List directions; @@ -91,10 +86,13 @@ public class PathFinderMoveStrategy implements MoveStrategy{ public boolean crashed() { for(PositionVector point : track.calculatePointsOnPath(positions.get(positions.size()-2), positions.get(positions.size()-1))) { - if (track.willCrashAtPosition(carIndex, point)) { + if (track.willCrashAtPosition(carIndex, point)){ return true; } } + if(track.calculateNewWinPoints(positions.get(positions.size()-2), positions.get(positions.size()-1)) == -1){ + return true; + } return false; } diff --git a/tracks/quarter-mile.txt b/tracks/quarter-mile.txt index 5a55e57..97d816b 100644 --- a/tracks/quarter-mile.txt +++ b/tracks/quarter-mile.txt @@ -1,7 +1,7 @@ ############################################################ ############################################################ ####### < ## -####### < § ## +####### < § ## ####### < ## ####### < @ ## ####### < ## -- 2.40.1 From 7c8cf55ac2bb9e847b2b367e29f20da6aa69a648 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Thu, 24 Mar 2022 19:14:36 +0100 Subject: [PATCH 12/15] fix calculateNewWinPoints --- src/main/java/ch/zhaw/pm2/racetrack/Track.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index 1012840..a403626 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -435,16 +435,16 @@ public class Track implements TrackSpecification { for (PositionVector point : path) { switch (getSpaceType(point)) { case FINISH_UP: - if (start.getY() < finish.getY()) { + if (start.getY() > finish.getY()) { return 1; - } else if (start.getY() > finish.getY()) { + } else if (start.getY() < finish.getY()) { return -1; } break; case FINISH_DOWN: - if (start.getY() > finish.getY()) { + if (start.getY() < finish.getY()) { return 1; - } else if (start.getY() < finish.getY()) { + } else if (start.getY() > finish.getY()) { return -1; } break; -- 2.40.1 From b01bee0142abc46d538f0a6cef38e15fbf91a3d6 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 25 Mar 2022 00:02:04 +0100 Subject: [PATCH 13/15] fixed PathFinderMoveStrategy --- .../strategy/PathFinderMoveStrategy.java | 86 +++++++++++++------ 1 file changed, 59 insertions(+), 27 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java index b9339a5..6d8dc0e 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java @@ -3,7 +3,9 @@ package ch.zhaw.pm2.racetrack.strategy; import ch.zhaw.pm2.racetrack.PositionVector; import ch.zhaw.pm2.racetrack.Track; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; public class PathFinderMoveStrategy implements MoveStrategy{ private Track track; @@ -11,6 +13,7 @@ public class PathFinderMoveStrategy implements MoveStrategy{ private List moveList; private int pointer; private List allDirections; + private List calculatedStates; public PathFinderMoveStrategy(Track track, int carIndex) { @@ -23,22 +26,29 @@ public class PathFinderMoveStrategy implements MoveStrategy{ private void createMoveList(){ + calculatedStates = new ArrayList<>(); PossibleMove finishedMove = null; List possibleMoves= new ArrayList<>(); for(PositionVector.Direction direction : allDirections){ - possibleMoves.add(new PossibleMove(null, direction)); + PossibleMove newMove = new PossibleMove(null, direction); + if(! newMove.crashed()){ + possibleMoves.add(newMove); + } + } while(finishedMove == null){ List newMoves = new ArrayList<>(); for(PossibleMove previousMove : possibleMoves){ for(PositionVector.Direction direction : allDirections){ PossibleMove newMove = new PossibleMove(previousMove, direction); - if(! (newMove.crashed() || newMove.drivingLoop() || finishedMove != null)){ + State newState = new State(newMove.endPosition, newMove.endVelocity); + if(! (newMove.crashed() || alreadyCalculated(newState) || finishedMove != null)){ if(newMove.finished()){ finishedMove = newMove; - break; + } else { + calculatedStates.add(newState); + newMoves.add(newMove); } - newMoves.add(newMove); } } } @@ -46,26 +56,54 @@ public class PathFinderMoveStrategy implements MoveStrategy{ } + moveList = finishedMove.directions; } + private boolean alreadyCalculated(State state){ + for(State calculatedState: calculatedStates){ + if(state.equals(calculatedState)){ + return true; + } + } + return false; + } + + public class State{ + PositionVector position; + PositionVector velocity; + + public State(PositionVector position, PositionVector velocity){ + this.position = position; + this.velocity = velocity; + } + + public boolean equals(State compareState){ + if(compareState.position.equals(position) && compareState.velocity.equals(velocity)){ + return true; + } else{ + return false; + } + } + } + public class PossibleMove { List directions; - List positions; + PositionVector startPosition; + PositionVector endPosition; PositionVector endVelocity; + public PossibleMove(PossibleMove previousMove, PositionVector.Direction nextDirection){ PositionVector startVelocity; - PositionVector startPosition; - positions = new ArrayList<>(); + directions = new ArrayList<>(); if(previousMove != null){ - positions.addAll(previousMove.positions); directions.addAll(previousMove.directions); - startPosition = positions.get(positions.size()-1); + startPosition = previousMove.endPosition; startVelocity = previousMove.endVelocity; } else { @@ -74,34 +112,28 @@ public class PathFinderMoveStrategy implements MoveStrategy{ } directions.add(nextDirection); endVelocity = new PositionVector(startVelocity.getX() + nextDirection.vector.getX(), startVelocity.getY() + nextDirection.vector.getY()); - positions.add(new PositionVector(startPosition.getX() + endVelocity.getX(), startPosition.getY() + endVelocity.getY())); + endPosition = new PositionVector(startPosition.getX() + endVelocity.getX(), startPosition.getY() + endVelocity.getY()); } public boolean finished(){ - if(track.calculateNewWinPoints(positions.get(positions.size()-2), positions.get(positions.size()-1)) == 1){ + if(track.calculateNewWinPoints(startPosition, endPosition) == 1){ return true; } - else return false; + else{ + return false; + } } public boolean crashed() { - for(PositionVector point : track.calculatePointsOnPath(positions.get(positions.size()-2), positions.get(positions.size()-1))) { - if (track.willCrashAtPosition(carIndex, point)){ - return true; - } - } - if(track.calculateNewWinPoints(positions.get(positions.size()-2), positions.get(positions.size()-1)) == -1){ - return true; - } - return false; - } - - public boolean drivingLoop(){ - for(int i = 0; i < positions.size()-1; i++){ - if(positions.get(i).equals(positions.get(positions.size()-1))){ + List points = track.calculatePointsOnPath(startPosition, endPosition); + for(PositionVector point : points) { + if (track.willCrashAtPosition(carIndex, point)){ return true; } } + if(track.calculateNewWinPoints(startPosition, endPosition) == -1){ + return true; + } return false; } -- 2.40.1 From 448e34e869cd099f1c6fe3426ae7c294be475389 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 25 Mar 2022 00:10:07 +0100 Subject: [PATCH 14/15] fixed PathFinderMoveStrategy --- .../ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java index 6d8dc0e..ed844fe 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java @@ -142,7 +142,7 @@ public class PathFinderMoveStrategy implements MoveStrategy{ @Override - public PositionVector.Direction nextMove() { + public PositionVector.Direction nextMove() { //TODO check for crash and recreate movelist if crash pointer += 1; if (pointer < moveList.size()) { return moveList.get(pointer); -- 2.40.1 From d6a9b5f1aa9c52093ee67d1b38ec6a35e6b7f46e Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 25 Mar 2022 08:58:01 +0100 Subject: [PATCH 15/15] fixed PathFinderMoveStrategy --- .../strategy/PathFinderMoveStrategy.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java index ed844fe..4148124 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java @@ -21,11 +21,10 @@ public class PathFinderMoveStrategy implements MoveStrategy{ this.carIndex = carIndex; allDirections = Arrays.asList(PositionVector.Direction.values()); createMoveList(); - pointer = -1; } - private void createMoveList(){ + pointer = -1; calculatedStates = new ArrayList<>(); PossibleMove finishedMove = null; List possibleMoves= new ArrayList<>(); @@ -145,6 +144,22 @@ public class PathFinderMoveStrategy implements MoveStrategy{ public PositionVector.Direction nextMove() { //TODO check for crash and recreate movelist if crash pointer += 1; if (pointer < moveList.size()) { + PositionVector.Direction direction = moveList.get(pointer); + PositionVector currentVelocity = track.getCarVelocity(carIndex); + PositionVector newVelocity = new PositionVector(currentVelocity.getX() + direction.vector.getX(), currentVelocity.getY() + direction.vector.getY()); + PositionVector currentPosition = track.getCarPos(carIndex); + PositionVector newPosition = new PositionVector(currentPosition.getX() + newVelocity.getX(), currentPosition.getY() + newVelocity.getY()); + System.out.println("currentVelocity:" + currentVelocity.getX()+ ","+ currentVelocity.getY()); + System.out.println("newVelocity:" + newVelocity.getX()+ ","+ newVelocity.getY()); + for(PositionVector point : track.calculatePointsOnPath(currentPosition, newPosition)){ + if(track.willCrashAtPosition(carIndex, point)){ + createMoveList(); + pointer = 0; + break; + } + } + + return moveList.get(pointer); } return null; -- 2.40.1