Fiasko/src/main/java/inf112/fiasko/roborally/utility/BoardLoaderUtil.java

97 lines
4.3 KiB
Java
Raw Normal View History

package inf112.fiasko.roborally.utility;
2020-04-14 15:54:09 +02:00
import inf112.fiasko.roborally.elementproperties.Direction;
import inf112.fiasko.roborally.elementproperties.TileType;
import inf112.fiasko.roborally.elementproperties.WallType;
2020-02-23 00:02:03 +01:00
import inf112.fiasko.roborally.objects.Board;
import inf112.fiasko.roborally.objects.IGrid;
import inf112.fiasko.roborally.objects.Robot;
import inf112.fiasko.roborally.objects.Tile;
import inf112.fiasko.roborally.objects.Wall;
import inf112.fiasko.roborally.objects.Grid;
import java.io.*;
import java.util.List;
/**
2020-04-07 22:48:27 +02:00
* This class helps loading boards
*/
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<Robot> robotList) throws IOException {
2020-02-23 23:17:21 +01:00
InputStream fileStream = ResourceUtil.getResourceAsInputStream(boardFile);
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<Tile> tileGrid = loadTileGrid(reader, gridWidth, gridHeight);
IGrid<Wall> 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<Tile> loadTileGrid(BufferedReader reader, int gridWidth, int gridHeight) throws IOException {
IGrid<Tile> tileGrid = new Grid<>(gridWidth, gridHeight);
for (int y = 0; y < gridHeight; y++) {
String gridLine = reader.readLine();
String[] tilesOnLine = gridLine.split(" ");
for (int x = 0; x < gridWidth; x++) {
String[] tileData = tilesOnLine[x].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(x, y, 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<Wall> loadWallGrid(BufferedReader reader, int gridWidth, int gridHeight) throws IOException {
IGrid<Wall> wallGrid = new Grid<>(gridWidth, gridHeight);
for (int y = 0; y < gridHeight; y++) {
String gridLine = reader.readLine();
String[] wallsOnLine = gridLine.split(" ");
for (int x = 0; x < gridWidth; x++) {
if (wallsOnLine[x].equals("0")) {
continue;
}
String[] wallData = wallsOnLine[x].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 wall encountered when loading board file.");
}
wallGrid.setElement(x, y, new Wall(wallType, direction));
}
}
return wallGrid;
}
}