From 7cb22379e723f9ef75f12a04edc9670b8aa658ae Mon Sep 17 00:00:00 2001 From: Tobydrama Date: Tue, 17 Mar 2020 17:23:57 +0100 Subject: [PATCH] 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. --- .../fiasko/roborally/objects/Board.java | 85 +++++++++++++++++++ .../roborally/objects/RoboRallyGame.java | 10 +-- .../fiasko/roborally/objects/BoardTest.java | 62 ++++++++++++++ 3 files changed, 152 insertions(+), 5 deletions(-) diff --git a/src/main/java/inf112/fiasko/roborally/objects/Board.java b/src/main/java/inf112/fiasko/roborally/objects/Board.java index cfbca4c..d58daa6 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/Board.java +++ b/src/main/java/inf112/fiasko/roborally/objects/Board.java @@ -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> listOfWallLasers = getPositionsOfWallOnBoard(WallType.WALL_LASER_SINGLE, + WallType.WALL_LASER_DOUBLE); + for (Robot robot:robots.values()) { + fireOneRobotLaser(robot.getPosition(),robot.getFacingDirection()); + } + for (BoardElementContainer 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 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); + } + } + + + } \ No newline at end of file diff --git a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java index 2eee13c..5a915aa 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java +++ b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java @@ -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(); + } } \ No newline at end of file diff --git a/src/test/java/inf112/fiasko/roborally/objects/BoardTest.java b/src/test/java/inf112/fiasko/roborally/objects/BoardTest.java index 7f10db1..7a62eb4 100644 --- a/src/test/java/inf112/fiasko/roborally/objects/BoardTest.java +++ b/src/test/java/inf112/fiasko/roborally/objects/BoardTest.java @@ -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 wallTypeNumberMap = new HashMap<>(); private final Map tileTypeNumberMap = new HashMap<>(); + private Grid tileGridforlaser; + private Grid wallGridforlaser; + private List 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);