Merge remote-tracking branch 'origin/main'

# Conflicts:
#	src/main/java/ch/zhaw/pm2/racetrack/InvalidFileFormatException.java
This commit is contained in:
Leonardo Brandenberger 2022-03-25 22:04:18 +01:00
commit 6560de8708
3 changed files with 47 additions and 35 deletions

View File

@ -33,6 +33,7 @@ public final class PositionVector {
/** /**
* Adds two PositionVectors (e.g. car position and velocity vector or two velocity vectors). * Adds two PositionVectors (e.g. car position and velocity vector or two velocity vectors).
*
* @param vectorA A position or velocity vector * @param vectorA A position or velocity vector
* @param vectorB A position or velocity vector * @param vectorB A position or velocity vector
* @return A new PositionVector holding the result of the addition. If both * @return A new PositionVector holding the result of the addition. If both
@ -45,6 +46,7 @@ public final class PositionVector {
/** /**
* Subtracts two PositionVectors (e.g. car position and velocity vector or two velocity vectors). * Subtracts two PositionVectors (e.g. car position and velocity vector or two velocity vectors).
*
* @param vectorA A position or velocity vector * @param vectorA A position or velocity vector
* @param vectorB A position or velocity vector * @param vectorB A position or velocity vector
* @return A new PositionVector holding the result of the addition. If both * @return A new PositionVector holding the result of the addition. If both
@ -58,6 +60,7 @@ public final class PositionVector {
/** /**
* Calculates the scalar product (Skalarprodukt) of two 2D vectors. The scalar product * Calculates the scalar product (Skalarprodukt) of two 2D vectors. The scalar product
* multiplies the lengths of the parallel components of the vectors. * multiplies the lengths of the parallel components of the vectors.
*
* @param vectorA A position or velocity vector * @param vectorA A position or velocity vector
* @param vectorB A position or velocity vector * @param vectorB A position or velocity vector
* @return The scalar product (vectorA * vectorB). Since vectorA and * @return The scalar product (vectorA * vectorB). Since vectorA and
@ -75,6 +78,7 @@ public final class PositionVector {
/** /**
* Copy constructor * Copy constructor
*
* @param other * @param other
*/ */
public PositionVector(final PositionVector other) { public PositionVector(final PositionVector other) {

View File

@ -11,6 +11,7 @@ public class DoNotMoveStrategy implements MoveStrategy {
/** /**
* This method will be used to return the next Direction for the car. * This method will be used to return the next Direction for the car.
*
* @return a NONE Direction * @return a NONE Direction
*/ */
@Override @Override

View File

@ -9,7 +9,7 @@ import java.util.List;
/** /**
* Strategy which calculates the path automatically * Strategy which calculates the path automatically
*/ */
public class PathFinderMoveStrategy implements MoveStrategy{ public class PathFinderMoveStrategy implements MoveStrategy {
private final Track track; private final Track track;
private final int carIndex; private final int carIndex;
private List<PositionVector.Direction> moveList; private List<PositionVector.Direction> moveList;
@ -23,6 +23,7 @@ public class PathFinderMoveStrategy implements MoveStrategy{
/** /**
* Constructor which initialises the needed variables and creates the MoveList * Constructor which initialises the needed variables and creates the MoveList
*
* @param track track instance of the game * @param track track instance of the game
* @param carIndex index of the car which owns this moveStrategy * @param carIndex index of the car which owns this moveStrategy
*/ */
@ -36,17 +37,17 @@ public class PathFinderMoveStrategy implements MoveStrategy{
/** /**
* Method to create a working moveList * Method to create a working moveList
*/ */
private void createMoveList(){ private void createMoveList() {
// if Movelist is recreated next move will be the first move in moveList // if Movelist is recreated next move will be the first move in moveList
pointer = 0; pointer = 0;
calculatedStates = new ArrayList<>(); calculatedStates = new ArrayList<>();
PossibleMove finishedMove = null; PossibleMove finishedMove = null;
List<PossibleMove> possibleMoves= new ArrayList<>(); List<PossibleMove> possibleMoves = new ArrayList<>();
// create a PossibleMove object for each direction which doesn't end with a crash. // create a PossibleMove object for each direction which doesn't end with a crash.
for(PositionVector.Direction direction : allDirections){ for (PositionVector.Direction direction : allDirections) {
PossibleMove newMove = new PossibleMove(null, direction); PossibleMove newMove = new PossibleMove(null, direction);
if(! newMove.crashed()){ if (!newMove.crashed()) {
possibleMoves.add(newMove); possibleMoves.add(newMove);
} }
@ -54,16 +55,16 @@ public class PathFinderMoveStrategy implements MoveStrategy{
// while no PossibleMove crosses the finishline // while no PossibleMove crosses the finishline
// every PossibleMove will be accelerated in each direction to find a Move which finishes. // every PossibleMove will be accelerated in each direction to find a Move which finishes.
while(finishedMove == null){ while (finishedMove == null) {
List<PossibleMove> newMoves = new ArrayList<>(); List<PossibleMove> newMoves = new ArrayList<>();
for(PossibleMove previousMove : possibleMoves){ for (PossibleMove previousMove : possibleMoves) {
for(PositionVector.Direction direction : allDirections){ for (PositionVector.Direction direction : allDirections) {
PossibleMove newMove = new PossibleMove(previousMove, direction); PossibleMove newMove = new PossibleMove(previousMove, direction);
State newState = new State(newMove.endPosition, newMove.endVelocity); State newState = new State(newMove.endPosition, newMove.endVelocity);
//only use the new created Possible Move if it doesn't crash, end State isn't in List of calculatedStates //only use the new created Possible Move if it doesn't crash, end State isn't in List of calculatedStates
// and if there is no move found yet which is finished. // and if there is no move found yet which is finished.
if(! (newMove.crashed() || alreadyCalculated(newState) || finishedMove != null)){ if (!(newMove.crashed() || alreadyCalculated(newState) || finishedMove != null)) {
if(newMove.finished()){ if (newMove.finished()) {
finishedMove = newMove; finishedMove = newMove;
} else { } else {
calculatedStates.add(newState); calculatedStates.add(newState);
@ -82,12 +83,13 @@ public class PathFinderMoveStrategy implements MoveStrategy{
/** /**
* Method to check if a State is already in List calculatedStates * Method to check if a State is already in List calculatedStates
*
* @param state the State which should be checked * @param state the State which should be checked
* @return true if it is in List, false if it isn't in List * @return true if it is in List, false if it isn't in List
*/ */
private boolean alreadyCalculated(State state){ private boolean alreadyCalculated(State state) {
for(State calculatedState: calculatedStates){ for (State calculatedState : calculatedStates) {
if(state.equals(calculatedState)){ if (state.equals(calculatedState)) {
return true; return true;
} }
} }
@ -97,26 +99,28 @@ public class PathFinderMoveStrategy implements MoveStrategy{
/** /**
* Combination of position and velocity * Combination of position and velocity
*/ */
public static class State{ public static class State {
final PositionVector position; final PositionVector position;
final PositionVector velocity; final PositionVector velocity;
/** /**
* Constructor of State * Constructor of State
*
* @param position the PositionVector object with coordinates of the Position * @param position the PositionVector object with coordinates of the Position
* @param velocity the PositionVector object with coordinates of the Velocity * @param velocity the PositionVector object with coordinates of the Velocity
*/ */
public State(PositionVector position, PositionVector velocity){ public State(PositionVector position, PositionVector velocity) {
this.position = position; this.position = position;
this.velocity = velocity; this.velocity = velocity;
} }
/** /**
* Checks if a state has the same Position and the same Velocity * Checks if a state has the same Position and the same Velocity
*
* @param compareState the State object to compare * @param compareState the State object to compare
* @return true if it is equal, false if it is not equal * @return true if it is equal, false if it is not equal
*/ */
public boolean equals(State compareState){ public boolean equals(State compareState) {
return compareState.position.equals(position) && compareState.velocity.equals(velocity); return compareState.position.equals(position) && compareState.velocity.equals(velocity);
} }
} }
@ -136,21 +140,21 @@ public class PathFinderMoveStrategy implements MoveStrategy{
/** /**
* Constructor of PossibleMove * Constructor of PossibleMove
*
* @param previousMove The move which must be executed bevor this move can be executed * @param previousMove The move which must be executed bevor this move can be executed
* @param nextDirection The direction of the move * @param nextDirection The direction of the move
*/ */
public PossibleMove(PossibleMove previousMove, PositionVector.Direction nextDirection){ public PossibleMove(PossibleMove previousMove, PositionVector.Direction nextDirection) {
// Velocity of the car bevor the move is executed // Velocity of the car bevor the move is executed
PositionVector startVelocity; PositionVector startVelocity;
directions = new ArrayList<>(); directions = new ArrayList<>();
// check if there was a previousMove. // check if there was a previousMove.
if(previousMove != null){ if (previousMove != null) {
directions.addAll(previousMove.directions); //copy the LIst of Directions from the previousMove directions.addAll(previousMove.directions); //copy the LIst of Directions from the previousMove
startPosition = previousMove.endPosition; //use the endPosition from previousMove as startPosition startPosition = previousMove.endPosition; //use the endPosition from previousMove as startPosition
startVelocity = previousMove.endVelocity; //use the endVelocity from previousMove as startVelocity startVelocity = previousMove.endVelocity; //use the endVelocity from previousMove as startVelocity
} } else { //if there was no previousMove
else { //if there was no previousMove
startPosition = track.getCarPos(carIndex); //use the current Position of the car from track as startPosition startPosition = track.getCarPos(carIndex); //use the current Position of the car from track as startPosition
startVelocity = track.getCar(carIndex).getVelocity(); //use the current Velocity of the car from track as startVelocity startVelocity = track.getCar(carIndex).getVelocity(); //use the current Velocity of the car from track as startVelocity
} }
@ -161,20 +165,22 @@ public class PathFinderMoveStrategy implements MoveStrategy{
/** /**
* check if the finishline is crossed (in correct direction) if this move is executed * check if the finishline is crossed (in correct direction) if this move is executed
*
* @return true if finishline will be crossed * @return true if finishline will be crossed
*/ */
public boolean finished(){ public boolean finished() {
return track.calculateNewWinPoints(startPosition, endPosition) == 1; return track.calculateNewWinPoints(startPosition, endPosition) == 1;
} }
/** /**
* checks if the car will crash or finishline will be crossed in wrong direction if this move is executed * checks if the car will crash or finishline will be crossed in wrong direction if this move is executed
*
* @return true if car will crash * @return true if car will crash
*/ */
public boolean crashed() { public boolean crashed() {
List<PositionVector> points = track.calculatePointsOnPath(startPosition, endPosition); List<PositionVector> points = track.calculatePointsOnPath(startPosition, endPosition);
for(PositionVector point : points) { for (PositionVector point : points) {
if (track.willCrashAtPosition(carIndex, point)){ if (track.willCrashAtPosition(carIndex, point)) {
return true; return true;
} }
} }
@ -187,6 +193,7 @@ public class PathFinderMoveStrategy implements MoveStrategy{
/** /**
* Checks if the next Move in moveList will crash. If no crash next move in moveList will be executed. * Checks if the next Move in moveList will crash. If no crash next move in moveList will be executed.
* If crash the moveList will be recreated. * If crash the moveList will be recreated.
*
* @return the direction of acceleration which should be executed. * @return the direction of acceleration which should be executed.
*/ */
@Override @Override
@ -197,8 +204,8 @@ public class PathFinderMoveStrategy implements MoveStrategy{
PositionVector newVelocity = new PositionVector(currentVelocity.getX() + direction.vector.getX(), currentVelocity.getY() + direction.vector.getY()); PositionVector newVelocity = new PositionVector(currentVelocity.getX() + direction.vector.getX(), currentVelocity.getY() + direction.vector.getY());
PositionVector currentPosition = track.getCarPos(carIndex); PositionVector currentPosition = track.getCarPos(carIndex);
PositionVector newPosition = new PositionVector(currentPosition.getX() + newVelocity.getX(), currentPosition.getY() + newVelocity.getY()); PositionVector newPosition = new PositionVector(currentPosition.getX() + newVelocity.getX(), currentPosition.getY() + newVelocity.getY());
for(PositionVector point : track.calculatePointsOnPath(currentPosition, newPosition)){ for (PositionVector point : track.calculatePointsOnPath(currentPosition, newPosition)) {
if(track.willCrashAtPosition(carIndex, point)){ if (track.willCrashAtPosition(carIndex, point)) {
createMoveList(); createMoveList();
pointer = 0; pointer = 0;
direction = moveList.get(pointer); direction = moveList.get(pointer);