Made a laser function that fires all lasers

made a firealllasers function in roborallygame and added many support function in board to actually fire the lasers. added som of the needed tests.
This commit is contained in:
Tobydrama 2020-03-17 17:23:57 +01:00
parent 7f370a9dbc
commit 7cb22379e7
3 changed files with 152 additions and 5 deletions

View File

@ -46,6 +46,21 @@ public class Board {
this.deadRobots = new ArrayList<>();
}
/**
* 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);
for (Robot robot:robots.values()) {
fireOneRobotLaser(robot.getPosition(),robot.getFacingDirection());
}
for (BoardElementContainer<Wall> laser:listOfWallLasers) {
fireOneWallLaser(laser);
}
killAllDeadRobot();
}
/**
* Gets the height of the board
* @return The height of the board
@ -408,4 +423,74 @@ public class Board {
}
return objList;
}
/**
* Kills all robots that has taken too much damage
*/
private void killAllDeadRobot(){
for (Robot robot:robots.values()) {
if(robot.getDamageTaken()>=10){
killRobot(robot);
}
}
}
/**
* Fires one wall laser
* @param laser the wall laser that is being fired
*/
private void fireOneWallLaser(BoardElementContainer<Wall> laser){
Position hitPosition = lineForTheLaser(Direction.getReverseDirection(laser.getElement().getDirection()),laser.getPosition());
if(getRobotOnPosition(hitPosition)!=null){
laserDamage(laser.getElement().getWallType(),robots.get(getRobotOnPosition(hitPosition)));
}
}
/**
* fires on robot laser
* @param robotPosition the position of the robot firing the laser
* @param robotDirection the direction the robot is facing
*/
private void fireOneRobotLaser(Position robotPosition, Direction robotDirection){
Position positionInFront = getNewPosition(robotPosition,robotDirection);
if(!isValidPosition(positionInFront)||robotMoveIsStoppedByWall(robotPosition,positionInFront,robotDirection)){
return;
}
Position hitPosition = lineForTheLaser(robotDirection,positionInFront);
if(getRobotOnPosition(hitPosition)!=null){
laserDamage(WallType.WALL_LASER_SINGLE,robots.get(getRobotOnPosition(hitPosition)));
}
}
/**
* Applies the damage form the laser to the robot the laser hit
* @param laserType the type of laser that hit the robot
* @param robot the robot getting hit by the robot
*/
private void laserDamage(WallType laserType, Robot robot){
robot.setDamageTaken(robot.getDamageTaken()+laserType.getWallTypeID()-2);
}
/**
* Gets the Position of where the laser hits something
* @param direction the direction of the laser
* @param startPosition the start positon of the laser
* @return the position of the element that stopped the laser
*/
private Position lineForTheLaser(Direction direction, Position startPosition){
Position newPosition = getNewPosition(startPosition,direction);
if(!isValidPosition(newPosition) || robotMoveIsStoppedByWall(startPosition,newPosition,direction)){
return startPosition;
}
else if(getRobotOnPosition(newPosition)!=null){
return newPosition;
}
else{
return lineForTheLaser(direction,newPosition);
}
}
}

View File

@ -1,10 +1,6 @@
package inf112.fiasko.roborally.objects;
import inf112.fiasko.roborally.element_properties.Action;
import inf112.fiasko.roborally.element_properties.Direction;
import inf112.fiasko.roborally.element_properties.Position;
import inf112.fiasko.roborally.element_properties.RobotID;
import inf112.fiasko.roborally.element_properties.TileType;
import inf112.fiasko.roborally.element_properties.*;
import inf112.fiasko.roborally.utility.BoardLoaderUtil;
import java.io.IOException;
@ -311,4 +307,8 @@ public class RoboRallyGame implements IDrawableGame {
}
}
}
private void fireAllLasers(){
gameBoard.fireAllLasers();
}
}

View File

@ -5,6 +5,7 @@ import inf112.fiasko.roborally.element_properties.Position;
import inf112.fiasko.roborally.element_properties.RobotID;
import inf112.fiasko.roborally.element_properties.TileType;
import inf112.fiasko.roborally.element_properties.WallType;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
@ -37,6 +38,10 @@ public class BoardTest {
private Board boardWithDifferentAmountOfAllTypes;
private final Map<WallType,Integer> wallTypeNumberMap = new HashMap<>();
private final Map<TileType,Integer> tileTypeNumberMap = new HashMap<>();
private Grid<Tile> tileGridforlaser;
private Grid<Wall> wallGridforlaser;
private List<Robot> robotListforlaser;
private Board boardforlaser;
@BeforeClass
public static void globalSetUp() {
@ -53,6 +58,25 @@ public class BoardTest {
@Before
public void setUp() {
tileGridforlaser = new Grid<>(8, 8, new Tile(TileType.TILE, Direction.NORTH));
wallGridforlaser = new Grid<>(8, 8);
robotListforlaser = new ArrayList<>();
robotListforlaser.add(new Robot(RobotID.ROBOT_1, new Position(2,1)));
robotListforlaser.add(new Robot(RobotID.ROBOT_2, new Position(4,0)));
robotListforlaser.add(new Robot(RobotID.ROBOT_3, new Position(4,1)));
robotListforlaser.add(new Robot(RobotID.ROBOT_4, new Position(0,3)));
robotListforlaser.add(new Robot(RobotID.ROBOT_5, new Position(0,4)));
robotListforlaser.add(new Robot(RobotID.ROBOT_6, new Position(0,5)));
robotListforlaser.add(new Robot(RobotID.ROBOT_7, new Position(7,0)));
robotListforlaser.add(new Robot(RobotID.ROBOT_8, new Position(1,1)));
wallGridforlaser.setElement(2, 2, new Wall(WallType.WALL_NORMAL, Direction.SOUTH));
wallGridforlaser.setElement(1, 3, new Wall(WallType.WALL_LASER_SINGLE, Direction.SOUTH));
wallGridforlaser.setElement(7, 4, new Wall(WallType.WALL_LASER_DOUBLE, Direction.SOUTH));
wallGridforlaser.setElement(2, 3, new Wall(WallType.WALL_LASER_SINGLE, Direction.SOUTH));
wallGridforlaser.setElement(4, 3, new Wall(WallType.WALL_LASER_SINGLE, Direction.SOUTH));
boardforlaser = new Board(tileGridforlaser, wallGridforlaser, robotListforlaser);
tileGrid = new Grid<>(5, 5, new Tile(TileType.TILE, Direction.NORTH));
wallGrid = new Grid<>(5, 5);
robotList = new ArrayList<>();
@ -64,6 +88,8 @@ public class BoardTest {
robotList.add(new Robot(RobotID.ROBOT_6, someValidPosition6));
robotList.add(new Robot(RobotID.ROBOT_7, someValidPosition7));
robotList.add(new Robot(RobotID.ROBOT_8, someValidPosition8));
wallGrid.setElement(4, 4, new Wall(WallType.WALL_LASER_SINGLE, Direction.WEST));
wallGrid.setElement(1, 4, new Wall(WallType.WALL_LASER_SINGLE, Direction.EAST));
wallGrid.setElement(2, 1, new Wall(WallType.WALL_NORMAL, Direction.SOUTH));
wallGrid.setElement(2, 2, new Wall(WallType.WALL_NORMAL, Direction.EAST));
wallGrid.setElement(1, 2, new Wall(WallType.WALL_CORNER, Direction.NORTH_EAST));
@ -98,6 +124,42 @@ public class BoardTest {
}
@Test
public void robotHitByLaserGetsDamaged(){
Robot testRobot = robotListforlaser.get(7);
assertEquals(0, testRobot.getDamageTaken());
boardforlaser.fireAllLasers();
assertNotEquals(0,testRobot.getDamageTaken());
}
@Test
public void laserBlockedByWallDoesNotDamageRobot(){
Robot testRobot = robotListforlaser.get(0);
assertEquals(0, testRobot.getDamageTaken());
boardforlaser.fireAllLasers();
assertEquals(0,testRobot.getDamageTaken());
}
@Test
public void laserBlockedByRobotDoesNotDamageOtherRobot(){
Robot testRobot1 = robotListforlaser.get(1);
Robot testRobot2 = robotListforlaser.get(2);
testRobot2.setFacingDirection(Direction.EAST);
assertEquals(0, testRobot1.getDamageTaken());
assertEquals(0, testRobot2.getDamageTaken());
boardforlaser.fireAllLasers();
assertEquals(0,testRobot1.getDamageTaken());
assertNotEquals(0,testRobot2.getDamageTaken());
}
@Test
public void doubleLaserDamage(){
Robot testRobot = robotListforlaser.get(6);
assertEquals(0, testRobot.getDamageTaken());
boardforlaser.fireAllLasers();
assertEquals(2,testRobot.getDamageTaken());
}
@Test
public void flagGetsUpdatedOnRobotWithCorrectLastVisitedFlag() {
Robot testRobot = robotList.get(6);