This commit is contained in:
GabrielMagnus 2020-03-12 11:15:17 +01:00
commit 7e37fbdce9
4 changed files with 214 additions and 52 deletions

View File

@ -114,6 +114,14 @@ public class Board {
moveRobot(robotID, robots.get(robotID).getFacingDirection()); 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 * 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
@ -124,14 +132,14 @@ public class Board {
Robot robot = robots.get(robotID); Robot robot = robots.get(robotID);
Position robotPosition = robot.getPosition(); Position robotPosition = robot.getPosition();
Position newPosition = getNewPosition(robotPosition, direction); 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. //There is a wall blocking the robot. It can't proceed.
if (robotMoveIsStoppedByWall(robotPosition, newPosition, direction)) { if (robotMoveIsStoppedByWall(robotPosition, newPosition, direction)) {
return false; 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 another robot is blocking this robot's path, try to shove it.
if (hasRobotOnPosition(newPosition)) { if (hasRobotOnPosition(newPosition)) {
RobotID otherRobotID = getRobotOnPosition(newPosition); RobotID otherRobotID = getRobotOnPosition(newPosition);
@ -178,8 +186,20 @@ public class Board {
* @return True if a wall would stop its path * @return True if a wall would stop its path
*/ */
private boolean robotMoveIsStoppedByWall(Position robotPosition, Position newPosition, Direction direction) { private boolean robotMoveIsStoppedByWall(Position robotPosition, Position newPosition, Direction direction) {
return hasWallFacing(robotPosition, direction) || return hasWallFacing(robotPosition, direction) || (isValidPosition(newPosition) &&
hasWallFacing(newPosition, Direction.getReverseDirection(direction)); 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;
} }
/** /**
@ -189,10 +209,7 @@ public class Board {
* @return True if the robot was killed for leaving the board * @return True if the robot was killed for leaving the board
*/ */
private boolean killRobotIfGoesOutsideMap(Robot robot, Position newPosition) { private boolean killRobotIfGoesOutsideMap(Robot robot, Position newPosition) {
if (newPosition.getXCoordinate() < 0 if (!isValidPosition(newPosition)) {
|| newPosition.getXCoordinate() >= boardWidth
|| newPosition.getYCoordinate() < 0
|| newPosition.getYCoordinate() >= boardHeight) {
killRobot(robot); killRobot(robot);
return true; return true;
} }

View File

@ -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 <ProgrammingCard> 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<ProgrammingCard> 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);
}
}

View File

@ -1,5 +1,6 @@
package inf112.fiasko.roborally.objects; 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.Position;
import inf112.fiasko.roborally.element_properties.RobotID; import inf112.fiasko.roborally.element_properties.RobotID;
import inf112.fiasko.roborally.utility.BoardLoaderUtil; import inf112.fiasko.roborally.utility.BoardLoaderUtil;
@ -52,6 +53,14 @@ public class RoboRallyGame implements IDrawableGame {
return gameBoard.getAliveRobots(); 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 * Initializes the game with a debugging board
*/ */
@ -94,48 +103,83 @@ public class RoboRallyGame implements IDrawableGame {
* @throws InterruptedException If interrupted while trying to sleep * @throws InterruptedException If interrupted while trying to sleep
*/ */
private void runGameLoop() throws InterruptedException { private void runGameLoop() throws InterruptedException {
long cycleDelay = 600;
TimeUnit.SECONDS.sleep(3); TimeUnit.SECONDS.sleep(3);
gameBoard.rotateRobotRight(RobotID.ROBOT_1); makeMove(RobotID.ROBOT_1, Action.MOVE_1);
TimeUnit.MILLISECONDS.sleep(cycleDelay); makeMove(RobotID.ROBOT_1, Action.MOVE_2);
gameBoard.rotateRobotRight(RobotID.ROBOT_1); makeMove(RobotID.ROBOT_1, Action.BACK_UP);
TimeUnit.MILLISECONDS.sleep(cycleDelay); makeMove(RobotID.ROBOT_1, Action.BACK_UP);
gameBoard.moveRobotForward(RobotID.ROBOT_1); makeMove(RobotID.ROBOT_1, Action.MOVE_3);
TimeUnit.MILLISECONDS.sleep(cycleDelay); makeMove(RobotID.ROBOT_1, Action.ROTATE_LEFT);
gameBoard.rotateRobotLeft(RobotID.ROBOT_1); makeMove(RobotID.ROBOT_1, Action.U_TURN);
TimeUnit.MILLISECONDS.sleep(cycleDelay); makeMove(RobotID.ROBOT_1, Action.ROTATE_RIGHT);
gameBoard.moveRobotForward(RobotID.ROBOT_1); makeMove(RobotID.ROBOT_2, Action.ROTATE_LEFT);
TimeUnit.MILLISECONDS.sleep(cycleDelay); makeMove(RobotID.ROBOT_2, Action.MOVE_3);
gameBoard.moveRobotForward(RobotID.ROBOT_1); makeMove(RobotID.ROBOT_2, Action.MOVE_3);
TimeUnit.MILLISECONDS.sleep(cycleDelay); makeMove(RobotID.ROBOT_2, Action.BACK_UP);
gameBoard.rotateRobotRight(RobotID.ROBOT_1); makeMove(RobotID.ROBOT_2, Action.U_TURN);
TimeUnit.MILLISECONDS.sleep(cycleDelay); makeMove(RobotID.ROBOT_2, Action.BACK_UP);
gameBoard.moveRobotForward(RobotID.ROBOT_1); makeMove(RobotID.ROBOT_2, Action.BACK_UP);
TimeUnit.MILLISECONDS.sleep(cycleDelay); makeMove(RobotID.ROBOT_2, Action.BACK_UP);
gameBoard.rotateRobotRight(RobotID.ROBOT_2); makeMove(RobotID.ROBOT_2, Action.MOVE_3);
TimeUnit.MILLISECONDS.sleep(cycleDelay); makeMove(RobotID.ROBOT_2, Action.BACK_UP);
gameBoard.moveRobotForward(RobotID.ROBOT_2); makeMove(RobotID.ROBOT_2, Action.BACK_UP);
TimeUnit.MILLISECONDS.sleep(cycleDelay); makeMove(RobotID.ROBOT_2, Action.ROTATE_LEFT);
gameBoard.rotateRobotRight(RobotID.ROBOT_2); makeMove(RobotID.ROBOT_2, Action.U_TURN);
TimeUnit.MILLISECONDS.sleep(cycleDelay); makeMove(RobotID.ROBOT_2, Action.MOVE_1);
gameBoard.rotateRobotRight(RobotID.ROBOT_2); }
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_2); /**
TimeUnit.MILLISECONDS.sleep(cycleDelay); * Makes the given robot move according to to the action input.
gameBoard.rotateRobotRight(RobotID.ROBOT_2); * @param robotID The ID of the robot to move.
TimeUnit.MILLISECONDS.sleep(cycleDelay); * @param action The specific movement the robot is to take.
gameBoard.rotateRobotRight(RobotID.ROBOT_2); * @throws InterruptedException If interrupted wile trying to sleep.
TimeUnit.MILLISECONDS.sleep(cycleDelay); */
gameBoard.moveRobotForward(RobotID.ROBOT_2); private void makeMove(RobotID robotID, Action action) throws InterruptedException {
TimeUnit.MILLISECONDS.sleep(cycleDelay); if (!gameBoard.isRobotAlive(robotID)) {
gameBoard.moveRobotForward(RobotID.ROBOT_2); return;
TimeUnit.MILLISECONDS.sleep(cycleDelay); }
gameBoard.rotateRobotRight(RobotID.ROBOT_2); sleep();
TimeUnit.MILLISECONDS.sleep(cycleDelay); switch (action) {
gameBoard.rotateRobotRight(RobotID.ROBOT_2); case MOVE_1:
TimeUnit.MILLISECONDS.sleep(cycleDelay); moveForward(robotID);
gameBoard.moveRobotForward(RobotID.ROBOT_2); break;
TimeUnit.MILLISECONDS.sleep(cycleDelay); case MOVE_2:
gameBoard.moveRobotForward(RobotID.ROBOT_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;
default:
throw new IllegalArgumentException("Not a recognized action.");
}
}
/**
* 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);
} }
} }

View File

@ -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<ProgrammingCard> 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());
}
}