Skip to content
Snippets Groups Projects
Commit 424451a7 authored by CELERIER Auguste's avatar CELERIER Auguste
Browse files

move()

parent 598f9120
Branches
No related tags found
No related merge requests found
......@@ -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() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment