From 438bbd70a07835db41957eddf02581d932110163 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sat, 22 Feb 2020 23:34:19 +0100 Subject: [PATCH] =?UTF-8?q?Legger=20til=20en=20utility=20klasse=20for=20?= =?UTF-8?q?=C3=A5=20laste=20inn=20et=20brett=20fra=20en=20tekstfil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../roborally/utility/BoardLoaderUtil.java | 94 +++++++++++++++++++ .../utility/BoardLoaderUtilTest.java | 15 +++ 2 files changed, 109 insertions(+) create mode 100644 src/main/java/inf112/fiasko/roborally/utility/BoardLoaderUtil.java create mode 100644 src/test/java/inf112/fiasko/roborally/utility/BoardLoaderUtilTest.java diff --git a/src/main/java/inf112/fiasko/roborally/utility/BoardLoaderUtil.java b/src/main/java/inf112/fiasko/roborally/utility/BoardLoaderUtil.java new file mode 100644 index 0000000..4dbe1cb --- /dev/null +++ b/src/main/java/inf112/fiasko/roborally/utility/BoardLoaderUtil.java @@ -0,0 +1,94 @@ +package inf112.fiasko.roborally.utility; + +import inf112.fiasko.roborally.element_properties.*; +import inf112.fiasko.roborally.objects.*; + +import java.io.*; +import java.util.List; + +/** + * Loads a board + */ +public final class BoardLoaderUtil { + private BoardLoaderUtil() {} + + /** + * Loads a board described in a file + * @param boardFile The file containing the board description + * @param robotList A list of robots on the board + * @return A board + * @throws IOException If the board file cannot be loaded + */ + public static Board loadBoard(String boardFile, List robotList) throws IOException { + ClassLoader classloader = Thread.currentThread().getContextClassLoader(); + InputStream fileStream = classloader.getResourceAsStream(boardFile); + if (fileStream == null) { + throw new IllegalArgumentException("Board file could not be loaded."); + } + + BufferedReader reader = new BufferedReader(new InputStreamReader(fileStream)); + String infoLine = reader.readLine(); + String[] infoData = infoLine.split(" "); + int gridWidth = Integer.parseInt(infoData[0]); + int gridHeight = Integer.parseInt(infoData[1]); + + IGrid tileGrid = loadTileGrid(reader, gridWidth, gridHeight); + IGrid wallGrid = loadWallGrid(reader, gridWidth, gridHeight); + return new Board(tileGrid, wallGrid, robotList); + } + + /** + * Loads information about a tile grid from a buffered reader + * @param reader A buffered reader ready to read information about the grid + * @param gridWidth The width of the grid to load + * @param gridHeight The height of the grid to load + * @return A grid containing the tiles described by the buffered reader + * @throws IOException If the reader reads invalid grid information + */ + private static IGrid loadTileGrid(BufferedReader reader, int gridWidth, int gridHeight) throws IOException { + IGrid tileGrid = new Grid<>(gridWidth, gridHeight); + for (int i = 0; i < gridHeight; i++) { + String gridLine = reader.readLine(); + String[] tilesOnLine = gridLine.split(" "); + for (int j = 0; j < gridWidth; j++) { + String[] tileData = tilesOnLine[j].split(";"); + TileType tileType = TileType.getTileTypeFromID(Integer.parseInt(tileData[0])); + Direction direction = Direction.getDirectionFromID(Integer.parseInt(tileData[1])); + if (direction == null) { + throw new IllegalArgumentException("Invalid direction for tile encountered when loading board file."); + } + tileGrid.setElement(i, j, new Tile(tileType, direction)); + } + } + return tileGrid; + } + + /** + * Loads information about a wall grid from a buffered reader + * @param reader A buffered reader ready to read information about the grid + * @param gridWidth The width of the grid to load + * @param gridHeight The height of the grid to load + * @return A grid containing the walls described by the buffered reader + * @throws IOException If the reader reads invalid grid information + */ + private static IGrid loadWallGrid(BufferedReader reader, int gridWidth, int gridHeight) throws IOException { + IGrid wallGrid = new Grid<>(gridWidth, gridHeight); + for (int i = 0; i < gridHeight; i++) { + String gridLine = reader.readLine(); + String[] wallsOnLine = gridLine.split(" "); + for (int j = 0; j < gridWidth; j++) { + if (wallsOnLine[j].equals("0")) { + continue; + } + String[] wallData = wallsOnLine[j].split(";"); + WallType wallType = WallType.getWallTypeFromID(Integer.parseInt(wallData[0])); + Direction direction = Direction.getDirectionFromID(Integer.parseInt(wallData[1])); + if (direction == null) { + throw new IllegalArgumentException("Invalid direction for tile encountered when loading board file."); + } + wallGrid.setElement(i, j, new Wall(wallType, direction)); + } + } + return wallGrid; + } +} diff --git a/src/test/java/inf112/fiasko/roborally/utility/BoardLoaderUtilTest.java b/src/test/java/inf112/fiasko/roborally/utility/BoardLoaderUtilTest.java new file mode 100644 index 0000000..885948b --- /dev/null +++ b/src/test/java/inf112/fiasko/roborally/utility/BoardLoaderUtilTest.java @@ -0,0 +1,15 @@ +package inf112.fiasko.roborally.utility; + +import inf112.fiasko.roborally.objects.Robot; +import org.junit.Test; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class BoardLoaderUtilTest { + @Test + public void loadTestBoard() throws IOException { + List robotList = new ArrayList<>(); + BoardLoaderUtil.loadBoard("boards/test_board.txt", robotList); + } +}