diff --git a/src/edu/kit/informatik/MainTest.java b/src/edu/kit/informatik/MainTest.java index a015479..9d100b5 100644 --- a/src/edu/kit/informatik/MainTest.java +++ b/src/edu/kit/informatik/MainTest.java @@ -128,6 +128,11 @@ class MainTest { cmpInOut("spt_input.txt", "spt_output.txt"); } + @Test + void fuzz12() throws IOException { + cmpInOut("fuzz12_input.txt", "fuzz12_output.txt"); + } + private void cmpInOut(String in, String out) throws IOException { System.setIn(new ByteArrayInputStream(readFile(in))); ByteArrayOutputStream output = new ByteArrayOutputStream(); diff --git a/src/edu/kit/informatik/model/RailwayNetwork.java b/src/edu/kit/informatik/model/RailwayNetwork.java index e890bb7..ea71096 100644 --- a/src/edu/kit/informatik/model/RailwayNetwork.java +++ b/src/edu/kit/informatik/model/RailwayNetwork.java @@ -2,6 +2,7 @@ package edu.kit.informatik.model; import edu.kit.informatik.ui.InvalidInputException; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; @@ -207,13 +208,6 @@ public class RailwayNetwork { return containingRail.get().move(position, direction, steps); } final Rail[] touchingRails = findTouchingRails(position); - final Vector2D nextPossiblePosition = position.add(direction); - final Optional possibleContainingRail = findRailPotentiallyContaining(nextPossiblePosition); - if (possibleContainingRail.isPresent() - && !possibleContainingRail.get().contains(position.subtract(direction))) { - return possibleContainingRail.get().move(position, direction, steps); - - } if (touchingRails.length == 0) { return null; } else if (touchingRails.length == 1) { @@ -221,12 +215,14 @@ public class RailwayNetwork { } final Vector2D onRailOne = touchingRails[0].move(position, new Vector2D(direction), steps); final Vector2D onRailTwo = touchingRails[1].move(position, new Vector2D(direction), steps); - if (position.equals(onRailOne) || onRailOne == null) { + if (position.equals(onRailOne) || onRailOne == null + || (onRailTwo != null && onRailTwo.subtract(position).directionEquals(direction))) { // we are moving on rail two final Vector2D newDirection = touchingRails[1].getDirectionFrom(position); direction.copyFrom(newDirection); return onRailTwo; - } else if (position.equals(onRailTwo) || onRailTwo == null) { + } else if (position.equals(onRailTwo) || onRailTwo == null + || onRailOne.subtract(position).directionEquals(direction)) { final Vector2D newDirection = touchingRails[0].getDirectionFrom(position); direction.copyFrom(newDirection); return onRailOne; diff --git a/src/edu/kit/informatik/model/Vector2D.java b/src/edu/kit/informatik/model/Vector2D.java index a6768ca..4b50a7f 100644 --- a/src/edu/kit/informatik/model/Vector2D.java +++ b/src/edu/kit/informatik/model/Vector2D.java @@ -67,13 +67,26 @@ public class Vector2D { * 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. * - * @param other the point to measure distance to - * @return the manhattan distance + * @param other vector to compare + * @return whether both are parallel */ public boolean isParallelTo(Vector2D other) { return x == 0 && other.x == 0 || y == 0 && other.y == 0; } + /** + * Calculate whether this vector points in the same direction as another. Note that this method only works for + * vectors that are parallel to one of the coordinate axes. + * + * @param other vector to compare + * @return whether they represent the same direction + */ + public boolean directionEquals(Vector2D other) { + return x == 0 && x == other.x && Long.signum(y) == Long.signum(other.y) + || y == 0 && y == other.y && Long.signum(x) == Long.signum(other.x); + } + + /** * Normalize this vector. Will essentially return a new vector with (x/|x|, y/|y|). *