From fa4842e440a2f5d79a0e1d1ad619b01fd6083f15 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Thu, 24 Mar 2022 13:32:12 +0100 Subject: [PATCH] refactoring of Method calculatePath from Game.java to Track.java --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 59 +------------------ .../java/ch/zhaw/pm2/racetrack/Track.java | 59 +++++++++++++++++++ 2 files changed, 62 insertions(+), 56 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index d10ec86..265cd15 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -271,64 +271,11 @@ public class Game implements GameSpecification { */ @Override public List calculatePath(PositionVector startPosition, PositionVector endPosition) { - ArrayList pathList = new ArrayList<>(); - // Use Bresenham's algorithm to determine positions. - int x = startPosition.getX(); - int y = startPosition.getY(); - - // Relative Distance (x & y axis) between end- and starting position - int diffX = endPosition.getX() - startPosition.getX(); - int diffY = endPosition.getY() - startPosition.getY(); - - // Absolute distance (x & y axis) between end- and starting position - int distX = Math.abs(diffX); - int distY = Math.abs(diffY); - - // Direction of vector on x & y axis (-1: to left/down, 0: none, +1 : to right/up) - int dirX = Integer.signum(diffX); - int dirY = Integer.signum(diffY); - - // Determine which axis is the fast direction and set parallel/diagonal step values - int parallelStepX, parallelStepY; - int diagonalStepX, diagonalStepY; - int distanceSlowAxis, distanceFastAxis; - if (distX > distY) { - // x axis is the 'fast' direction - parallelStepX = dirX; - parallelStepY = 0; // parallel step only moves in x direction - diagonalStepX = dirX; - diagonalStepY = dirY; // diagonal step moves in both directions - distanceSlowAxis = distY; - distanceFastAxis = distX; - } else { - // y axis is the 'fast' direction - parallelStepX = 0; - parallelStepY = dirY; // parallel step only moves in y direction - diagonalStepX = dirX; - diagonalStepY = dirY; // diagonal step moves in both directions - distanceSlowAxis = distX; - distanceFastAxis = distY; - } - - int error = distanceFastAxis / 2; - for (int step = 0; step < distanceFastAxis; step++) { - error -= distanceSlowAxis; - if (error < 0) { - error += distanceFastAxis; // correct error value to be positive again - // step into slow direction; diagonal step - x += diagonalStepX; - y += diagonalStepY; - } else { - // step into fast direction; parallel step - x += parallelStepX; - y += parallelStepY; - } - - pathList.add(new PositionVector(x, y)); - } - return pathList; + return track.calculatePointsOnPath(startPosition, endPosition); } + + private void calculateWinner(PositionVector start, PositionVector finish, int carIndex) { List path = calculatePath(start, finish); for (PositionVector point : path) { diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index c83839e..e0a6ff4 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -355,6 +355,65 @@ public class Track implements TrackSpecification { return currentSpace.getValue(); } + public ArrayList calculatePointsOnPath(PositionVector startPosition, PositionVector endPosition) { + ArrayList pathList = new ArrayList<>(); + // Use Bresenham's algorithm to determine positions. + int x = startPosition.getX(); + int y = startPosition.getY(); + + // Relative Distance (x & y axis) between end- and starting position + int diffX = endPosition.getX() - startPosition.getX(); + int diffY = endPosition.getY() - startPosition.getY(); + + // Absolute distance (x & y axis) between end- and starting position + int distX = Math.abs(diffX); + int distY = Math.abs(diffY); + + // Direction of vector on x & y axis (-1: to left/down, 0: none, +1 : to right/up) + int dirX = Integer.signum(diffX); + int dirY = Integer.signum(diffY); + + // Determine which axis is the fast direction and set parallel/diagonal step values + int parallelStepX, parallelStepY; + int diagonalStepX, diagonalStepY; + int distanceSlowAxis, distanceFastAxis; + if (distX > distY) { + // x axis is the 'fast' direction + parallelStepX = dirX; + parallelStepY = 0; // parallel step only moves in x direction + diagonalStepX = dirX; + diagonalStepY = dirY; // diagonal step moves in both directions + distanceSlowAxis = distY; + distanceFastAxis = distX; + } else { + // y axis is the 'fast' direction + parallelStepX = 0; + parallelStepY = dirY; // parallel step only moves in y direction + diagonalStepX = dirX; + diagonalStepY = dirY; // diagonal step moves in both directions + distanceSlowAxis = distX; + distanceFastAxis = distY; + } + + int error = distanceFastAxis / 2; + for (int step = 0; step < distanceFastAxis; step++) { + error -= distanceSlowAxis; + if (error < 0) { + error += distanceFastAxis; // correct error value to be positive again + // step into slow direction; diagonal step + x += diagonalStepX; + y += diagonalStepY; + } else { + // step into fast direction; parallel step + x += parallelStepX; + y += parallelStepY; + } + + pathList.add(new PositionVector(x, y)); + } + return pathList; + } + /** * Return a String representation of the track, including the car locations. *