From 822ca33345b033a6f21daa3262d1e72854463a07 Mon Sep 17 00:00:00 2001 From: Steinar Aalstad Lillesund Date: Tue, 10 Mar 2020 16:02:34 +0100 Subject: [PATCH 1/6] Added A player class Started on the functions needed for a player. --- .../fiasko/roborally/objects/Player.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/main/java/inf112/fiasko/roborally/objects/Player.java diff --git a/src/main/java/inf112/fiasko/roborally/objects/Player.java b/src/main/java/inf112/fiasko/roborally/objects/Player.java new file mode 100644 index 0000000..7e28586 --- /dev/null +++ b/src/main/java/inf112/fiasko/roborally/objects/Player.java @@ -0,0 +1,58 @@ +package inf112.fiasko.roborally.objects; + +import inf112.fiasko.roborally.element_properties.RobotID; + +import java.util.ArrayList; +import java.util.List; + +public class Player { + private final RobotID robotID; + private final String name; + private boolean powerDownNextRound = false; + private ProgrammingCardDeck playerDeck; + private List program = new ArrayList(); + + // Constructor for the player class, it get assigned a Robot, a player nam and a deck + public Player(RobotID robotID, String name, ProgrammingCardDeck playerDeck) { + this.robotID = robotID; + this.name = name; + this.playerDeck = playerDeck; + } + + /** + * Gives you the RobotID of a player + * @return An RobotID + */ + public RobotID getRobotID(){return robotID;} + + public String getName() {return name;} + + public List getProgram() {return program;} + + public ProgrammingCardDeck getPlayerDeck() {return playerDeck;} + + public boolean getPowerDownNextRound() { return powerDownNextRound;} + + public void setPowerDownNextRound(boolean powerDownStatus) { this.powerDownNextRound = powerDownStatus;} + + public void setCardInProgram(ProgrammingCard card) { + for (int i = 0; i < 5; i++) { + if (program.size() == 0) { + program.add(i, card); + return; + } + if (program.get(i) == null) { + program.add(i, card); + return; + } + } + throw new IllegalArgumentException("Program deck is full,tried to add to many cards"); + } + + + public ProgrammingCard removeProgramCard(int cardNr) { + program.add(cardNr, null); + return program.remove(cardNr+1); + } + +} From e95ee32e6406e23444eb7592dcf606f9940ec20d Mon Sep 17 00:00:00 2001 From: Steinar Aalstad Lillesund Date: Tue, 10 Mar 2020 16:03:07 +0100 Subject: [PATCH 2/6] Added tests for the playerclass. Started on the tests needed for a player. --- .../fiasko/roborally/objects/PlayerTest.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/test/java/inf112/fiasko/roborally/objects/PlayerTest.java diff --git a/src/test/java/inf112/fiasko/roborally/objects/PlayerTest.java b/src/test/java/inf112/fiasko/roborally/objects/PlayerTest.java new file mode 100644 index 0000000..5e4e00b --- /dev/null +++ b/src/test/java/inf112/fiasko/roborally/objects/PlayerTest.java @@ -0,0 +1,43 @@ +package inf112.fiasko.roborally.objects; + +import inf112.fiasko.roborally.element_properties.Action; +import inf112.fiasko.roborally.element_properties.RobotID; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import java.util.ArrayList; +import java.util.List; + + +public class PlayerTest { + private Player playerTest; + @Before + public void setUp() { + List cards = new ArrayList(); + cards.add(new ProgrammingCard(10, Action.MOVE_1)); + cards.add(new ProgrammingCard(20, Action.MOVE_2)); + cards.add(new ProgrammingCard(30, Action.MOVE_3)); + cards.add(new ProgrammingCard(40, Action.BACK_UP)); + cards.add(new ProgrammingCard(50, Action.ROTATE_LEFT)); + ProgrammingCardDeck playerDeck = new ProgrammingCardDeck(cards); + playerTest = new Player(RobotID.ROBOT_1, "TestPlayer" ,playerDeck); + } + + @Test + public void setPowerDownStatusToTrue() { + playerTest.setPowerDownNextRound(true); + assertEquals(true, playerTest.getPowerDownNextRound()); + } + + @Test + public void setPowerDownStatusToFalse() { + playerTest.setPowerDownNextRound(false); + assertEquals(false, playerTest.getPowerDownNextRound()); + } + + @Test + public void cardGetsInsertedIntoProgram() { + playerTest.setCardInProgram(new ProgrammingCard(10,Action.MOVE_1)); + assertEquals(Action.MOVE_1,playerTest.getProgram().get(0).getAction()); + } +} From cba469d09fffe41f4f89ffe33c660985eb46f78a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torbj=C3=B8rn=20Lunde=20Jensen?= Date: Tue, 10 Mar 2020 17:29:36 +0100 Subject: [PATCH 3/6] Adds method that moves robot according to action enum --- .../fiasko/roborally/objects/Board.java | 8 + .../roborally/objects/RoboRallyGame.java | 145 +++++++++++++----- 2 files changed, 111 insertions(+), 42 deletions(-) diff --git a/src/main/java/inf112/fiasko/roborally/objects/Board.java b/src/main/java/inf112/fiasko/roborally/objects/Board.java index b068e24..05c471b 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/Board.java +++ b/src/main/java/inf112/fiasko/roborally/objects/Board.java @@ -115,6 +115,14 @@ public class Board { moveRobot(robotID, robots.get(robotID).getFacingDirection()); } + /** + * Moves a robot one unit backwards according to the direction it's currently facing + * @param robotID The robot to move + */ + public void reverseRobot(RobotID robotID) { + moveRobot(robotID, Direction.getReverseDirection(robots.get(robotID).getFacingDirection())); + } + /** * Moves a robot one unit in a specified direction * @param robotID ID of the robot to move diff --git a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java index f5dc4c2..46554bd 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java +++ b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java @@ -1,5 +1,6 @@ package inf112.fiasko.roborally.objects; +import inf112.fiasko.roborally.element_properties.Action; import inf112.fiasko.roborally.element_properties.Position; import inf112.fiasko.roborally.element_properties.RobotID; import inf112.fiasko.roborally.utility.BoardLoaderUtil; @@ -52,6 +53,14 @@ public class RoboRallyGame implements IDrawableGame { return gameBoard.getAliveRobots(); } + /** + * Makes the game thread wait a given time amount before continuing. + * @throws InterruptedException If interrupted while trying to sleep. + */ + private void sleep() throws InterruptedException { + long cycleDelay = 600; + TimeUnit.MILLISECONDS.sleep(cycleDelay); + } /** * Initializes the game with a debugging board */ @@ -94,48 +103,100 @@ public class RoboRallyGame implements IDrawableGame { * @throws InterruptedException If interrupted while trying to sleep */ private void runGameLoop() throws InterruptedException { - long cycleDelay = 600; TimeUnit.SECONDS.sleep(3); - gameBoard.rotateRobotRight(RobotID.ROBOT_1); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.rotateRobotRight(RobotID.ROBOT_1); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.moveRobotForward(RobotID.ROBOT_1); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.rotateRobotLeft(RobotID.ROBOT_1); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.moveRobotForward(RobotID.ROBOT_1); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.moveRobotForward(RobotID.ROBOT_1); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.rotateRobotRight(RobotID.ROBOT_1); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.moveRobotForward(RobotID.ROBOT_1); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.rotateRobotRight(RobotID.ROBOT_2); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.moveRobotForward(RobotID.ROBOT_2); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.rotateRobotRight(RobotID.ROBOT_2); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.rotateRobotRight(RobotID.ROBOT_2); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.moveRobotForward(RobotID.ROBOT_2); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.rotateRobotRight(RobotID.ROBOT_2); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.rotateRobotRight(RobotID.ROBOT_2); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.moveRobotForward(RobotID.ROBOT_2); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.moveRobotForward(RobotID.ROBOT_2); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.rotateRobotRight(RobotID.ROBOT_2); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.rotateRobotRight(RobotID.ROBOT_2); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.moveRobotForward(RobotID.ROBOT_2); - TimeUnit.MILLISECONDS.sleep(cycleDelay); - gameBoard.moveRobotForward(RobotID.ROBOT_2); + makeMove(RobotID.ROBOT_1, Action.MOVE_1); + + makeMove(RobotID.ROBOT_1, Action.MOVE_2); + + makeMove(RobotID.ROBOT_1, Action.BACK_UP); + + makeMove(RobotID.ROBOT_1, Action.BACK_UP); + + makeMove(RobotID.ROBOT_1, Action.MOVE_3); + + makeMove(RobotID.ROBOT_1, Action.ROTATE_LEFT); + + makeMove(RobotID.ROBOT_1, Action.U_TURN); + + makeMove(RobotID.ROBOT_1, Action.ROTATE_RIGHT); + + makeMove(RobotID.ROBOT_2, Action.ROTATE_LEFT); + + makeMove(RobotID.ROBOT_2, Action.MOVE_3); + + makeMove(RobotID.ROBOT_2, Action.MOVE_3); + + makeMove(RobotID.ROBOT_2, Action.BACK_UP); + + makeMove(RobotID.ROBOT_2, Action.U_TURN); + + makeMove(RobotID.ROBOT_2, Action.BACK_UP); + + makeMove(RobotID.ROBOT_2, Action.BACK_UP); + + makeMove(RobotID.ROBOT_2, Action.BACK_UP); + + makeMove(RobotID.ROBOT_2, Action.MOVE_3); + + makeMove(RobotID.ROBOT_2, Action.BACK_UP); + + makeMove(RobotID.ROBOT_2, Action.BACK_UP); + + makeMove(RobotID.ROBOT_2, Action.ROTATE_LEFT); + + makeMove(RobotID.ROBOT_2, Action.U_TURN); + } + + /** + * Makes the given robot move according to to the action input. + * @param robotID The ID of the robot to move. + * @param action The specific movement the robot is to take. + * @throws InterruptedException If interrupted wile trying to sleep. + */ + private void makeMove(RobotID robotID, Action action) throws InterruptedException { + if (!gameBoard.isRobotAlive(robotID)) { + return; + } + sleep(); + switch (action) { + case MOVE_1: + moveForward(robotID); + break; + case MOVE_2: + moveForward(robotID); + moveForward(robotID); + break; + case MOVE_3: + moveForward(robotID); + moveForward(robotID); + moveForward(robotID); + break; + case ROTATE_RIGHT: + gameBoard.rotateRobotRight(robotID); + break; + case ROTATE_LEFT: + gameBoard.rotateRobotLeft(robotID); + break; + case U_TURN: + gameBoard.rotateRobotLeft(robotID); + gameBoard.rotateRobotLeft(robotID); + break; + case BACK_UP: + gameBoard.reverseRobot(robotID); + break; + } + } + + /** + * Helper method for makeMove. Takes care of movement forward of given robot. + * @param robotID ID of the given robot. + * @throws InterruptedException If interrupted wile sleeping. + */ + private void moveForward(RobotID robotID) throws InterruptedException { + if (!gameBoard.isRobotAlive(robotID)) { + return; + } + sleep(); + gameBoard.moveRobotForward(robotID); } } From a86b4efbb7460d9f608aab47b665fe1f3e4038d0 Mon Sep 17 00:00:00 2001 From: torlunjen Date: Tue, 10 Mar 2020 18:18:32 +0100 Subject: [PATCH 4/6] =?UTF-8?q?Fikser=20bug=20der=20robot=20kan=20g=C3=A5?= =?UTF-8?q?=20forbi=20vegger=20som=20er=20p=C3=A5=20kanten=20av=20brettet.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fiasko/roborally/objects/Board.java | 18 ++++++++++------ .../roborally/objects/RoboRallyGame.java | 21 +------------------ 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/src/main/java/inf112/fiasko/roborally/objects/Board.java b/src/main/java/inf112/fiasko/roborally/objects/Board.java index 05c471b..3ecc739 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/Board.java +++ b/src/main/java/inf112/fiasko/roborally/objects/Board.java @@ -133,14 +133,14 @@ public class Board { Robot robot = robots.get(robotID); Position robotPosition = robot.getPosition(); Position newPosition = getNewPosition(robotPosition, direction); - //Robot tried to go outside of the map. Kill it. - if (killRobotIfGoesOutsideMap(robot, newPosition)) { - return true; - } //There is a wall blocking the robot. It can't proceed. if (robotMoveIsStoppedByWall(robotPosition, newPosition, direction)) { return false; } + //Robot tried to go outside of the map. Kill it. + if (killRobotIfGoesOutsideMap(robot, newPosition)) { + return true; + } //If another robot is blocking this robot's path, try to shove it. if (hasRobotOnPosition(newPosition)) { RobotID otherRobotID = getRobotOnPosition(newPosition); @@ -187,8 +187,14 @@ public class Board { * @return True if a wall would stop its path */ private boolean robotMoveIsStoppedByWall(Position robotPosition, Position newPosition, Direction direction) { - return hasWallFacing(robotPosition, direction) || - hasWallFacing(newPosition, Direction.getReverseDirection(direction)); + try { + return hasWallFacing(robotPosition, direction) || + hasWallFacing(newPosition, Direction.getReverseDirection(direction)); + } + catch (IllegalArgumentException e) { + return false; + } + } /** diff --git a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java index 46554bd..54ea4f6 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java +++ b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java @@ -105,46 +105,27 @@ public class RoboRallyGame implements IDrawableGame { private void runGameLoop() throws InterruptedException { TimeUnit.SECONDS.sleep(3); makeMove(RobotID.ROBOT_1, Action.MOVE_1); - makeMove(RobotID.ROBOT_1, Action.MOVE_2); - makeMove(RobotID.ROBOT_1, Action.BACK_UP); - makeMove(RobotID.ROBOT_1, Action.BACK_UP); - makeMove(RobotID.ROBOT_1, Action.MOVE_3); - makeMove(RobotID.ROBOT_1, Action.ROTATE_LEFT); - makeMove(RobotID.ROBOT_1, Action.U_TURN); - makeMove(RobotID.ROBOT_1, Action.ROTATE_RIGHT); - makeMove(RobotID.ROBOT_2, Action.ROTATE_LEFT); - makeMove(RobotID.ROBOT_2, Action.MOVE_3); - makeMove(RobotID.ROBOT_2, Action.MOVE_3); - makeMove(RobotID.ROBOT_2, Action.BACK_UP); - makeMove(RobotID.ROBOT_2, Action.U_TURN); - makeMove(RobotID.ROBOT_2, Action.BACK_UP); - makeMove(RobotID.ROBOT_2, Action.BACK_UP); - makeMove(RobotID.ROBOT_2, Action.BACK_UP); - makeMove(RobotID.ROBOT_2, Action.MOVE_3); - makeMove(RobotID.ROBOT_2, Action.BACK_UP); - makeMove(RobotID.ROBOT_2, Action.BACK_UP); - makeMove(RobotID.ROBOT_2, Action.ROTATE_LEFT); - makeMove(RobotID.ROBOT_2, Action.U_TURN); + makeMove(RobotID.ROBOT_2, Action.MOVE_1); } /** From 8d9a85946e57e7f6174ff8064327af9efe97125b Mon Sep 17 00:00:00 2001 From: torlunjen Date: Tue, 10 Mar 2020 18:26:16 +0100 Subject: [PATCH 5/6] Adds default label to RoboRallyGame::makeMove switch. --- .../java/inf112/fiasko/roborally/objects/RoboRallyGame.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java index 54ea4f6..f56c624 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java +++ b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java @@ -165,6 +165,8 @@ public class RoboRallyGame implements IDrawableGame { case BACK_UP: gameBoard.reverseRobot(robotID); break; + default: + throw new IllegalArgumentException("Not a recognized action."); } } From f22e577180084ab0158689ee37e8998c65e7c51d Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Tue, 10 Mar 2020 18:35:15 +0100 Subject: [PATCH 6/6] Legger til forbedring av posisjonssjekking i Board --- .../fiasko/roborally/objects/Board.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/main/java/inf112/fiasko/roborally/objects/Board.java b/src/main/java/inf112/fiasko/roborally/objects/Board.java index 3ecc739..8326711 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/Board.java +++ b/src/main/java/inf112/fiasko/roborally/objects/Board.java @@ -187,14 +187,20 @@ public class Board { * @return True if a wall would stop its path */ private boolean robotMoveIsStoppedByWall(Position robotPosition, Position newPosition, Direction direction) { - try { - return hasWallFacing(robotPosition, direction) || - hasWallFacing(newPosition, Direction.getReverseDirection(direction)); - } - catch (IllegalArgumentException e) { - return false; - } + return hasWallFacing(robotPosition, direction) || (isValidPosition(newPosition) && + hasWallFacing(newPosition, Direction.getReverseDirection(direction))); + } + /** + * Checks whether a given position is valid + * @param position The position to test + * @return True if the position is valid. False otherwise + */ + private boolean isValidPosition(Position position) { + return position.getXCoordinate() >= 0 + && position.getXCoordinate() < boardWidth + && position.getYCoordinate() >= 0 + && position.getYCoordinate() < boardHeight; } /** @@ -204,10 +210,7 @@ public class Board { * @return True if the robot was killed for leaving the board */ private boolean killRobotIfGoesOutsideMap(Robot robot, Position newPosition) { - if (newPosition.getXCoordinate() < 0 - || newPosition.getXCoordinate() >= boardWidth - || newPosition.getYCoordinate() < 0 - || newPosition.getYCoordinate() >= boardHeight) { + if (!isValidPosition(newPosition)) { killRobot(robot); return true; }