Auto reformaterer alle utility klasser og tester

This commit is contained in:
Kristian Knarvik 2020-04-20 13:08:48 +02:00
parent 32302c0dd3
commit 91bdcd8394
8 changed files with 100 additions and 74 deletions

View File

@ -4,11 +4,11 @@ import inf112.fiasko.roborally.elementproperties.Direction;
import inf112.fiasko.roborally.elementproperties.TileType;
import inf112.fiasko.roborally.elementproperties.WallType;
import inf112.fiasko.roborally.objects.Board;
import inf112.fiasko.roborally.objects.IGrid;
import inf112.fiasko.roborally.objects.Grid;
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 inf112.fiasko.roborally.objects.ListGrid;
import java.io.*;
import java.util.List;
@ -17,10 +17,13 @@ import java.util.List;
* This class helps loading boards
*/
public final class BoardLoaderUtil {
private 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
@ -34,21 +37,22 @@ public final class BoardLoaderUtil {
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);
Grid<Tile> tileGrid = loadTileGrid(reader, gridWidth, gridHeight);
Grid<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 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);
private static Grid<Tile> loadTileGrid(BufferedReader reader, int gridWidth, int gridHeight) throws IOException {
Grid<Tile> tileGrid = new ListGrid<>(gridWidth, gridHeight);
for (int y = 0; y < gridHeight; y++) {
String gridLine = reader.readLine();
String[] tilesOnLine = gridLine.split(" ");
@ -67,14 +71,15 @@ public final class BoardLoaderUtil {
/**
* 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 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);
private static Grid<Wall> loadWallGrid(BufferedReader reader, int gridWidth, int gridHeight) throws IOException {
Grid<Wall> wallGrid = new ListGrid<>(gridWidth, gridHeight);
for (int y = 0; y < gridHeight; y++) {
String gridLine = reader.readLine();
String[] wallsOnLine = gridLine.split(" ");

View File

@ -17,6 +17,7 @@ public final class DeckLoaderUtil {
/**
* Returns a programming card deck containing all official programming cards
*
* @return A programming card deck with programming cards
* @throws IOException If the programming cards file is invalid
*/

View File

@ -5,14 +5,14 @@ import inf112.fiasko.roborally.elementproperties.Direction;
import inf112.fiasko.roborally.elementproperties.Position;
import inf112.fiasko.roborally.elementproperties.RobotID;
import inf112.fiasko.roborally.objects.Player;
import inf112.fiasko.roborally.objects.IDrawableObject;
import inf112.fiasko.roborally.objects.Tile;
import inf112.fiasko.roborally.objects.IDrawableGame;
import inf112.fiasko.roborally.objects.DrawableGame;
import inf112.fiasko.roborally.objects.Wall;
import inf112.fiasko.roborally.objects.Particle;
import inf112.fiasko.roborally.objects.Robot;
import inf112.fiasko.roborally.objects.DrawableObject;
import com.esotericsoftware.kryonet.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -21,17 +21,19 @@ import java.util.Map;
* This class helps with tasks which mix primitive classes and classes from external libraries
*/
public final class IOUtil {
private IOUtil() {}
private IOUtil() {
}
/**
* Generates a list of players from a map of player names and a map of robot ids
*
* @param playerNames A map between connections and player names
* @param robotIDs A map between connections and robot ids
* @param robotIDs A map between connections and robot ids
* @return A list of players
*/
public static List<Player> playerGenerator(Map<Connection, String> playerNames, Map<Connection, RobotID> robotIDs) {
List<Player> playerList = new ArrayList<>();
for (Connection connection: playerNames.keySet()) {
for (Connection connection : playerNames.keySet()) {
Player player = new Player(robotIDs.get(connection), playerNames.get(connection));
playerList.add(player);
}
@ -40,13 +42,14 @@ public final class IOUtil {
/**
* Gets a list of all elements which should be drawn from the game provided
* @param game A game implementing IDrawableGame
* @param tileWidth The with of all tiles to be drawn
*
* @param game A game implementing DrawableGame
* @param tileWidth The with of all tiles to be drawn
* @param tileHeight The height of all tiles to be drawn
* @return A list of drawable objects
*/
public static List<IDrawableObject> getDrawableObjectsFromGame(IDrawableGame game, int tileWidth, int tileHeight) {
List<IDrawableObject> drawableObjects = new ArrayList<>();
public static List<DrawableObject> getDrawableObjectsFromGame(DrawableGame game, int tileWidth, int tileHeight) {
List<DrawableObject> drawableObjects = new ArrayList<>();
List<Tile> tilesToDraw = game.getTilesToDraw();
List<Wall> wallsToDraw = game.getWallsToDraw();
List<Particle> particlesToDraw = game.getParticlesToDraw();
@ -62,19 +65,21 @@ public final class IOUtil {
/**
* Gets a list of all drawable robots on the board
* @param robots A list of robots to draw
*
* @param robots A list of robots to draw
* @param gameHeight The height of the game
* @param tileWidth The width of a tile
* @param tileWidth The width of a tile
* @param tileHeight The height of a tile
* @return A list of drawable robots
*/
private static List<IDrawableObject> getDrawableRobots(List<Robot> robots, int gameHeight, int tileWidth, int tileHeight) {
List<IDrawableObject> drawableObjects = new ArrayList<>();
private static List<DrawableObject> getDrawableRobots(List<Robot> robots, int gameHeight, int tileWidth,
int tileHeight) {
List<DrawableObject> drawableObjects = new ArrayList<>();
for (Robot robot : robots) {
TextureRegion region = TextureConverterUtil.convertElement(robot);
Position robotPosition = robot.getPosition();
int rotation = getElementRotation(robot);
IDrawableObject drawableObject = new DrawableObject(region, robotPosition.getXCoordinate() * tileWidth,
DrawableObject drawableObject = new DrawableObject(region, robotPosition.getXCoordinate() * tileWidth,
(-robotPosition.getYCoordinate() + gameHeight - 1) * tileHeight, tileWidth, tileHeight, rotation);
drawableObjects.add(drawableObject);
}
@ -83,16 +88,17 @@ public final class IOUtil {
/**
* Gets a list of drawable objects with correct positions from a list of elements
*
* @param elementsToDraw A list of elements to draw
* @param gameWidth The width of the game board in tiles
* @param tileWidth The width of a tile
* @param tileHeight The height of a tile
* @param <K> Should be type Robot, Tile or Wall
* @param gameWidth The width of the game board in tiles
* @param tileWidth The width of a tile
* @param tileHeight The height of a tile
* @param <K> Should be type Robot, Tile or Wall
* @return A list of drawable objects
*/
private static <K> List<IDrawableObject> getDrawableObjectsFromElementList(List<K> elementsToDraw, int gameWidth, int tileWidth,
int tileHeight) {
List<IDrawableObject> drawableObjects = new ArrayList<>();
private static <K> List<DrawableObject> getDrawableObjectsFromElementList(List<K> elementsToDraw, int gameWidth,
int tileWidth, int tileHeight) {
List<DrawableObject> drawableObjects = new ArrayList<>();
int y = 0;
for (int i = 0; i < elementsToDraw.size(); i++) {
K currentElement = elementsToDraw.get(i);
@ -117,7 +123,7 @@ public final class IOUtil {
throw new IllegalArgumentException("Unknown element type passed to function.");
}
int rotation = getElementRotation(currentElement);
IDrawableObject drawableObject = new DrawableObject(region,
DrawableObject drawableObject = new DrawableObject(region,
x * tileWidth, y * tileHeight, tileWidth, tileHeight, rotation);
drawableObjects.add(drawableObject);
}
@ -126,8 +132,9 @@ public final class IOUtil {
/**
* Gets the amount of degrees to rotate an element
*
* @param element The element to rotate
* @param <K> Should be of type Tile, Robot or Wall
* @param <K> Should be of type Tile, Robot or Wall
* @return The amount of degrees the tile should be rotated to be properly displayed
*/
private static <K> int getElementRotation(K element) {

View File

@ -5,10 +5,10 @@ import inf112.fiasko.roborally.elementproperties.Action;
import inf112.fiasko.roborally.elementproperties.RobotID;
import inf112.fiasko.roborally.networking.containers.ErrorResponse;
import inf112.fiasko.roborally.networking.containers.GameStartInfo;
import inf112.fiasko.roborally.networking.containers.PowerdownContainer;
import inf112.fiasko.roborally.networking.containers.PowerDownContainer;
import inf112.fiasko.roborally.networking.containers.ProgamsContainer;
import inf112.fiasko.roborally.networking.containers.ProgramAndPowerdownRequest;
import inf112.fiasko.roborally.objects.IDeck;
import inf112.fiasko.roborally.objects.Deck;
import inf112.fiasko.roborally.objects.Player;
import inf112.fiasko.roborally.objects.ProgrammingCard;
import inf112.fiasko.roborally.objects.ProgrammingCardDeck;
@ -23,11 +23,12 @@ public final class NetworkUtil {
/**
* Registers all classes which can be sent between a server and a client
*
* @param kryo The kryo object to register the classes to
*/
public static void registerClasses(Kryo kryo) {
kryo.register(ErrorResponse.class);
kryo.register(IDeck.class);
kryo.register(Deck.class);
kryo.register(ProgrammingCard.class);
kryo.register(GameStartInfo.class);
kryo.register(ArrayList.class);
@ -37,7 +38,7 @@ public final class NetworkUtil {
kryo.register(Action.class);
kryo.register(ProgramAndPowerdownRequest.class);
kryo.register(ProgamsContainer.class);
kryo.register(PowerdownContainer.class);
kryo.register(PowerDownContainer.class);
kryo.register(HashMap.class);

View File

@ -6,10 +6,12 @@ import java.io.InputStream;
* This class helps with tasks related to resource loading
*/
public final class ResourceUtil {
private ResourceUtil() {}
private ResourceUtil() {
}
/**
* Gets an input stream for a given resource
*
* @param resourcePath The relative path from the resources folder to the resource
* @return An input stream
*/

View File

@ -35,10 +35,12 @@ public final class TextureConverterUtil {
private static Map<ParticleType, Boolean> tileSheetParticleHasRotatedTextureMappings;
private static Map<WallType, TextureConverterContainer> tileSheetWallTextureMappings;
private TextureConverterUtil() {}
private TextureConverterUtil() {
}
/**
* Gets a list of all disposable elements which should be disposed when the software closes
*
* @return A list of disposable elements
*/
public static List<Disposable> getDisposableElements() {
@ -50,6 +52,7 @@ public final class TextureConverterUtil {
/**
* Gets the texture representing the tile
*
* @param tile The tile to draw
* @return The texture to draw
*/
@ -74,6 +77,7 @@ public final class TextureConverterUtil {
/**
* Gets the texture representing the particle
*
* @param particle The particle to draw
* @return The texture to draw
*/
@ -98,6 +102,7 @@ public final class TextureConverterUtil {
/**
* Gets the texture representing the tile
*
* @param wall The wall to draw
* @return The texture to draw
*/
@ -125,6 +130,7 @@ public final class TextureConverterUtil {
/**
* Gets the texture representing the robot
*
* @param robot The robot to draw
* @return The texture to draw
*/
@ -134,17 +140,17 @@ public final class TextureConverterUtil {
} else if (robot.getRobotId() == RobotID.ROBOT_2) {
return new TextureRegion(robotsTexture, 64, 0, 64, 64);
} else if (robot.getRobotId() == RobotID.ROBOT_3) {
return new TextureRegion(robotsTexture, 2*64, 0, 64, 64);
return new TextureRegion(robotsTexture, 2 * 64, 0, 64, 64);
} else if (robot.getRobotId() == RobotID.ROBOT_4) {
return new TextureRegion(robotsTexture, 3*64, 0, 64, 64);
return new TextureRegion(robotsTexture, 3 * 64, 0, 64, 64);
} else if (robot.getRobotId() == RobotID.ROBOT_5) {
return new TextureRegion(robotsTexture, 4*64, 0, 64, 64);
return new TextureRegion(robotsTexture, 4 * 64, 0, 64, 64);
} else if (robot.getRobotId() == RobotID.ROBOT_6) {
return new TextureRegion(robotsTexture, 5*64, 0, 64, 64);
return new TextureRegion(robotsTexture, 5 * 64, 0, 64, 64);
} else if (robot.getRobotId() == RobotID.ROBOT_7) {
return new TextureRegion(robotsTexture, 6*64, 0, 64, 64);
return new TextureRegion(robotsTexture, 6 * 64, 0, 64, 64);
} else if (robot.getRobotId() == RobotID.ROBOT_8) {
return new TextureRegion(robotsTexture, 7*64, 0, 64, 64);
return new TextureRegion(robotsTexture, 7 * 64, 0, 64, 64);
}
throw new IllegalArgumentException("Robot has no drawable texture.");
}
@ -152,7 +158,7 @@ public final class TextureConverterUtil {
/**
* Checks whether a tile has textures for different rotations
*
* For a tile without a rotated texture, the texture needs to be rotated when rendering.
* <p>For a tile without a rotated texture, the texture needs to be rotated when rendering.</p>
*
* @param tile The tile to check
* @return True if rotated versions of the texture exists. False otherwise
@ -171,7 +177,7 @@ public final class TextureConverterUtil {
/**
* Checks whether a particle has textures for different rotations
*
* For a particle without a rotated texture, the texture needs to be rotated when rendering.
* <p>For a particle without a rotated texture, the texture needs to be rotated when rendering.</p>
*
* @param particle The particle to check
* @return True if rotated versions of the texture exists. False otherwise
@ -190,8 +196,8 @@ public final class TextureConverterUtil {
/**
* Loads mappings between a tile and texture
*
* Loads both information about mapping from a tile to a texture converter container and information about mapping
* from a tile to whether the tile has a rotated version of each texture
* <p>Loads both information about mapping from a tile to a texture converter container and information about mapping
* from a tile to whether the tile has a rotated version of each texture</p>
*
* @throws IOException If the mapping file can't be properly read
*/
@ -212,8 +218,8 @@ public final class TextureConverterUtil {
/**
* Loads mappings between a particle and a texture
*
* Loads both information about mapping from a particle to a texture converter container and information about
* mapping from a particle to whether the particle has a rotated version of each texture
* <p>Loads both information about mapping from a particle to a texture converter container and information about
* mapping from a particle to whether the particle has a rotated version of each texture</p>
*
* @throws IOException If the mapping file can't be properly read
*/
@ -251,15 +257,16 @@ public final class TextureConverterUtil {
/**
* Reads one line of texture mapping and puts it into the correct maps
* @param parameters The parameters describing the texture mapping of the element
* @param mapKey The key to store in the map
* @param textureMapping The map containing texture mappings
*
* @param parameters The parameters describing the texture mapping of the element
* @param mapKey The key to store in the map
* @param textureMapping The map containing texture mappings
* @param hasRotatedTextureMapping The map containing whether an element has rotated textures or not
* @param <K> The type of element that will be used for map keys
* @param <K> The type of element that will be used for map keys
*/
private static synchronized <K> void storeTextMappingInMap(String[] parameters, K mapKey,
Map<K,TextureConverterContainer> textureMapping,
Map<K,Boolean> hasRotatedTextureMapping) {
Map<K, TextureConverterContainer> textureMapping,
Map<K, Boolean> hasRotatedTextureMapping) {
TextureConverterContainer container;
int xNorth = Integer.parseInt(parameters[1]);
int yNorth = Integer.parseInt(parameters[2]);
@ -287,15 +294,16 @@ public final class TextureConverterUtil {
/**
* Gets a texture region based on the direction of the tile
*
* @param direction The direction the tile is facing
* @param xNorth The relative x position on the texture sheet if the tile is facing north
* @param yNorth The relative y position on the texture sheet if the tile is facing north
* @param xEast The relative x position on the texture sheet if the tile is facing east
* @param yEast The relative y position on the texture sheet if the tile is facing east
* @param xSouth The relative x position on the texture sheet if the tile is facing south
* @param ySouth The relative y position on the texture sheet if the tile is facing south
* @param xWest The relative x position on the texture sheet if the tile is facing west
* @param yWest The relative y position on the texture sheet if the tile is facing west
* @param xNorth The relative x position on the texture sheet if the tile is facing north
* @param yNorth The relative y position on the texture sheet if the tile is facing north
* @param xEast The relative x position on the texture sheet if the tile is facing east
* @param yEast The relative y position on the texture sheet if the tile is facing east
* @param xSouth The relative x position on the texture sheet if the tile is facing south
* @param ySouth The relative y position on the texture sheet if the tile is facing south
* @param xWest The relative x position on the texture sheet if the tile is facing west
* @param yWest The relative y position on the texture sheet if the tile is facing west
* @return The texture region containing the tile's texture
*/
private static TextureRegion getDirectionalTextureRegion(Direction direction, int xNorth, int yNorth, int xEast,
@ -321,6 +329,7 @@ public final class TextureConverterUtil {
/**
* Gets a texture on the main tiles texture sheet
*
* @param x The relative x coordinate on the sheet
* @param y The relative y coordinate on the sheet
* @return The texture region containing the texture
@ -328,7 +337,7 @@ public final class TextureConverterUtil {
private static TextureRegion getTextureOnSheet(int x, int y) {
final int tileTextureHeight = 300;
final int tileTextureWidth = 300;
return new TextureRegion(textureSheet, x*tileTextureWidth, y*tileTextureHeight,
return new TextureRegion(textureSheet, x * tileTextureWidth, y * tileTextureHeight,
tileTextureWidth, tileTextureHeight);
}

View File

@ -2,6 +2,7 @@ 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;

View File

@ -13,7 +13,7 @@ import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@RunWith (GdxTestRunner.class)
@RunWith(GdxTestRunner.class)
public class TextureConverterUtilTest {
private Tile tileNorth;
private Tile holeNorth;
@ -34,20 +34,20 @@ public class TextureConverterUtilTest {
@Test
public void tileTileConversion() {
assertEquals(4*300, tileTextureRegion.getRegionX());
assertEquals(4 * 300, tileTextureRegion.getRegionX());
assertEquals(0, tileTextureRegion.getRegionY());
}
@Test
public void tileHoleConversion() {
assertEquals(5*300, holeTextureRegion.getRegionX());
assertEquals(5 * 300, holeTextureRegion.getRegionX());
assertEquals(0, holeTextureRegion.getRegionY());
}
@Test
public void tileTransportBandSlowFacingEastConversion() {
assertEquals(3*300, transportBandSlowEastTextureRegion.getRegionX());
assertEquals(6*300, transportBandSlowEastTextureRegion.getRegionY());
assertEquals(3 * 300, transportBandSlowEastTextureRegion.getRegionX());
assertEquals(6 * 300, transportBandSlowEastTextureRegion.getRegionY());
}
@Test