From 424451a7c3a2abc17074d7773e46fad6e7fb32b7 Mon Sep 17 00:00:00 2001 From: Auguste <auguste.celerier@imt-atlantique.net> Date: Tue, 21 Jan 2025 10:39:19 +0100 Subject: [PATCH] move() --- src/train/Position.java | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/train/Position.java b/src/train/Position.java index 4d42b5c..e7b62cd 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() { -- GitLab