mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-01-31 23:29:36 +01:00
Merge branch 'master' of https://github.com/inf112-v20/Fiasko
This commit is contained in:
commit
7e37fbdce9
@ -114,6 +114,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
|
||||
@ -124,14 +132,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);
|
||||
@ -178,8 +186,20 @@ 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));
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -189,10 +209,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;
|
||||
}
|
||||
|
58
src/main/java/inf112/fiasko/roborally/objects/Player.java
Normal file
58
src/main/java/inf112/fiasko/roborally/objects/Player.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
@ -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,83 @@ 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);
|
||||
makeMove(RobotID.ROBOT_2, Action.MOVE_1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user