From d03ad6430bdc0b3552149435ba20fb2bca4f294e Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 3 Feb 2018 16:06:06 +0100 Subject: Implement initial robot placement Signed-off-by: pacien --- .../java/fr/umlv/java/wallj/board/BoardConverter.java | 2 +- src/main/java/fr/umlv/java/wallj/context/Stage.java | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/fr/umlv/java/wallj/board/BoardConverter.java b/src/main/java/fr/umlv/java/wallj/board/BoardConverter.java index 064df7a..84feb41 100644 --- a/src/main/java/fr/umlv/java/wallj/board/BoardConverter.java +++ b/src/main/java/fr/umlv/java/wallj/board/BoardConverter.java @@ -33,9 +33,9 @@ public final class BoardConverter { * @return the list of blocks converted */ public static List boardToWorld(Board board) { - ArrayList blocks = new ArrayList<>(); int nbRow = board.getDim().getRow(); int nbCol = board.getDim().getCol(); + ArrayList blocks = new ArrayList<>(nbCol * nbRow); for (int i = 0; i < nbRow; i++) { for (int j = 0; j < nbCol; j++) { Block block; diff --git a/src/main/java/fr/umlv/java/wallj/context/Stage.java b/src/main/java/fr/umlv/java/wallj/context/Stage.java index 8852568..8ef4558 100644 --- a/src/main/java/fr/umlv/java/wallj/context/Stage.java +++ b/src/main/java/fr/umlv/java/wallj/context/Stage.java @@ -5,12 +5,15 @@ import fr.umlv.java.wallj.block.BlockFactory; import fr.umlv.java.wallj.block.BlockType; import fr.umlv.java.wallj.board.Board; import fr.umlv.java.wallj.board.BoardConverter; +import fr.umlv.java.wallj.board.TileVec2; import fr.umlv.java.wallj.event.*; import org.jbox2d.common.Vec2; import org.jbox2d.dynamics.World; import java.time.Duration; +import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Objects; /** @@ -21,15 +24,16 @@ public class Stage implements Updateable { private static final int POSITION_TICK_PER_MS = 2; private final World world = new World(new Vec2()); + private final List blocks = new LinkedList<>(); private final Board board; - private final List blocks; /** * @param board the base board */ public Stage(Board board) { this.board = Objects.requireNonNull(board); - blocks = BoardConverter.boardToWorld(board); + blocks.addAll(BoardConverter.boardToWorld(board)); + blocks.add(BlockFactory.build(BlockType.ROBOT, findAnyFreeTile(board))); blocks.forEach(block -> block.link(world)); } @@ -85,4 +89,12 @@ public class Stage implements Updateable { block.link(world); }); } + + private static TileVec2 findAnyFreeTile(Board board) { + return board.stream() + .filter(entry -> entry.getValue() == BlockType.FREE) + .findAny() + .map(Map.Entry::getKey) + .orElseThrow(IllegalArgumentException::new); + } } -- cgit v1.2.3