mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final1.git
synced 2024-11-24 01:15:05 +00:00
Correctly derail trains on switches
This commit is contained in:
parent
461e359b12
commit
dab2f23e82
@ -20,4 +20,7 @@ add train 2 T1-Beta
|
||||
put train 1 at (0,5) in direction 0,1
|
||||
put train 2 at (0,0) in direction 1,0
|
||||
step 1
|
||||
put train 1 at (0,0) in direction 0,-1
|
||||
set switch 3 position (0,0)
|
||||
put train 1 at (0,0) in direction 0,-1
|
||||
exit
|
||||
|
@ -24,3 +24,6 @@ electrical engine T1-Beta added to train 2
|
||||
OK
|
||||
OK
|
||||
Crash of train 1,2
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
|
@ -91,8 +91,12 @@ public class ModelRailwaySimulation {
|
||||
* @return whether the switch could be set
|
||||
*/
|
||||
public boolean setSwitch(final int id, final Vector2D position) {
|
||||
// TODO Falls Gleiseweichen auf denen ein Zug bereitssteht, geschaltet werden, entgleist der daraufstehende Zug
|
||||
return railNetwork.setSwitch(id, position);
|
||||
boolean success = railNetwork.setSwitch(id, position);
|
||||
if (success) {
|
||||
// derail trains on switch TODO: await forum answer on printing derailed trains
|
||||
trains.values().stream().filter(train -> train.isOnRail(id)).forEach(Train::removeFromRails);
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -27,7 +27,7 @@ public final class Train {
|
||||
/**
|
||||
* List of positions this train touches.
|
||||
*/
|
||||
private List<Vector2D> position;
|
||||
private LinkedList<Vector2D> position;
|
||||
/**
|
||||
* Direction of the train head.
|
||||
*/
|
||||
@ -149,7 +149,7 @@ public final class Train {
|
||||
}
|
||||
|
||||
LinkedList<Vector2D> positions = new LinkedList<>();
|
||||
positions.add(position);
|
||||
positions.addLast(position);
|
||||
Vector2D positioningDirection = direction.negated();
|
||||
Vector2D rollingStockPosition = position;
|
||||
if (length > 10000) {
|
||||
@ -163,7 +163,7 @@ public final class Train {
|
||||
|| (positions.contains(rollingStockPosition) && !positions.get(0).equals(rollingStockPosition))) {
|
||||
return false;
|
||||
}
|
||||
positions.add(rollingStockPosition);
|
||||
positions.addLast(rollingStockPosition);
|
||||
}
|
||||
Set<Rail> occupiedRails = new HashSet<>();
|
||||
for (int i = 0; i < positions.size(); i++) {
|
||||
@ -217,7 +217,7 @@ public final class Train {
|
||||
public Vector2D getFrontPosition() {
|
||||
if (position != null) {
|
||||
// make sure caller can not modify internal data
|
||||
return new Vector2D(position.get(0));
|
||||
return new Vector2D(position.getFirst());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
@ -229,7 +229,7 @@ public final class Train {
|
||||
public Vector2D getRearPosition() {
|
||||
if (position != null) {
|
||||
// make sure caller can not modify internal data
|
||||
return new Vector2D(position.get(position.size() - 1));
|
||||
return new Vector2D(position.getLast());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
@ -250,7 +250,7 @@ public final class Train {
|
||||
if (position.size() == 1) {
|
||||
return direction.negated();
|
||||
} else {
|
||||
return position.get(position.size() - 1).subtract(position.get(position.size() - 2));
|
||||
return position.getLast().subtract(position.get(position.size() - 2)); // TODO
|
||||
}
|
||||
}
|
||||
|
||||
@ -269,9 +269,9 @@ public final class Train {
|
||||
*/
|
||||
public void moveTo(final RailwayNetwork railNetwork, final Vector2D nextPosition) {
|
||||
// make sure caller can not modiy internal state
|
||||
position.add(0, new Vector2D(nextPosition));
|
||||
Vector2D last = position.remove(position.size() - 1);
|
||||
updateOccupiedRails(railNetwork, position.get(0), last, position.get(position.size() - 1));
|
||||
position.addFirst(new Vector2D(nextPosition));
|
||||
Vector2D last = position.removeLast();
|
||||
updateOccupiedRails(railNetwork, position.getFirst(), last, position.getLast());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -280,9 +280,9 @@ public final class Train {
|
||||
* @param rearPosition position to move rear of the train to
|
||||
*/
|
||||
public void moveBackTo(final RailwayNetwork railNetwork, final Vector2D rearPosition) {
|
||||
Vector2D last = position.remove(0);
|
||||
position.add(new Vector2D(rearPosition));
|
||||
updateOccupiedRails(railNetwork, position.get(position.size() - 1), last, position.get(0));
|
||||
Vector2D last = position.removeFirst();
|
||||
position.addLast(new Vector2D(rearPosition));
|
||||
updateOccupiedRails(railNetwork, position.getLast(), last, position.getFirst());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,7 +1,5 @@
|
||||
package edu.kit.informatik.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* A two-dimensional vector, usually a position or movement direction vector.
|
||||
*
|
||||
@ -141,9 +139,4 @@ public class Vector2D {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(x, y);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user