mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-06-27 11:44:42 +02:00
Merge branch 'master' of https://github.com/inf112-v20/Fiasko
This commit is contained in:
@ -1,9 +1,6 @@
|
||||
package inf112.fiasko.roborally.objects;
|
||||
|
||||
import inf112.fiasko.roborally.element_properties.*;
|
||||
import inf112.fiasko.roborally.utility.TextureConverterUtil;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@ -178,6 +175,19 @@ public class Board {
|
||||
return robots.containsKey(robot);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the flag of the robot if it stands on the correct flag.
|
||||
* @param robotID The RobotID of a robot
|
||||
* @param flagID TileType of the flag we check
|
||||
*/
|
||||
public void updateFlagOnRobot(RobotID robotID, TileType flagID) {
|
||||
Robot robot = robots.get(robotID);
|
||||
int flagNr = flagID.getTileTypeID() % 16;
|
||||
if (flagNr - 1 == robot.getLastFlagVisited()) {
|
||||
robot.setLastFlagVisitedAndUpdateBackupPosition(flagNr);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a potential robot move would be blocked by a wall
|
||||
* @param robotPosition The current position of the robot
|
||||
@ -296,7 +306,7 @@ public class Board {
|
||||
* @param direction The direction to move the element
|
||||
* @return The new position of the element
|
||||
*/
|
||||
private Position getNewPosition(Position oldPosition, Direction direction) {
|
||||
Position getNewPosition(Position oldPosition, Direction direction) {
|
||||
switch (direction) {
|
||||
case NORTH:
|
||||
return new Position(oldPosition.getXCoordinate(), oldPosition.getYCoordinate() - 1);
|
||||
@ -327,6 +337,13 @@ public class Board {
|
||||
return elements;
|
||||
}
|
||||
|
||||
public Tile getTileOnPosition(Position position) {
|
||||
if (!isValidPosition(position)) {
|
||||
throw new IllegalArgumentException("Position is not on the board!");
|
||||
}
|
||||
return tiles.getElement(position.getXCoordinate(), position.getYCoordinate());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of BoardElementContainers, containing all tiles and positions of given tile types
|
||||
* @param tiles The tiles you want all positions for
|
||||
@ -345,7 +362,7 @@ public class Board {
|
||||
* @param walls The walls you want all positions for
|
||||
* @return A list of BoardElementContainers
|
||||
*/
|
||||
public List<BoardElementContainer<Wall>> getPositionsOfTileOnBoard(WallType ... walls) {
|
||||
public List<BoardElementContainer<Wall>> getPositionsOfWallOnBoard(WallType ... walls) {
|
||||
List<BoardElementContainer<Wall>> combinedList = new ArrayList<>();
|
||||
for (WallType wall : walls) {
|
||||
combinedList.addAll(makeTileList(wall, this.walls));
|
||||
@ -367,18 +384,20 @@ public class Board {
|
||||
for (int y = grid.getHeight() - 1; y >= 0; y--) {
|
||||
for (int x = 0; x < grid.getWidth(); x++) {
|
||||
T gridElement = grid.getElement(x, y);
|
||||
if (gridElement.getClass().isAssignableFrom(Tile.class)) {
|
||||
Tile tile = (Tile) gridElement;
|
||||
if (tile.getTileType() == type) {
|
||||
objList.add(new BoardElementContainer<>(gridElement, new Position(x,y)));
|
||||
if (gridElement != null) {
|
||||
if (gridElement.getClass().isAssignableFrom(Tile.class)) {
|
||||
Tile tile = (Tile) gridElement;
|
||||
if (tile.getTileType() == type) {
|
||||
objList.add(new BoardElementContainer<>(gridElement, new Position(x,y)));
|
||||
}
|
||||
} else if (gridElement.getClass().isAssignableFrom(Wall.class)) {
|
||||
Wall wall = (Wall) gridElement;
|
||||
if (wall.getWallType() == type) {
|
||||
objList.add(new BoardElementContainer<>(gridElement, new Position(x,y)));
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("Grid has unknown type.");
|
||||
}
|
||||
} else if (gridElement.getClass().isAssignableFrom(Wall.class)) {
|
||||
Wall wall = (Wall) gridElement;
|
||||
if (wall.getWallType() == type) {
|
||||
objList.add(new BoardElementContainer<>(gridElement, new Position(x,y)));
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("Grid has unknown type.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import inf112.fiasko.roborally.element_properties.Position;
|
||||
*/
|
||||
public class BoardElementContainer <K>{
|
||||
K obj;
|
||||
Position pos;
|
||||
private Position pos;
|
||||
|
||||
/**
|
||||
* Initializes the BoardElementContainer
|
||||
|
@ -14,23 +14,16 @@ public class Player {
|
||||
private final String name;
|
||||
private boolean powerDownNextRound = false;
|
||||
private ProgrammingCardDeck playerDeck;
|
||||
private List <ProgrammingCard> program = new ArrayList();
|
||||
private List <ProgrammingCard> program;
|
||||
|
||||
/**
|
||||
* Instantiates a new player
|
||||
* @param robotID the global identifier of the robot
|
||||
* @param name the unique name of the player
|
||||
* @param playerDeck the hand of cards dealt to the player
|
||||
*/
|
||||
public Player(RobotID robotID, String name, ProgrammingCardDeck playerDeck) {
|
||||
public Player(RobotID robotID, String name) {
|
||||
this.robotID = robotID;
|
||||
this.name = name;
|
||||
this.playerDeck = playerDeck;
|
||||
program.add(0, null); //sets the initial values in program to null
|
||||
program.add(1, null);
|
||||
program.add(2, null);
|
||||
program.add(3, null);
|
||||
program.add(4, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -39,6 +32,14 @@ public class Player {
|
||||
*/
|
||||
public RobotID getRobotID(){return robotID;}
|
||||
|
||||
/**
|
||||
* Set the players deck to the given deck
|
||||
* @param playerDeck a deck of cards given to the player
|
||||
*/
|
||||
public void setPlayerDeck(ProgrammingCardDeck playerDeck){
|
||||
this.playerDeck=playerDeck;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives you the Name of the player
|
||||
* @return a player Name
|
||||
@ -70,32 +71,23 @@ public class Player {
|
||||
public void setPowerDownNextRound(boolean powerDownStatus) { this.powerDownNextRound = powerDownStatus;}
|
||||
|
||||
/**
|
||||
* Places a card in to the player program
|
||||
* @param card the card that is placed in to the player program
|
||||
* Gets the program from the player
|
||||
* @return List of programing cards
|
||||
*/
|
||||
public void setCardInProgram(ProgrammingCard card) {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
if (program.get(i) == null) {
|
||||
program.add(i, card);
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Program deck is full,tried to add to many cards");
|
||||
public List <ProgrammingCard> getProgramFromPlayer(){
|
||||
return program;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a card by the given index from the player program and returns it.
|
||||
* @param cardNr the index of the card that is being removed
|
||||
* @return the card that was removed from the program
|
||||
* Sets the Players program to the given list of programing cards
|
||||
* @param cardList list the size of 5 with programing cards
|
||||
*/
|
||||
public ProgrammingCard removeProgramCard(int cardNr) {
|
||||
if(cardNr<5 && cardNr>-1) {
|
||||
program.add(cardNr, null);
|
||||
return program.remove(cardNr + 1);
|
||||
public void setInProgram(List <ProgrammingCard> cardList){
|
||||
if(cardList.size() != 5){
|
||||
throw new IllegalArgumentException("list must contain 5 programing cards");
|
||||
}
|
||||
else{
|
||||
throw new IllegalArgumentException("cant remove more then index 4 or remove negatives");
|
||||
|
||||
else {
|
||||
program = new ArrayList<>(cardList);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,7 @@
|
||||
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.element_properties.TileType;
|
||||
|
||||
import inf112.fiasko.roborally.element_properties.*;
|
||||
import inf112.fiasko.roborally.utility.BoardLoaderUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -16,7 +14,8 @@ import java.util.concurrent.TimeUnit;
|
||||
*/
|
||||
public class RoboRallyGame implements IDrawableGame {
|
||||
private Board gameBoard;
|
||||
List<BoardElementContainer<Tile>> cogwheels;
|
||||
private List<BoardElementContainer<Tile>> cogwheels;
|
||||
private List<BoardElementContainer<Tile>> conveyorBelts;
|
||||
|
||||
public RoboRallyGame(boolean debug) {
|
||||
if (debug) {
|
||||
@ -90,6 +89,13 @@ public class RoboRallyGame implements IDrawableGame {
|
||||
gameBoard = BoardLoaderUtil.loadBoard("boards/Checkmate.txt", robots);
|
||||
cogwheels = gameBoard.getPositionsOfTileOnBoard(TileType.COGWHEEL_RIGHT,
|
||||
TileType.COGWHEEL_LEFT);
|
||||
conveyorBelts = gameBoard.getPositionsOfTileOnBoard(TileType.TRANSPORT_BAND_FAST,
|
||||
TileType.TRANSPORT_BAND_SLOW, TileType.TRANSPORT_BAND_FAST_SIDE_ENTRANCE_RIGHT,
|
||||
TileType.TRANSPORT_BAND_FAST_RIGHT, TileType.TRANSPORT_BAND_SLOW_RIGHT,
|
||||
TileType.TRANSPORT_BAND_SLOW_SIDE_ENTRANCE_RIGHT, TileType.TRANSPORT_BAND_FAST_SIDE_ENTRANCE_LEFT,
|
||||
TileType.TRANSPORT_BAND_FAST_LEFT, TileType.TRANSPORT_BAND_SLOW_LEFT,
|
||||
TileType.TRANSPORT_BAND_SLOW_SIDE_ENTRANCE_LEFT);
|
||||
|
||||
new Thread(() -> {
|
||||
try {
|
||||
runGameLoop();
|
||||
@ -204,4 +210,104 @@ public class RoboRallyGame implements IDrawableGame {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Boolean listContainsTile(Tile tile) {
|
||||
boolean containsTile = false;
|
||||
for (BoardElementContainer<Tile> conveyorBelt : conveyorBelts) {
|
||||
if (conveyorBelt.getObject() == tile) {
|
||||
containsTile = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return containsTile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves robots standing on conveyor belts in the direction of the conveyor belt.
|
||||
* Rotates robots being moved to a turn on the conveyor belt.
|
||||
* @throws InterruptedException If disturbed during sleep.
|
||||
*/
|
||||
private void moveConveyorBelts() throws InterruptedException {
|
||||
for (BoardElementContainer<Tile> conveyorBelt : conveyorBelts) {
|
||||
if (!gameBoard.hasRobotOnPosition(conveyorBelt.getPosition())) {
|
||||
continue;
|
||||
}
|
||||
Position newPosition = gameBoard.getNewPosition(conveyorBelt.getPosition(),
|
||||
conveyorBelt.getObject().getDirection());
|
||||
Tile nextTile = gameBoard.getTileOnPosition(newPosition);
|
||||
Direction currentDirection = conveyorBelt.getObject().getDirection();
|
||||
Direction nextDirection = nextTile.getDirection();
|
||||
RobotID robot = gameBoard.getRobotOnPosition(conveyorBelt.getPosition());
|
||||
if (listContainsTile(nextTile) && currentDirection != nextDirection) {
|
||||
if (currentDirection.equals(Direction.NORTH)) {
|
||||
if (nextDirection.equals(Direction.WEST)) {
|
||||
sleep();
|
||||
gameBoard.moveRobot(robot, currentDirection);
|
||||
sleep();
|
||||
gameBoard.rotateRobotLeft(robot);
|
||||
} else {
|
||||
sleep();
|
||||
gameBoard.moveRobot(robot, currentDirection);
|
||||
sleep();
|
||||
gameBoard.rotateRobotRight(robot);
|
||||
}
|
||||
} else if (currentDirection.equals(Direction.WEST)) {
|
||||
if (nextDirection.equals(Direction.SOUTH)) {
|
||||
sleep();
|
||||
gameBoard.moveRobot(robot, currentDirection);
|
||||
sleep();
|
||||
gameBoard.rotateRobotLeft(robot);
|
||||
} else {
|
||||
sleep();
|
||||
gameBoard.moveRobot(robot, currentDirection);
|
||||
sleep();
|
||||
gameBoard.rotateRobotLeft(robot);
|
||||
}
|
||||
} else if (currentDirection.equals(Direction.SOUTH)) {
|
||||
if (nextDirection.equals(Direction.EAST)) {
|
||||
sleep();
|
||||
gameBoard.moveRobot(robot, currentDirection);
|
||||
sleep();
|
||||
gameBoard.rotateRobotLeft(robot);
|
||||
} else {
|
||||
sleep();
|
||||
gameBoard.moveRobot(robot, currentDirection);
|
||||
sleep();
|
||||
gameBoard.rotateRobotRight(robot);
|
||||
}
|
||||
} else if (currentDirection.equals(Direction.EAST)) {
|
||||
if (nextDirection.equals(Direction.NORTH)) {
|
||||
sleep();
|
||||
gameBoard.moveRobot(robot, currentDirection);
|
||||
sleep();
|
||||
gameBoard.rotateRobotLeft(robot);
|
||||
} else {
|
||||
sleep();
|
||||
gameBoard.moveRobot(robot, currentDirection);
|
||||
sleep();
|
||||
gameBoard.rotateRobotRight(robot);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
sleep();
|
||||
gameBoard.moveRobot(gameBoard.getRobotOnPosition(conveyorBelt.getPosition()),
|
||||
conveyorBelt.getObject().getDirection());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks all flags for robots. Tries to update the flag of the robot.
|
||||
*/
|
||||
private void checkAllFlags() {
|
||||
List<BoardElementContainer<Tile>> listOfFlags = gameBoard.getPositionsOfTileOnBoard(TileType.FLAG_1,
|
||||
TileType.FLAG_2, TileType.FLAG_3, TileType.FLAG_4);
|
||||
for (BoardElementContainer<Tile> flag:listOfFlags) {
|
||||
Position flagPosition = flag.getPosition();
|
||||
if (gameBoard.hasRobotOnPosition(flagPosition)) {
|
||||
RobotID robot = gameBoard.getRobotOnPosition(flagPosition);
|
||||
gameBoard.updateFlagOnRobot(robot, flag.getObject().getTileType());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user