From 63182fc0c738490929a4bb7c82cdbf6350c3ca13 Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 3 Feb 2018 18:27:32 +0100 Subject: Ignore invalid robot move orders (outside of map) Signed-off-by: pacien --- src/main/java/fr/umlv/java/wallj/block/RobotBlock.java | 2 +- src/main/java/fr/umlv/java/wallj/board/Board.java | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/fr/umlv/java/wallj/block/RobotBlock.java b/src/main/java/fr/umlv/java/wallj/block/RobotBlock.java index 0969009..25dc734 100644 --- a/src/main/java/fr/umlv/java/wallj/block/RobotBlock.java +++ b/src/main/java/fr/umlv/java/wallj/block/RobotBlock.java @@ -67,7 +67,7 @@ public class RobotBlock extends Block { } private void updatePath(Board board, TileVec2 target) { - if (!board.getBlockTypeAt(target).isTraversable()) return; + if (!board.inside(target) || !board.getBlockTypeAt(target).isTraversable()) return; if (pathFinder == null) pathFinder = new PathFinder(board); path = new LinkedList<>(pathFinder.findPath(TileVec2.of(pos), target)); } diff --git a/src/main/java/fr/umlv/java/wallj/board/Board.java b/src/main/java/fr/umlv/java/wallj/board/Board.java index 5677c39..2e67c53 100644 --- a/src/main/java/fr/umlv/java/wallj/board/Board.java +++ b/src/main/java/fr/umlv/java/wallj/board/Board.java @@ -70,6 +70,16 @@ public final class Board { return TileVec2.of(Matrix.getWidth(map), Matrix.getHeight(map)); } + /** + * @param v a tile vector + * @return T(v is a valid position of an element within the board) + */ + public boolean inside(TileVec2 v) { + TileVec2 dim = getDim(); + return v.getRow() >= 0 && v.getCol() >= 0 && + v.getRow() < dim.getRow() && v.getCol() < dim.getCol(); + } + /** * @return a stream of block types and their associated tile position vectors */ -- cgit v1.2.3