diff --git a/src/train/Position.java b/src/train/Position.java index 4d42b5ce850f1007de93bc6d6a11e9483fcc7b8e..e7b62cda7c2c36c3634c283594b3adcd1e8b938d 100644 --- a/src/train/Position.java +++ b/src/train/Position.java @@ -21,7 +21,7 @@ package train; */ public class Position implements Cloneable { private final Direction direction; - private final Element pos; + private Element pos; public Position(Element elt, Direction d) { if (elt == null || d == null) @@ -44,6 +44,24 @@ public class Position implements Cloneable { public Element getPos() { return pos; } + + public void move(Railway railway) { + if (railway == null) { + throw new NullPointerException("Railway cannot be null."); + } + + try { + // Get the next element based on the current position and direction + Element nextElement = railway.nextElement(this.pos, this.direction); + this.pos = nextElement; + + } catch (IllegalArgumentException e) { + // If no next element exists, reverse the direction + this.direction.turn(); + } + } + + @Override public String toString() {