Refaktorerer og forenkler Board litt

Lager en hjelpeklasse for Grid
Lager en hjelpeklasser for lasere
Bytter navn på noen metoder i Board
Legger til en interface for å lettere bruke Tile, Wall og Particle om hverandre
This commit is contained in:
Kristian Knarvik 2020-04-28 22:54:23 +02:00
parent 1747f1ea21
commit 4aae3663ad
15 changed files with 316 additions and 283 deletions

View File

@ -6,6 +6,8 @@ import inf112.fiasko.roborally.elementproperties.Position;
import inf112.fiasko.roborally.elementproperties.RobotID;
import inf112.fiasko.roborally.elementproperties.TileType;
import inf112.fiasko.roborally.elementproperties.WallType;
import inf112.fiasko.roborally.utility.GridUtil;
import inf112.fiasko.roborally.utility.LaserHelper;
import java.util.ArrayList;
import java.util.HashMap;
@ -25,6 +27,7 @@ public class Board {
private List<Robot> deadRobots;
private List<RobotID> realDeadRobots;
private List<TileType> dangerousTiles;
private List<BoardElementContainer<Wall>> wallLasers;
/**
* Initializes the board
@ -53,6 +56,8 @@ public class Board {
this.realDeadRobots = new ArrayList<>();
this.dangerousTiles = new ArrayList<>();
loadDangerousTileTypes();
wallLasers = getPositionsOfWallsOnBoard(WallType.WALL_LASER_SINGLE,
WallType.WALL_LASER_DOUBLE, WallType.WALL_LASER_TRIPLE);
}
/**
@ -68,9 +73,9 @@ public class Board {
}
/**
* All the Real dead player's robots.
* Gets a list of robots no longer part of the game
*
* @return A list of Robots.
* @return Robots no longer part of the game
*/
public List<RobotID> getRealDeadRobots() {
return realDeadRobots;
@ -111,9 +116,9 @@ public class Board {
* @return A list of robots
*/
public List<Robot> getAllRobots() {
List<Robot> robotsCopy = new ArrayList<>(robots.values());
robotsCopy.addAll(deadRobots);
List<Robot> robotsCopy = new ArrayList<>(deadRobots);
robotsCopy.replaceAll(Robot::copy);
robotsCopy.addAll(getAliveRobots());
return robotsCopy;
}
@ -123,7 +128,7 @@ public class Board {
* @return A list of all tiles on the board
*/
public List<Tile> getTiles() {
return getAllElementsFromGrid(tiles);
return GridUtil.getAllElementsFromGrid(tiles);
}
/**
@ -132,7 +137,7 @@ public class Board {
* @return A list of all the walls on the board
*/
public List<Wall> getWalls() {
return getAllElementsFromGrid(walls);
return GridUtil.getAllElementsFromGrid(walls);
}
/**
@ -141,7 +146,7 @@ public class Board {
* @return A list of all the particles on the board
*/
public List<Particle> getParticles() {
return getAllElementsFromGrid(particles);
return GridUtil.getAllElementsFromGrid(particles);
}
/**
@ -191,11 +196,9 @@ public class Board {
* @param powerDown The status of the power down
*/
public void setPowerDown(RobotID robotID, Boolean powerDown) {
Robot alternateRobot = getRobotFromDeadRobots(robotID);
if (robots.containsKey(robotID)) {
robots.get(robotID).setPowerDown(powerDown);
} else if (alternateRobot != null) {
alternateRobot.setPowerDown(powerDown);
Robot robot = getRobot(robotID);
if (robot != null) {
robot.setPowerDown(powerDown);
}
}
@ -203,10 +206,10 @@ public class Board {
* Sets the backup position of a given robot to a given position
*
* @param robotID The robot to change backup position for
* @param pos The robot's new backup position
* @param position The robot's new backup position
*/
public void setBackupPositionOfRobot(RobotID robotID, Position pos) {
robots.get(robotID).setBackupPosition(pos);
public void setBackupPositionOfRobot(RobotID robotID, Position position) {
robots.get(robotID).setBackupPosition(position);
}
/**
@ -216,13 +219,8 @@ public class Board {
* @return The power down status of the robot
*/
public boolean getPowerDown(RobotID robotID) {
Robot alternateRobot = getRobotFromDeadRobots(robotID);
if (robots.containsKey(robotID)) {
return robots.get(robotID).isInPowerDown();
} else if (alternateRobot != null) {
return alternateRobot.isInPowerDown();
}
return false;
Robot robot = getRobot(robotID);
return robot != null && robot.isInPowerDown();
}
/**
@ -275,7 +273,7 @@ public class Board {
/**
* Moves a robot one unit in a specified direction
*
* @param robotID ID of the robot to move
* @param robotID Id of the robot to move
* @param direction The direction to move the robot
* @return True if the robot moved away from its old position
*/
@ -315,7 +313,7 @@ public class Board {
if (tile == null) {
return false;
}
int tileTypeId = tile.getTileType().getTileTypeID();
int tileTypeId = tile.getType().getTileTypeID();
return tileTypeId >= 5 && tileTypeId <= 16;
}
@ -493,11 +491,11 @@ public class Board {
* @param robotID The RobotID of a robot
* @param flagID TileType of the flag we check
*/
public void updateFlagOnRobot(RobotID robotID, TileType flagID) {
public void updateRobotFlag(RobotID robotID, TileType flagID) {
Robot robot = robots.get(robotID);
int flagNr = flagID.getTileTypeID() % 16;
if (flagNr - 1 == robot.getLastFlagVisited()) {
robot.setLastFlagVisited(flagNr);
int flagNumber = flagID.getTileTypeID() % 16;
if (flagNumber - 1 == robot.getLastFlagVisited()) {
robot.setLastFlagVisited(flagNumber);
setHasTouchedFlagThisTurn(robotID, true);
}
}
@ -528,12 +526,10 @@ public class Board {
* Fires all lasers on the board and kills any robot that has taken to much damage after all lasers have fired.
*/
public void fireAllLasers() {
List<BoardElementContainer<Wall>> listOfWallLasers = getPositionsOfWallOnBoard(WallType.WALL_LASER_SINGLE,
WallType.WALL_LASER_DOUBLE, WallType.WALL_LASER_TRIPLE);
for (Robot robot : robots.values()) {
fireRobotLaser(robot.getPosition(), robot.getFacingDirection());
}
for (BoardElementContainer<Wall> laser : listOfWallLasers) {
for (BoardElementContainer<Wall> laser : wallLasers) {
fireWallLaser(laser);
}
}
@ -565,10 +561,10 @@ public class Board {
* @param tiles The tiles you want all positions for
* @return A list of BoardElementContainers
*/
public List<BoardElementContainer<Tile>> getPositionsOfTileOnBoard(TileType... tiles) {
public List<BoardElementContainer<Tile>> getPositionsOfTilesOnBoard(TileType... tiles) {
List<BoardElementContainer<Tile>> combinedList = new ArrayList<>();
for (TileType tile : tiles) {
combinedList.addAll(makeTileList(tile, this.tiles));
combinedList.addAll(GridUtil.getMatchingElements(tile, this.tiles));
}
return combinedList;
}
@ -579,10 +575,10 @@ public class Board {
* @param walls The walls you want all positions for
* @return A list of BoardElementContainers
*/
public List<BoardElementContainer<Wall>> getPositionsOfWallOnBoard(WallType... walls) {
public List<BoardElementContainer<Wall>> getPositionsOfWallsOnBoard(WallType... walls) {
List<BoardElementContainer<Wall>> combinedList = new ArrayList<>();
for (WallType wall : walls) {
combinedList.addAll(makeTileList(wall, this.walls));
combinedList.addAll(GridUtil.getMatchingElements(wall, this.walls));
}
return combinedList;
}
@ -649,7 +645,7 @@ public class Board {
if (tileRobotStepsOn == null) {
throw new IllegalArgumentException("The game board is missing a tile. This should not happen.");
}
TileType tileTypeRobotStepsOn = tileRobotStepsOn.getTileType();
TileType tileTypeRobotStepsOn = tileRobotStepsOn.getType();
if (dangerousTiles.contains(tileTypeRobotStepsOn)) {
killRobot(robot);
}
@ -690,58 +686,6 @@ public class Board {
}
}
/**
* Gets all elements on a grid
*
* @param grid The grid to get elements from
* @param <K> The type of the elements int the grid
* @return A list containing all the elements in the grid
*/
private <K> List<K> getAllElementsFromGrid(Grid<K> grid) {
List<K> elements = new ArrayList<>();
for (int y = grid.getHeight() - 1; y >= 0; y--) {
for (int x = 0; x < grid.getWidth(); x++) {
elements.add(grid.getElement(x, y));
}
}
return elements;
}
/**
* Finds all tiles/walls with a certain type
*
* @param type The type of tile/wall to look for
* @param grid The grid to look through
* @param <K> Type of the type to look for
* @param <T> Type of the grid
* @return List of BoardElementContainers
*/
private <K, T> List<BoardElementContainer<T>> makeTileList(K type, Grid<T> grid) {
List<BoardElementContainer<T>> objList = new ArrayList<>();
for (int y = grid.getHeight() - 1; y >= 0; y--) {
for (int x = 0; x < grid.getWidth(); x++) {
T gridElement = grid.getElement(x, y);
if (gridElement != null) {
if (gridElement.getClass().isAssignableFrom(Tile.class)) {
Tile tile = (Tile) gridElement;
if (tile.getTileType() == type) {
objList.add(new BoardElementContainer<>(gridElement, new Position(x, y)));
}
} else if (gridElement.getClass().isAssignableFrom(Wall.class)) {
Wall wall = (Wall) gridElement;
if (wall.getWallType() == type) {
objList.add(new BoardElementContainer<>(gridElement, new Position(x, y)));
}
} else {
throw new IllegalArgumentException("ListGrid has unknown type.");
}
}
}
}
return objList;
}
/**
* Kills all robots that have taken too much damage
*/
@ -764,7 +708,7 @@ public class Board {
List<Position> laserTargets = new ArrayList<>();
getLaserTarget(laserDirection, wallLaser.getPosition(), laserTargets);
Position hitPosition = laserTargets.get(laserTargets.size() - 1);
WallType laserType = wallLaser.getElement().getWallType();
WallType laserType = wallLaser.getElement().getType();
updateLaserDisplay(laserTargets, laserDirection, laserType);
if (getRobotOnPosition(hitPosition) != null) {
applyLaserDamage(laserType, robots.get(getRobotOnPosition(hitPosition)));
@ -844,20 +788,7 @@ public class Board {
* @param laserType The type of the laser shooting
*/
private void updateLaserBeamOnParticleGrid(Position addPosition, Direction laserDirection, WallType laserType) {
ParticleType laserParticleType;
switch (laserType) {
case WALL_LASER_SINGLE:
laserParticleType = ParticleType.LASER_BEAM_SINGLE;
break;
case WALL_LASER_DOUBLE:
laserParticleType = ParticleType.LASER_BEAM_DOUBLE;
break;
case WALL_LASER_TRIPLE:
laserParticleType = ParticleType.LASER_BEAM_TRIPLE;
break;
default:
throw new IllegalArgumentException("Invalid laser type encountered.");
}
ParticleType laserParticleType = LaserHelper.getParticleFromLaserType(laserType);
Particle laserParticle = new Particle(laserParticleType, laserDirection);
int positionX = addPosition.getXCoordinate();
int positionY = addPosition.getYCoordinate();
@ -865,105 +796,11 @@ public class Board {
if (particleAtPosition == null) {
particles.setElement(positionX, positionY, laserParticle);
} else {
particles.setElement(positionX, positionY, getNewLaserBeamParticle(laserParticle, particleAtPosition));
particles.setElement(positionX, positionY,
LaserHelper.getNewLaserBeamParticle(laserParticle, particleAtPosition));
}
}
/**
* Gets the new particle to use given the laser firing and the existing beam particle
*
* @param laserBeam The laser beam which is fired
* @param existingBeam The laser beam which already exists at a tile
* @return The particle which is a combination of the two
*/
private Particle getNewLaserBeamParticle(Particle laserBeam, Particle existingBeam) {
ParticleType laserBeamType = laserBeam.getParticleType();
ParticleType existingBeamType = existingBeam.getParticleType();
Direction laserDirection = laserBeam.getDirection();
Direction existingDirection = existingBeam.getDirection();
int forwardBeamsLaser = getNumberOfForwardBeams(laserBeamType);
int crossingBeamsLaser = getNumberOfPerpendicularBeams(laserBeamType);
int forwardBeamsExisting = getNumberOfForwardBeams(existingBeamType);
int crossingBeamsExisting = getNumberOfPerpendicularBeams(existingBeamType);
//Flip number of beams if beams are perpendicular
if (Direction.arePerpendicular(laserDirection, existingDirection)) {
int temp = forwardBeamsExisting;
forwardBeamsExisting = crossingBeamsExisting;
crossingBeamsExisting = temp;
}
//Calculates the new number of forward beams
int forwardBeams = getNumberOfBeams(forwardBeamsLaser, forwardBeamsExisting);
//Calculates the new number of crossing beams
int crossingBeams = getNumberOfBeams(crossingBeamsLaser, crossingBeamsExisting);
//The direction should be the direction with the least amount of beams
Direction newDirection;
if (forwardBeams > crossingBeams) {
newDirection = existingDirection;
} else {
newDirection = laserDirection;
}
//If using the existing direction and the beams are perpendicular, the direction needs to be rotated
if (newDirection.equals(existingDirection) &&
Direction.arePerpendicular(laserDirection, existingDirection)) {
newDirection = Direction.getLeftRotatedDirection(newDirection);
}
//If the particle does not exist, we have to rotate the beam to get the correct one
ParticleType newParticleType = getNewParticleType(forwardBeams, crossingBeams);
if (newParticleType == null) {
newParticleType = getNewParticleType(crossingBeams, forwardBeams);
newDirection = Direction.getLeftRotatedDirection(newDirection);
}
return new Particle(newParticleType, newDirection);
}
/**
* Gets the correct number of beams given existing beams and the beams to add
*
* @param newBeams The beam count of the new beam to add
* @param existingBeams The beam count of the existing beam
* @return The new number/thickness of beams/the beam
*/
private int getNumberOfBeams(int newBeams, int existingBeams) {
if ((newBeams + existingBeams) != 0 && (newBeams + existingBeams) % 3 == 0) {
return 3;
} else {
return Math.max(newBeams, existingBeams);
}
}
/**
* Gets a new particle type with the given number of beams
*
* @param forwardBeams The number of beams in the direction of the laser
* @param perpendicularBeams The number of beams in the perpendicular direction of the laser
* @return The correct particle type to be displayed
*/
private ParticleType getNewParticleType(int forwardBeams, int perpendicularBeams) {
return ParticleType.getParticleTypeFromID(10 * forwardBeams + perpendicularBeams);
}
/**
* Gets the number of beams in the forward direction given a particle type
*
* @param particleType The type of particle to check
* @return The number of beams in the forward direction of the laser beam
*/
private int getNumberOfForwardBeams(ParticleType particleType) {
return particleType.getParticleTypeID() / 10;
}
/**
* Gets the number of beams in the perpendicular direction given a particle type
*
* @param particleType The type of particle to check
* @return The number of beams in the perpendicular direction of the laser beam
*/
private int getNumberOfPerpendicularBeams(ParticleType particleType) {
return particleType.getParticleTypeID() % 10;
}
/**
* Gets the int corresponding to the flag a robot has last visited
*
@ -981,12 +818,9 @@ public class Board {
* @param hasTouched If the robot has touched a flag this turn
*/
public void setHasTouchedFlagThisTurn(RobotID robotID, boolean hasTouched) {
Robot aliveRobot = robots.get(robotID);
Robot deadRobot = getRobotFromDeadRobots(robotID);
if (aliveRobot != null) {
aliveRobot.setHasTouchedFlagThisTurn(hasTouched);
} else if (deadRobot != null) {
deadRobot.setHasTouchedFlagThisTurn(hasTouched);
Robot robot = getRobot(robotID);
if (robot != null) {
robot.setHasTouchedFlagThisTurn(hasTouched);
}
}
@ -1000,4 +834,19 @@ public class Board {
return robots.get(robotID).hasTouchedFlagThisTurn();
}
/**
* Gets the robot with the given robot id
*
* @param robotID The id of the robot to get
* @return The robot with the id or null if it's not in the game
*/
private Robot getRobot(RobotID robotID) {
Robot aliveRobot = robots.get(robotID);
if (aliveRobot == null) {
return getRobotFromDeadRobots(robotID);
} else {
return aliveRobot;
}
}
}

View File

@ -0,0 +1,26 @@
package inf112.fiasko.roborally.objects;
import inf112.fiasko.roborally.elementproperties.Direction;
/**
* Represents an element on the board
*
* @param <K> The type of the element
*/
public interface BoardElement<K> {
/**
* Gets the type of the element
*
* @return An enum value of type K
*/
K getType();
/**
* Gets the direction of the element
*
* @return The element's direction
*/
Direction getDirection();
}

View File

@ -7,7 +7,7 @@ import inf112.fiasko.roborally.elementproperties.Position;
*
* @param <K> The type of element
*/
public class BoardElementContainer<K> {
public class BoardElementContainer<K extends BoardElement> {
private final K element;
private final Position position;
@ -17,7 +17,7 @@ public class BoardElementContainer<K> {
* @param element The element
* @param position The position
*/
BoardElementContainer(K element, Position position) {
public BoardElementContainer(K element, Position position) {
this.element = element;
this.position = position;
}
@ -32,9 +32,9 @@ public class BoardElementContainer<K> {
}
/**
* Gets the position
* Gets the position of the element
*
* @return The position
* @return The position of the element
*/
public Position getPosition() {
return position;

View File

@ -6,7 +6,7 @@ import inf112.fiasko.roborally.elementproperties.ParticleType;
/**
* This class represents a particle
*/
public class Particle {
public class Particle implements BoardElement<ParticleType> {
private ParticleType particleType;
private Direction direction;
@ -25,20 +25,12 @@ public class Particle {
this.direction = direction;
}
/**
* Gets the particle type of the particle
*
* @return The particle's particle type
*/
public ParticleType getParticleType() {
@Override
public ParticleType getType() {
return particleType;
}
/**
* Gets the direction of the particle
*
* @return The particle's direction
*/
@Override
public Direction getDirection() {
return direction;
}

View File

@ -109,7 +109,7 @@ public class Phase {
if (gameBoard.hasTouchedFlagThisTurn(robotID)) {
continue;
}
gameBoard.updateFlagOnRobot(robotID, flag.getElement().getTileType());
gameBoard.updateRobotFlag(robotID, flag.getElement().getType());
checkIfPlayerWon(robotID, flags.size());
}
}
@ -165,7 +165,7 @@ public class Phase {
continue;
}
RobotID robotAtCogwheel = gameBoard.getRobotOnPosition(cogwheel.getPosition());
if (cogwheel.getElement().getTileType() == TileType.COGWHEEL_RIGHT) {
if (cogwheel.getElement().getType() == TileType.COGWHEEL_RIGHT) {
gameBoard.rotateRobotRight(robotAtCogwheel);
} else {
gameBoard.rotateRobotLeft(robotAtCogwheel);
@ -355,23 +355,23 @@ public class Phase {
* Generates lists containing board element containers with all tiles of certain types
*/
private void generateTileLists() {
cogwheels = gameBoard.getPositionsOfTileOnBoard(TileType.COGWHEEL_RIGHT,
cogwheels = gameBoard.getPositionsOfTilesOnBoard(TileType.COGWHEEL_RIGHT,
TileType.COGWHEEL_LEFT);
fastConveyorBelts = gameBoard.getPositionsOfTileOnBoard(TileType.CONVEYOR_BELT_FAST,
fastConveyorBelts = gameBoard.getPositionsOfTilesOnBoard(TileType.CONVEYOR_BELT_FAST,
TileType.CONVEYOR_BELT_FAST_RIGHT, TileType.CONVEYOR_BELT_FAST_LEFT,
TileType.CONVEYOR_BELT_FAST_SIDE_ENTRANCE_RIGHT,
TileType.CONVEYOR_BELT_FAST_SIDE_ENTRANCE_LEFT,
TileType.CONVEYOR_BELT_FAST_SIDE_ENTRANCES);
conveyorBelts = new ArrayList<>();
conveyorBelts.addAll(fastConveyorBelts);
conveyorBelts.addAll(gameBoard.getPositionsOfTileOnBoard(TileType.CONVEYOR_BELT_SLOW,
conveyorBelts.addAll(gameBoard.getPositionsOfTilesOnBoard(TileType.CONVEYOR_BELT_SLOW,
TileType.CONVEYOR_BELT_SLOW_RIGHT, TileType.CONVEYOR_BELT_SLOW_LEFT,
TileType.CONVEYOR_BELT_SLOW_SIDE_ENTRANCE_RIGHT,
TileType.CONVEYOR_BELT_SLOW_SIDE_ENTRANCE_LEFT,
TileType.CONVEYOR_BELT_SLOW_SIDE_ENTRANCES));
flags = gameBoard.getPositionsOfTileOnBoard(TileType.FLAG_1,
flags = gameBoard.getPositionsOfTilesOnBoard(TileType.FLAG_1,
TileType.FLAG_2, TileType.FLAG_3, TileType.FLAG_4);
oddPushers = gameBoard.getPositionsOfWallOnBoard(WallType.WALL_PUSHER_ODD);
evenPushers = gameBoard.getPositionsOfWallOnBoard(WallType.WALL_PUSHER_EVEN);
oddPushers = gameBoard.getPositionsOfWallsOnBoard(WallType.WALL_PUSHER_ODD);
evenPushers = gameBoard.getPositionsOfWallsOnBoard(WallType.WALL_PUSHER_EVEN);
}
}

View File

@ -228,7 +228,7 @@ public class RoboRallyGame implements DrawableGame, InteractableGame {
gameBoard = BoardLoaderUtil.loadBoard("boards/" + boardName, robots);
moveRobotsToSpawn();
repairTiles = gameBoard.getPositionsOfTileOnBoard(TileType.FLAG_1, TileType.FLAG_2, TileType.FLAG_3,
repairTiles = gameBoard.getPositionsOfTilesOnBoard(TileType.FLAG_1, TileType.FLAG_2, TileType.FLAG_3,
TileType.FLAG_4, TileType.WRENCH, TileType.WRENCH_AND_HAMMER);
if (host) {
@ -248,7 +248,7 @@ public class RoboRallyGame implements DrawableGame, InteractableGame {
for (Player player : playerList) {
RobotID robotID = player.getRobotID();
TileType robotSpawn = TileType.getTileTypeFromID(robotID.getRobotIDID() + 22);
List<BoardElementContainer<Tile>> spawnTileContainerList = gameBoard.getPositionsOfTileOnBoard(robotSpawn);
List<BoardElementContainer<Tile>> spawnTileContainerList = gameBoard.getPositionsOfTilesOnBoard(robotSpawn);
if (spawnTileContainerList.size() < 1) {
throw new IllegalArgumentException("The chosen board seems to be missing a robot spawn");
}

View File

@ -6,7 +6,7 @@ import inf112.fiasko.roborally.elementproperties.TileType;
/**
* This class represents a simple tile
*/
public class Tile {
public class Tile implements BoardElement<TileType> {
private TileType tileType;
private Direction direction;
@ -25,20 +25,12 @@ public class Tile {
this.direction = direction;
}
/**
* Gets the tile type of the tile
*
* @return The tile's tile type
*/
public TileType getTileType() {
@Override
public TileType getType() {
return tileType;
}
/**
* Gets the direction of the tile
*
* @return The tile's direction
*/
@Override
public Direction getDirection() {
return direction;
}

View File

@ -6,7 +6,7 @@ import inf112.fiasko.roborally.elementproperties.WallType;
/**
* This class represents a wall
*/
public class Wall {
public class Wall implements BoardElement<WallType> {
private final WallType wallType;
private final Direction direction;
@ -24,20 +24,12 @@ public class Wall {
this.direction = direction;
}
/**
* Gets the type of the wall
*
* @return The wall type
*/
public WallType getWallType() {
@Override
public WallType getType() {
return wallType;
}
/**
* Gets the direction of the wall
*
* @return The direction of the wall
*/
@Override
public Direction getDirection() {
return direction;
}

View File

@ -0,0 +1,55 @@
package inf112.fiasko.roborally.utility;
import inf112.fiasko.roborally.elementproperties.Position;
import inf112.fiasko.roborally.objects.BoardElement;
import inf112.fiasko.roborally.objects.BoardElementContainer;
import inf112.fiasko.roborally.objects.Grid;
import java.util.ArrayList;
import java.util.List;
/**
* A helper class containing helper methods fro a grid
*/
public class GridUtil {
/**
* Gets all elements in a grid
*
* @param grid The grid to get elements from
* @param <K> The type of the elements int the grid
* @return A list containing all the elements in the grid
*/
public static <K> List<K> getAllElementsFromGrid(Grid<K> grid) {
List<K> elements = new ArrayList<>();
for (int y = grid.getHeight() - 1; y >= 0; y--) {
for (int x = 0; x < grid.getWidth(); x++) {
elements.add(grid.getElement(x, y));
}
}
return elements;
}
/**
* Finds all tiles/walls with a certain type
*
* @param type The type of tile/wall to look for
* @param grid The grid to look through
* @param <K> Type of the type to look for
* @param <T> Type of the grid
* @return List of BoardElementContainers
*/
public static <K, T extends BoardElement> List<BoardElementContainer<T>> getMatchingElements(K type, Grid<T> grid) {
List<BoardElementContainer<T>> containerList = new ArrayList<>();
for (int y = grid.getHeight() - 1; y >= 0; y--) {
for (int x = 0; x < grid.getWidth(); x++) {
T gridElement = grid.getElement(x, y);
if (gridElement != null && gridElement.getType() == type) {
containerList.add(new BoardElementContainer<>(gridElement, new Position(x, y)));
}
}
}
return containerList;
}
}

View File

@ -0,0 +1,127 @@
package inf112.fiasko.roborally.utility;
import inf112.fiasko.roborally.elementproperties.Direction;
import inf112.fiasko.roborally.elementproperties.ParticleType;
import inf112.fiasko.roborally.elementproperties.WallType;
import inf112.fiasko.roborally.objects.Particle;
/**
* Helps with displaying laser beams
*/
public class LaserHelper {
/**
* Gets the correct particle type from a laser type
*
* @param laserType The type of laser firing
* @return The particle representing the laser's beam
*/
public static ParticleType getParticleFromLaserType(WallType laserType) {
switch (laserType) {
case WALL_LASER_SINGLE:
return ParticleType.LASER_BEAM_SINGLE;
case WALL_LASER_DOUBLE:
return ParticleType.LASER_BEAM_DOUBLE;
case WALL_LASER_TRIPLE:
return ParticleType.LASER_BEAM_TRIPLE;
default:
throw new IllegalArgumentException("Invalid laser type encountered.");
}
}
/**
* Gets the new particle to use given the laser firing and the existing beam particle
*
* @param laserBeam The laser beam which is fired
* @param existingBeam The laser beam which already exists at a tile
* @return The particle which is a combination of the two
*/
public static Particle getNewLaserBeamParticle(Particle laserBeam, Particle existingBeam) {
ParticleType laserBeamType = laserBeam.getType();
ParticleType existingBeamType = existingBeam.getType();
Direction laserDirection = laserBeam.getDirection();
Direction existingDirection = existingBeam.getDirection();
int forwardBeamsLaser = getNumberOfForwardBeams(laserBeamType);
int crossingBeamsLaser = getNumberOfPerpendicularBeams(laserBeamType);
int forwardBeamsExisting = getNumberOfForwardBeams(existingBeamType);
int crossingBeamsExisting = getNumberOfPerpendicularBeams(existingBeamType);
//Flip number of beams if beams are perpendicular
if (Direction.arePerpendicular(laserDirection, existingDirection)) {
int temp = forwardBeamsExisting;
forwardBeamsExisting = crossingBeamsExisting;
crossingBeamsExisting = temp;
}
//Calculates the new number of forward beams
int forwardBeams = getNumberOfBeams(forwardBeamsLaser, forwardBeamsExisting);
//Calculates the new number of crossing beams
int crossingBeams = getNumberOfBeams(crossingBeamsLaser, crossingBeamsExisting);
//The direction should be the direction with the least amount of beams
Direction newDirection;
if (forwardBeams > crossingBeams) {
newDirection = existingDirection;
} else {
newDirection = laserDirection;
}
//If using the existing direction and the beams are perpendicular, the direction needs to be rotated
if (newDirection.equals(existingDirection) &&
Direction.arePerpendicular(laserDirection, existingDirection)) {
newDirection = Direction.getLeftRotatedDirection(newDirection);
}
//If the particle does not exist, we have to rotate the beam to get the correct one
ParticleType newParticleType = getNewParticleType(forwardBeams, crossingBeams);
if (newParticleType == null) {
newParticleType = getNewParticleType(crossingBeams, forwardBeams);
newDirection = Direction.getLeftRotatedDirection(newDirection);
}
return new Particle(newParticleType, newDirection);
}
/**
* Gets the correct number of beams given existing beams and the beams to add
*
* @param newBeams The beam count of the new beam to add
* @param existingBeams The beam count of the existing beam
* @return The new number/thickness of beams/the beam
*/
private static int getNumberOfBeams(int newBeams, int existingBeams) {
if ((newBeams + existingBeams) != 0 && (newBeams + existingBeams) % 3 == 0) {
return 3;
} else {
return Math.max(newBeams, existingBeams);
}
}
/**
* Gets a new particle type with the given number of beams
*
* @param forwardBeams The number of beams in the direction of the laser
* @param perpendicularBeams The number of beams in the perpendicular direction of the laser
* @return The correct particle type to be displayed
*/
private static ParticleType getNewParticleType(int forwardBeams, int perpendicularBeams) {
return ParticleType.getParticleTypeFromID(10 * forwardBeams + perpendicularBeams);
}
/**
* Gets the number of beams in the forward direction given a particle type
*
* @param particleType The type of particle to check
* @return The number of beams in the forward direction of the laser beam
*/
private static int getNumberOfForwardBeams(ParticleType particleType) {
return particleType.getParticleTypeID() / 10;
}
/**
* Gets the number of beams in the perpendicular direction given a particle type
*
* @param particleType The type of particle to check
* @return The number of beams in the perpendicular direction of the laser beam
*/
private static int getNumberOfPerpendicularBeams(ParticleType particleType) {
return particleType.getParticleTypeID() % 10;
}
}

View File

@ -124,7 +124,7 @@ public final class TextureConverterUtil {
}
}
Direction direction = tile.getDirection();
TextureConverterContainer converterContainer = tileSheetTileTextureMappings.get(tile.getTileType());
TextureConverterContainer converterContainer = tileSheetTileTextureMappings.get(tile.getType());
if (converterContainer != null) {
return getDirectionalTextureRegion(direction, converterContainer.getXNorth(),
converterContainer.getYNorth(), converterContainer.getXEast(), converterContainer.getYEast(),
@ -149,7 +149,7 @@ public final class TextureConverterUtil {
}
}
Direction direction = particle.getDirection();
TextureConverterContainer converterContainer = tileSheetParticleTextureMappings.get(particle.getParticleType());
TextureConverterContainer converterContainer = tileSheetParticleTextureMappings.get(particle.getType());
if (converterContainer != null) {
return getDirectionalTextureRegion(direction, converterContainer.getXNorth(),
converterContainer.getYNorth(), converterContainer.getXEast(), converterContainer.getYEast(),
@ -177,7 +177,7 @@ public final class TextureConverterUtil {
return null;
}
Direction direction = wall.getDirection();
TextureConverterContainer converterContainer = tileSheetWallTextureMappings.get(wall.getWallType());
TextureConverterContainer converterContainer = tileSheetWallTextureMappings.get(wall.getType());
if (converterContainer != null) {
return getDirectionalTextureRegion(direction, converterContainer.getXNorth(),
converterContainer.getYNorth(), converterContainer.getXEast(), converterContainer.getYEast(),
@ -242,7 +242,7 @@ public final class TextureConverterUtil {
e.printStackTrace();
}
}
return tileSheetTileHasRotatedTextureMappings.get(tile.getTileType());
return tileSheetTileHasRotatedTextureMappings.get(tile.getType());
}
/**
@ -261,7 +261,7 @@ public final class TextureConverterUtil {
e.printStackTrace();
}
}
return tileSheetWallHasRotatedTextureMappings.get(wall.getWallType());
return tileSheetWallHasRotatedTextureMappings.get(wall.getType());
}
/**
@ -280,7 +280,7 @@ public final class TextureConverterUtil {
e.printStackTrace();
}
}
return tileSheetParticleHasRotatedTextureMappings.get(particle.getParticleType());
return tileSheetParticleHasRotatedTextureMappings.get(particle.getType());
}
/**

View File

@ -234,7 +234,7 @@ public class BoardTest {
public void flagGetsUpdatedOnRobotWithCorrectLastVisitedFlag() {
Robot testRobot = robotList.get(6);
assertEquals(0, testRobot.getLastFlagVisited());
board.updateFlagOnRobot(RobotID.ROBOT_7, TileType.FLAG_1);
board.updateRobotFlag(RobotID.ROBOT_7, TileType.FLAG_1);
assertEquals(1, testRobot.getLastFlagVisited());
}
@ -242,7 +242,7 @@ public class BoardTest {
public void flagDoesNotUpdatedOnRobotWithWrongLastVisitedFlag() {
Robot testRobot = robotList.get(6);
assertEquals(0, testRobot.getLastFlagVisited());
board.updateFlagOnRobot(RobotID.ROBOT_7, TileType.FLAG_2);
board.updateRobotFlag(RobotID.ROBOT_7, TileType.FLAG_2);
assertEquals(0, testRobot.getLastFlagVisited());
}
@ -347,74 +347,74 @@ public class BoardTest {
@Test
public void getPositionsOfTileOnBoardGivesCorrectAmountOfCogwheelLeftTiles() {
assertEquals((int) tileTypeNumberMap.get(TileType.COGWHEEL_LEFT),
boardWithDifferentAmountOfAllTypes.getPositionsOfTileOnBoard(TileType.COGWHEEL_LEFT).size());
boardWithDifferentAmountOfAllTypes.getPositionsOfTilesOnBoard(TileType.COGWHEEL_LEFT).size());
}
@Test
public void getPositionsOfTileOnBoardHasTypeCogwheelLeft() {
List<BoardElementContainer<Tile>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfTileOnBoard(TileType.COGWHEEL_LEFT);
List<BoardElementContainer<Tile>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfTilesOnBoard(TileType.COGWHEEL_LEFT);
assertTrue(checkIfAllElementsAreOfSpecificTileType(boardElemList, TileType.COGWHEEL_LEFT));
}
@Test
public void getPositionsOfTileOnBoardGivesCorrectAmountOfTileTiles() {
assertEquals((int) tileTypeNumberMap.get(TileType.TILE),
boardWithDifferentAmountOfAllTypes.getPositionsOfTileOnBoard(TileType.TILE).size());
boardWithDifferentAmountOfAllTypes.getPositionsOfTilesOnBoard(TileType.TILE).size());
}
@Test
public void getPositionsOfTileOnBoardHasTypeTile() {
List<BoardElementContainer<Tile>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfTileOnBoard(TileType.TILE);
List<BoardElementContainer<Tile>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfTilesOnBoard(TileType.TILE);
assertTrue(checkIfAllElementsAreOfSpecificTileType(boardElemList, TileType.TILE));
}
@Test
public void getPositionsOfWallOnBoardGivesCorrectAmountOfWallNormalWalls() {
assertEquals((int) wallTypeNumberMap.get(WallType.WALL_NORMAL),
boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_NORMAL).size());
boardWithDifferentAmountOfAllTypes.getPositionsOfWallsOnBoard(WallType.WALL_NORMAL).size());
}
@Test
public void getPositionsOfWallOnBoardHasTypeWallNormal() {
List<BoardElementContainer<Wall>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_NORMAL);
List<BoardElementContainer<Wall>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfWallsOnBoard(WallType.WALL_NORMAL);
assertTrue(checkIfAllElementsAreOfSpecificWallType(boardElemList, WallType.WALL_NORMAL));
}
@Test
public void getPositionsOfWallOnBoardGivesCorrectAmountOfWallCornerWalls() {
assertEquals((int) wallTypeNumberMap.get(WallType.WALL_CORNER),
boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_CORNER).size());
boardWithDifferentAmountOfAllTypes.getPositionsOfWallsOnBoard(WallType.WALL_CORNER).size());
}
@Test
public void getPositionsOfWallOnBoardHasTypeWallCorner() {
List<BoardElementContainer<Wall>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_CORNER);
List<BoardElementContainer<Wall>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfWallsOnBoard(WallType.WALL_CORNER);
assertTrue(checkIfAllElementsAreOfSpecificWallType(boardElemList, WallType.WALL_CORNER));
}
@Test
public void getPositionsOfWallOnBoardHasCorrectTypesWithMultipleParameters() {
List<BoardElementContainer<Tile>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfTileOnBoard(TileType.COGWHEEL_LEFT, TileType.COGWHEEL_RIGHT);
List<BoardElementContainer<Tile>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfTilesOnBoard(TileType.COGWHEEL_LEFT, TileType.COGWHEEL_RIGHT);
List<TileType> tileTypeList = new ArrayList<>();
List<TileType> tileTypeListResult = new ArrayList<>();
tileTypeList.add(TileType.COGWHEEL_LEFT);
tileTypeList.add(TileType.COGWHEEL_RIGHT);
for (BoardElementContainer<Tile> elem : boardElemList) {
tileTypeListResult.add(elem.getElement().getTileType());
tileTypeListResult.add(elem.getElement().getType());
}
assertTrue(tileTypeList.containsAll(tileTypeListResult) && tileTypeListResult.containsAll(tileTypeList));
}
private <K> boolean checkIfAllElementsAreOfSpecificWallType(List<BoardElementContainer<Wall>> elemList, K WallType) {
Predicate<BoardElementContainer<Wall>> pred = (element) -> element.getElement().getWallType() == WallType;
Predicate<BoardElementContainer<Wall>> pred = (element) -> element.getElement().getType() == WallType;
elemList.removeIf(pred);
return 0 == elemList.size();
}
private <K> boolean checkIfAllElementsAreOfSpecificTileType(List<BoardElementContainer<Tile>> elemList, K TileType) {
Predicate<BoardElementContainer<Tile>> pred = (element) -> element.getElement().getTileType() == TileType;
Predicate<BoardElementContainer<Tile>> pred = (element) -> element.getElement().getType() == TileType;
elemList.removeIf(pred);
return 0 == elemList.size();
}

View File

@ -20,12 +20,12 @@ public class ParticleTest {
@Test
public void getParticleTypeFromParticle() {
assertEquals(ParticleType.LASER_BEAM_SINGLE, particle.getParticleType());
assertEquals(ParticleType.LASER_BEAM_SINGLE, particle.getType());
}
@Test
public void getParticleTypeFromParticle2() {
assertEquals(ParticleType.LASER_BEAM_DOUBLE_CROSS, particle2.getParticleType());
assertEquals(ParticleType.LASER_BEAM_DOUBLE_CROSS, particle2.getType());
}

View File

@ -20,12 +20,12 @@ public class TileTest {
@Test
public void getTileTypeFromTile() {
assertEquals(TileType.HOLE, tile.getTileType());
assertEquals(TileType.HOLE, tile.getType());
}
@Test
public void getTileTypeFromTile2() {
assertEquals(TileType.COGWHEEL_RIGHT, tile2.getTileType());
assertEquals(TileType.COGWHEEL_RIGHT, tile2.getType());
}

View File

@ -10,19 +10,19 @@ public class WallTest {
@Test
public void testWallGetWallTypeNormal() {
Wall testGetWall = new Wall(WallType.WALL_NORMAL, Direction.NORTH);
assertEquals(WallType.WALL_NORMAL, testGetWall.getWallType());
assertEquals(WallType.WALL_NORMAL, testGetWall.getType());
}
@Test
public void testWallGetWallTypeCorner() {
Wall testGetWall = new Wall(WallType.WALL_CORNER, Direction.NORTH);
assertEquals(WallType.WALL_CORNER, testGetWall.getWallType());
assertEquals(WallType.WALL_CORNER, testGetWall.getType());
}
@Test
public void testWallGetWallTypeLaserSingle() {
Wall testGetWall = new Wall(WallType.WALL_LASER_SINGLE, Direction.NORTH);
assertEquals(WallType.WALL_LASER_SINGLE, testGetWall.getWallType());
assertEquals(WallType.WALL_LASER_SINGLE, testGetWall.getType());
}
@Test