mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final1.git
synced 2024-11-24 09:24:58 +00:00
Write comments
This commit is contained in:
parent
122193300d
commit
c82ab689d4
@ -277,21 +277,40 @@ public final class TrainManager {
|
|||||||
return collisions;
|
return collisions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate train collisions, removing crashing trains in the process.
|
||||||
|
*
|
||||||
|
* @param collisions list of collisions to expand
|
||||||
|
* @param occupiedRails map of occupied rails before the step
|
||||||
|
* @param nextOccupiedRails map of occupied rails after the step
|
||||||
|
*/
|
||||||
private void checkForBlockCollisions(List<Set<Train>> collisions, Map<Train, Set<Rail>> occupiedRails,
|
private void checkForBlockCollisions(List<Set<Train>> collisions, Map<Train, Set<Rail>> occupiedRails,
|
||||||
Map<Train, Set<Rail>> nextOccupiedRails) {
|
Map<Train, Set<Rail>> nextOccupiedRails) {
|
||||||
trains.values().stream().filter(train -> train.isPlaced()
|
trains.values().stream()
|
||||||
&& collisions.stream().noneMatch(x -> x.contains(train))).forEach(train -> {
|
// only process placed trains
|
||||||
|
.filter(Train::isPlaced)
|
||||||
|
// if already in a collision, do not process again
|
||||||
|
.filter(train -> collisions.stream().noneMatch(x -> x.contains(train)))
|
||||||
|
.forEach(train -> {
|
||||||
Set<Rail> occupiedByThisTrain = nextOccupiedRails.get(train);
|
Set<Rail> occupiedByThisTrain = nextOccupiedRails.get(train);
|
||||||
|
// check each other train
|
||||||
trains.values().stream().filter(x -> x != train).forEach(otherTrain -> {
|
trains.values().stream().filter(x -> x != train).forEach(otherTrain -> {
|
||||||
Set<Rail> occupiedByOtherTrainPreviously = occupiedRails.get(otherTrain);
|
Set<Rail> occupiedByOtherTrainPreviously = occupiedRails.get(otherTrain);
|
||||||
Set<Rail> occupiedByOtherTrain = nextOccupiedRails.get(otherTrain);
|
Set<Rail> occupiedByOtherTrain = nextOccupiedRails.get(otherTrain);
|
||||||
|
// if train did not occupy any rails (it is not placed)
|
||||||
if (occupiedByOtherTrain == null) {
|
if (occupiedByOtherTrain == null) {
|
||||||
|
// skip
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// first check if train is on the same rails
|
||||||
boolean anyIntersection = Stream.concat(
|
boolean anyIntersection = Stream.concat(
|
||||||
occupiedByOtherTrain.stream(), occupiedByOtherTrainPreviously.stream())
|
occupiedByOtherTrain.stream(), occupiedByOtherTrainPreviously.stream())
|
||||||
|
// only search in currently occupied rails, previously occupied rails
|
||||||
|
// will be checked once the other train is processed in the outer loop
|
||||||
.anyMatch(occupiedByThisTrain::contains);
|
.anyMatch(occupiedByThisTrain::contains);
|
||||||
|
// or simply touches the other train
|
||||||
if (anyIntersection || train.touches(otherTrain)) {
|
if (anyIntersection || train.touches(otherTrain)) {
|
||||||
|
// remove crashing trains
|
||||||
train.removeFromRails();
|
train.removeFromRails();
|
||||||
otherTrain.removeFromRails();
|
otherTrain.removeFromRails();
|
||||||
// try to find/merge existing collisions
|
// try to find/merge existing collisions
|
||||||
@ -312,6 +331,7 @@ public final class TrainManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (existingCollision == null) {
|
if (existingCollision == null) {
|
||||||
|
// create a new collision
|
||||||
collisions.add(Stream.of(train, otherTrain).collect(Collectors.toSet()));
|
collisions.add(Stream.of(train, otherTrain).collect(Collectors.toSet()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user