From 1347f30afbed7ee9c149f77534f5e75fbe819b98 Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Thu, 11 Jan 2018 20:07:28 +0100 Subject: Implementing BoardConverter and test failed --- .../fr/umlv/java/wallj/board/BoardConverter.java | 40 +++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'src/main/java') 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 9f5736a..0f8359b 100644 --- a/src/main/java/fr/umlv/java/wallj/board/BoardConverter.java +++ b/src/main/java/fr/umlv/java/wallj/board/BoardConverter.java @@ -1,5 +1,43 @@ package fr.umlv.java.wallj.board; -public class BoardConverter { +import fr.umlv.java.wallj.model.*; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Adam NAILI + */ +public final class BoardConverter { //TODO + private BoardConverter() { + } + + public static Board worldToBoard(List blocks) { + int width = blocks.stream().map(Block::getTile).mapToInt(TileVec2::getRow).max().orElse(-1) + 1; + int height = blocks.stream().map(Block::getTile).mapToInt(TileVec2::getCol).max().orElse(-1) + 1; + + Board.Builder builder = new Board.Builder(width, height); + for (Block block : blocks) { + builder.setBlockTypeAt(block.getTile(), block.getType()); + } + return builder.build(); + } + + public static List boardToWorld(Board board) { + ArrayList blocks = new ArrayList<>(); + int nbRow = board.getDim().getRow(); + int nbCol = board.getDim().getCol(); + for (int i = 0; i < nbRow; i++) { + for (int j = 0; i < nbCol; j++) { + Block block; + TileVec2 location = TileVec2.of(i, j); + block = BlockFactory.build(board.getBlockTypeAt(location), location); + if (block != null) { + blocks.add(block); + } + } + } + return blocks; + } } -- cgit v1.2.3