From f5cb7e6f80d43b951c863f6ce2ed7be4f9cbd9c5 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Fri, 6 Mar 2020 10:20:30 +0100 Subject: [PATCH] Checkstyle --- src/edu/kit/informatik/Terminal.java | 2 + .../kit/informatik/model/RailwayNetwork.java | 39 ++++++++++++-- src/edu/kit/informatik/model/Train.java | 54 +++++++++---------- .../kit/informatik/model/TrainManager.java | 2 +- src/edu/kit/informatik/model/Vector2D.java | 10 ++++ src/edu/kit/informatik/ui/CommandLine.java | 3 -- 6 files changed, 71 insertions(+), 39 deletions(-) diff --git a/src/edu/kit/informatik/Terminal.java b/src/edu/kit/informatik/Terminal.java index 25bad30..eac6c3a 100644 --- a/src/edu/kit/informatik/Terminal.java +++ b/src/edu/kit/informatik/Terminal.java @@ -100,6 +100,8 @@ public final class Terminal { String line = IN.readLine(); if ("breakpoint".equals(line)) { return readLine(); + } else if (line != null && line.startsWith("#")) { + return readLine(); } return line; } catch (final IOException e) { diff --git a/src/edu/kit/informatik/model/RailwayNetwork.java b/src/edu/kit/informatik/model/RailwayNetwork.java index 8d919e2..b1fb5d3 100644 --- a/src/edu/kit/informatik/model/RailwayNetwork.java +++ b/src/edu/kit/informatik/model/RailwayNetwork.java @@ -234,7 +234,40 @@ public class RailwayNetwork { } /** - * Find a rail that *contains* (not touch) this position. + * Check whether a train can be placed at the specified position in the specified direction. + * + * @param position front position of the train + * @param direction initial direction of the train + * @return whether placement is possible + */ + public boolean allowsPlacementAt(Vector2D position, Vector2D direction) { + // check that the requested direction is equal to the direction of one the tracks + final Rail[] touchingRails = findTouchingRails(position); + if (touchingRails.length == 0) { + if (!findContainingRail(position) + .map(rail -> rail.allowsPlacement(position, direction)) + .orElse(false)) { + return false; // containing rail is orthogonal to the requested direction + } + } else if (touchingRails.length == 1) { + if (!(touchingRails[0].allowsPlacement(position, direction) + || touchingRails[0].allowsMovement(position, direction))) { + return false; // rail is orthogonal to the requested direction + } + } else if (!touchingRails[0].allowsPlacement(position, direction) + && !touchingRails[1].allowsPlacement(position, direction) + && !(touchingRails[0].allowsMovement(position, direction) + || touchingRails[1].allowsMovement(position, direction))) { + // rail network does not allow this direction according to + // https://ilias.studium.kit.edu/goto.php?client_id=produktiv&target=frm_996924_139143_534378 + return false; + } + return true; + } + + /** + * Find a rail that contains this position. + * * @param position the position to check * @return the rail that contains the position */ @@ -242,10 +275,6 @@ public class RailwayNetwork { return rails.values().stream().filter(rail -> rail.contains(position)).findFirst(); } - private Optional findRailPotentiallyContaining(Vector2D position) { - return rails.values().stream().filter(rail -> rail.canContain(position)).findFirst(); - } - /** * Find a rail that connects to or contains the first position and connects to or contains the second position. * diff --git a/src/edu/kit/informatik/model/Train.java b/src/edu/kit/informatik/model/Train.java index e668320..9d286a1 100644 --- a/src/edu/kit/informatik/model/Train.java +++ b/src/edu/kit/informatik/model/Train.java @@ -116,24 +116,7 @@ public final class Train { positionList.addLast(position); Vector2D rollingStockPosition = position; // check that the requested direction is equal to the direction of one the tracks - final Rail[] touchingRails = railNetwork.findTouchingRails(position); - if (touchingRails.length == 0) { - if (!railNetwork.findContainingRail(position) - .map(rail -> rail.allowsPlacement(position, direction)) - .orElse(false)) { - return false; // containing rail is orthogonal to the requested direction - } - } else if (touchingRails.length == 1) { - if (!(touchingRails[0].allowsPlacement(position, direction) - || touchingRails[0].allowsMovement(position, direction))) { - return false; // rail is orthogonal to the requested direction - } - } else if (!touchingRails[0].allowsPlacement(position, direction) - && !touchingRails[1].allowsPlacement(position, direction) - && !(touchingRails[0].allowsMovement(position, direction) - || touchingRails[1].allowsMovement(position, direction))) { - // rail network does not allow this direction according to - // https://ilias.studium.kit.edu/goto.php?client_id=produktiv&target=frm_996924_139143_534378 + if (!railNetwork.allowsPlacementAt(position, direction)) { return false; } final Set occupiedRailsSet = new HashSet<>(); @@ -252,11 +235,7 @@ public final class Train { if (nextPosition != null) { // not derailing positions.addFirst(new Vector2D(nextPosition)); - if (positions.size() >= 3 && nextPosition.subtract(positions.get(1)).normalized() - .equals(positions.get(1).subtract(positions.get(2)).normalized()) - && railNetwork.findTouchingRails(positions.get(1)).length == 0) { - positions.remove(1); - } + simplifyPositions(railNetwork); occupiedRails.add(railNetwork.findRail(positions.get(1), nextPosition).get()); } // else: derailing } @@ -282,17 +261,32 @@ public final class Train { if (backPosition != null) { // not derailing positions.addLast(new Vector2D(backPosition)); - if (positions.size() >= 3 - && backPosition.subtract(positions.get(positions.size() - 2)).normalized() - .equals(positions.get(positions.size() - 2) - .subtract(positions.get(positions.size() - 3)).normalized()) - && railNetwork.findTouchingRails(positions.get(positions.size() - 2)).length == 0) { - positions.remove(positions.size() - 2); - } + simplifyPositions(railNetwork); occupiedRails.add(railNetwork.findRail(positions.get(positions.size() - 2), backPosition).get()); } // else: derailing } + /** + * Simplify internal position data. Removes positions that are contained between two other positions. + * + * @param railNetwork railway network to use + */ + private void simplifyPositions(RailwayNetwork railNetwork) { + if (positions.size() >= 3 + && positions.getFirst().subtract(positions.get(1)).normalized() + .equals(positions.get(1).subtract(positions.get(2)).normalized()) + && railNetwork.findTouchingRails(positions.get(1)).length == 0) { + positions.remove(1); + } + if (positions.size() >= 3 + && positions.getLast().subtract(positions.get(positions.size() - 2)).normalized() + .equals(positions.get(positions.size() - 2) + .subtract(positions.get(positions.size() - 3)).normalized()) + && railNetwork.findTouchingRails(positions.get(positions.size() - 2)).length == 0) { + positions.remove(positions.size() - 2); + } + } + /** * Remove this train from the rail network. */ diff --git a/src/edu/kit/informatik/model/TrainManager.java b/src/edu/kit/informatik/model/TrainManager.java index 84475f3..1202eba 100644 --- a/src/edu/kit/informatik/model/TrainManager.java +++ b/src/edu/kit/informatik/model/TrainManager.java @@ -159,7 +159,7 @@ public final class TrainManager { throw new InvalidInputException("train is not valid"); } else if (train.isPlaced()) { throw new InvalidInputException("train is already placed"); - } else if (direction.getX() != 0 && direction.getY() != 0) { + } else if (!direction.isDirection()) { throw new InvalidInputException("invalid train direction"); } else if (!railNetwork.isReadyForTrains()) { throw new InvalidInputException("switches not set up"); diff --git a/src/edu/kit/informatik/model/Vector2D.java b/src/edu/kit/informatik/model/Vector2D.java index 4b50a7f..854bfa9 100644 --- a/src/edu/kit/informatik/model/Vector2D.java +++ b/src/edu/kit/informatik/model/Vector2D.java @@ -63,6 +63,16 @@ public class Vector2D { return Math.abs(this.x - other.x) + Math.abs(this.y - other.y); } + /** + * Check whether this vector represents a valid direction. Is equivalent to checking whether it is parallel to + * the x-axis or the y-axis. + * + * @return whether this vector is a valid direction + */ + public boolean isDirection() { + return (x == 0 || y == 0) && (x != 0 || y != 0); + } + /** * Check whether this vector is parallel to another vector. Note that this method only works for vectors that * are parallel to one of the coordinate axes. diff --git a/src/edu/kit/informatik/ui/CommandLine.java b/src/edu/kit/informatik/ui/CommandLine.java index e832d4f..b359d8f 100644 --- a/src/edu/kit/informatik/ui/CommandLine.java +++ b/src/edu/kit/informatik/ui/CommandLine.java @@ -34,9 +34,6 @@ public final class CommandLine { if (input == null) { break; // FIXME remove } - if (input.startsWith("#")) { - continue; // FIXME remove - } if (input.startsWith(EXIT)) { if (input.length() != EXIT.length()) {