mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final1.git
synced 2024-11-24 09:24:58 +00:00
Correctly handle crashes of more than two trains
This commit is contained in:
parent
16ddc708ef
commit
302b755ee6
@ -466,7 +466,8 @@ public class ModelRailwaySimulation {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
// check for block collisions
|
// check for block collisions
|
||||||
trains.values().stream().filter(train -> train.isPlaced() && collisions.stream().noneMatch((x) -> x.contains(train))).forEach(train -> {
|
trains.values().stream().filter(train -> train.isPlaced()
|
||||||
|
&& collisions.stream().noneMatch((x) -> x.contains(train))).forEach(train -> {
|
||||||
Set<Rail> occupiedByThisTrain = nextOccupiedRails.get(train);
|
Set<Rail> occupiedByThisTrain = nextOccupiedRails.get(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);
|
||||||
@ -481,12 +482,17 @@ public class ModelRailwaySimulation {
|
|||||||
// try to find/merge existing collisions
|
// try to find/merge existing collisions
|
||||||
Set<Train> existingCollision = null;
|
Set<Train> existingCollision = null;
|
||||||
for (Set<Train> collision : collisions) {
|
for (Set<Train> collision : collisions) {
|
||||||
if (collision.contains(otherTrain)) {
|
if (collision.contains(otherTrain) || collision.contains(train)) {
|
||||||
if (existingCollision == null) {
|
if (existingCollision == null) {
|
||||||
existingCollision = collision;
|
existingCollision = collision;
|
||||||
collision.add(train);
|
collision.add(train);
|
||||||
|
collision.add(otherTrain);
|
||||||
} else {
|
} else {
|
||||||
existingCollision.addAll(collision);
|
existingCollision.addAll(collision);
|
||||||
|
existingCollision.add(train);
|
||||||
|
existingCollision.add(otherTrain);
|
||||||
|
collisions.remove(collision);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,3 +61,12 @@ add track (1,0) -> (10,0)
|
|||||||
put train 1 at (7,0) in direction -1,0
|
put train 1 at (7,0) in direction -1,0
|
||||||
put train 5 at (0,0) in direction -1,0
|
put train 5 at (0,0) in direction -1,0
|
||||||
step 8
|
step 8
|
||||||
|
delete track 1
|
||||||
|
delete track 2
|
||||||
|
add track (0,0) -> (10,0)
|
||||||
|
add track (10,0) -> (20,0)
|
||||||
|
add track (20,0) -> (30,0)
|
||||||
|
put train 1 at (10,0) in direction 1,0
|
||||||
|
put train 2 at (15,0) in direction 1,0
|
||||||
|
put train 5 at (20,0) in direction -1,0
|
||||||
|
step 1
|
||||||
|
@ -78,3 +78,12 @@ OK
|
|||||||
OK
|
OK
|
||||||
Crash of train 1
|
Crash of train 1
|
||||||
Crash of train 5
|
Crash of train 5
|
||||||
|
OK
|
||||||
|
OK
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
OK
|
||||||
|
OK
|
||||||
|
OK
|
||||||
|
Crash of train 1,2,5
|
||||||
|
Loading…
Reference in New Issue
Block a user