From 16eae42fa310c643a4518d010fd4b72ff732f107 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Sat, 15 Feb 2020 15:24:31 +0100 Subject: [PATCH] Train collision checking fixes --- .../informatik/ModelRailwaySimulation.java | 49 +++++-------------- 1 file changed, 11 insertions(+), 38 deletions(-) diff --git a/src/edu/kit/informatik/ModelRailwaySimulation.java b/src/edu/kit/informatik/ModelRailwaySimulation.java index 5eed11f..1bce758 100644 --- a/src/edu/kit/informatik/ModelRailwaySimulation.java +++ b/src/edu/kit/informatik/ModelRailwaySimulation.java @@ -518,24 +518,6 @@ public class ModelRailwaySimulation { private ArrayList> getCollisionsOfOneStep() { ArrayList> collisions = new ArrayList<>(); - // check touching trains - /* - for (Train train : trains) { - if (!train.isPlaced()) { - continue; - } - for (Train otherTrain : trains) { - if (!otherTrain.isPlaced() || train == otherTrain) { - continue; - } - if (train.getFrontPosition().equals(otherTrain.getFrontPosition())) { - train.removeFromRails(); - otherTrain.removeFromRails(); - collisions.add(Arrays.asList(train, otherTrain)); - } - } - } - */ Map> occupiedRails = new HashMap<>(); for (Train train : trains) { if (train.isPlaced()) { @@ -570,9 +552,12 @@ public class ModelRailwaySimulation { continue; // already included in collision } Set occupiedByThisTrain = nextOccupiedRails.get(train); - trains.stream().filter((x) -> x != train && x.isPlaced()).forEach((otherTrain) -> { + trains.stream().filter((x) -> x != train).forEach((otherTrain) -> { Set occupiedByOtherTrainPreviously = occupiedRails.get(otherTrain); Set occupiedByOtherTrain = nextOccupiedRails.get(otherTrain); + if (occupiedByOtherTrain == null || occupiedByOtherTrainPreviously == null) { + return; + } boolean anyIntersection = occupiedByOtherTrain.stream().anyMatch(occupiedByThisTrain::contains) || occupiedByOtherTrainPreviously.stream().anyMatch(occupiedByThisTrain::contains); if (anyIntersection) { @@ -589,6 +574,9 @@ public class ModelRailwaySimulation { } } } + if (existingCollision == null) { + collisions.add(new HashSet<>(Arrays.asList(train, otherTrain))); + } } }); } @@ -597,24 +585,6 @@ public class ModelRailwaySimulation { private ArrayList> getCollisionsOfOneReverseStep() { ArrayList> collisions = new ArrayList<>(); - // check touching trains - /* - for (Train train : trains) { - if (!train.isPlaced()) { - continue; - } - for (Train otherTrain : trains) { - if (!otherTrain.isPlaced() || train == otherTrain) { - continue; - } - if (train.getBackPosition().equals(otherTrain.getBackPosition())) { - train.removeFromRails(); - otherTrain.removeFromRails(); - collisions.add(new HashSet<>(Arrays.asList(train, otherTrain))); - } - } - } - */ Map> occupiedRails = new HashMap<>(); for (Train train : trains) { if (train.isPlaced()) { @@ -649,9 +619,12 @@ public class ModelRailwaySimulation { continue; // already included in collision } Set occupiedByThisTrain = nextOccupiedRails.get(train); - trains.stream().filter((x) -> x != train && train.isPlaced()).forEach((otherTrain) -> { + trains.stream().filter((x) -> x != train).forEach((otherTrain) -> { Set occupiedByOtherTrainPreviously = occupiedRails.get(otherTrain); Set occupiedByOtherTrain = nextOccupiedRails.get(otherTrain); + if (occupiedByOtherTrain == null || occupiedByOtherTrainPreviously == null) { + return; + } boolean anyIntersection = occupiedByOtherTrain.stream().anyMatch(occupiedByThisTrain::contains) || occupiedByOtherTrainPreviously.stream().anyMatch(occupiedByThisTrain::contains); if (anyIntersection) {