mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-12-04 18:08:46 +01:00
Merge branch 'master' of https://github.com/inf112-v20/Fiasko
This commit is contained in:
@@ -150,7 +150,7 @@ public class Board {
|
||||
Position robotPosition = robot.getPosition();
|
||||
Position newPosition = getNewPosition(robotPosition, direction);
|
||||
//There is a wall blocking the robot. It can't proceed.
|
||||
if (robotMoveIsStoppedByWall(robotPosition, newPosition, direction)) {
|
||||
if (moveIsStoppedByWall(robotPosition, newPosition, direction)) {
|
||||
return false;
|
||||
}
|
||||
//Robot tried to go outside of the map. Kill it.
|
||||
@@ -224,13 +224,72 @@ public class Board {
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a potential robot move would be blocked by a wall
|
||||
* @param robotPosition The current position of the robot
|
||||
* @param newPosition The position the robot is trying to move to
|
||||
* @param direction The direction the robot is going
|
||||
* Gets the position 1 unit in a specific direction from another position
|
||||
* @param oldPosition The old/current position of the element
|
||||
* @param direction The direction to move the element
|
||||
* @return The new position of the element
|
||||
*/
|
||||
public Position getNewPosition(Position oldPosition, Direction direction) {
|
||||
switch (direction) {
|
||||
case NORTH:
|
||||
return new Position(oldPosition.getXCoordinate(), oldPosition.getYCoordinate() - 1);
|
||||
case SOUTH:
|
||||
return new Position(oldPosition.getXCoordinate(), oldPosition.getYCoordinate() + 1);
|
||||
case EAST:
|
||||
return new Position(oldPosition.getXCoordinate() + 1, oldPosition.getYCoordinate());
|
||||
case WEST:
|
||||
return new Position(oldPosition.getXCoordinate() - 1, oldPosition.getYCoordinate());
|
||||
default:
|
||||
throw new IllegalArgumentException("It's not possible to move in that direction.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the tile on a specific position
|
||||
* @param position The position to get a tile from
|
||||
* @return The tile on the given position
|
||||
*/
|
||||
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
|
||||
* @return A list of BoardElementContainers
|
||||
*/
|
||||
public List<BoardElementContainer<Tile>> getPositionsOfTileOnBoard(TileType ... tiles) {
|
||||
List<BoardElementContainer<Tile>> combinedList = new ArrayList<>();
|
||||
for (TileType tile : tiles) {
|
||||
combinedList.addAll(makeTileList(tile, this.tiles));
|
||||
}
|
||||
return combinedList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of BoardElementContainers, containing all tiles and positions of given wall types
|
||||
* @param walls The walls you want all positions for
|
||||
* @return A list of BoardElementContainers
|
||||
*/
|
||||
public List<BoardElementContainer<Wall>> getPositionsOfWallOnBoard(WallType... walls) {
|
||||
List<BoardElementContainer<Wall>> combinedList = new ArrayList<>();
|
||||
for (WallType wall : walls) {
|
||||
combinedList.addAll(makeTileList(wall, this.walls));
|
||||
}
|
||||
return combinedList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a potential move would be blocked by a wall
|
||||
* @param robotPosition The current position of whatever is trying to move
|
||||
* @param newPosition The position something is trying to move to
|
||||
* @param direction The direction something is going
|
||||
* @return True if a wall would stop its path
|
||||
*/
|
||||
private boolean robotMoveIsStoppedByWall(Position robotPosition, Position newPosition, Direction direction) {
|
||||
public boolean moveIsStoppedByWall(Position robotPosition, Position newPosition, Direction direction) {
|
||||
return hasWallFacing(robotPosition, direction) || (isValidPosition(newPosition) &&
|
||||
hasWallFacing(newPosition, Direction.getReverseDirection(direction)));
|
||||
}
|
||||
@@ -320,27 +379,6 @@ public class Board {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the position 1 unit in a specific direction from another position
|
||||
* @param oldPosition The old/current position of the element
|
||||
* @param direction The direction to move the element
|
||||
* @return The new position of the element
|
||||
*/
|
||||
public Position getNewPosition(Position oldPosition, Direction direction) {
|
||||
switch (direction) {
|
||||
case NORTH:
|
||||
return new Position(oldPosition.getXCoordinate(), oldPosition.getYCoordinate() - 1);
|
||||
case SOUTH:
|
||||
return new Position(oldPosition.getXCoordinate(), oldPosition.getYCoordinate() + 1);
|
||||
case EAST:
|
||||
return new Position(oldPosition.getXCoordinate() + 1, oldPosition.getYCoordinate());
|
||||
case WEST:
|
||||
return new Position(oldPosition.getXCoordinate() - 1, oldPosition.getYCoordinate());
|
||||
default:
|
||||
throw new IllegalArgumentException("It's not possible to move in that direction.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all elements on a grid
|
||||
* @param grid The grid to get elements from
|
||||
@@ -357,39 +395,6 @@ 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
|
||||
* @return A list of BoardElementContainers
|
||||
*/
|
||||
public List<BoardElementContainer<Tile>> getPositionsOfTileOnBoard(TileType ... tiles) {
|
||||
List<BoardElementContainer<Tile>> combinedList = new ArrayList<>();
|
||||
for (TileType tile : tiles) {
|
||||
combinedList.addAll(makeTileList(tile, this.tiles));
|
||||
}
|
||||
return combinedList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of BoardElementContainers, containing all tiles and positions of given wall types
|
||||
* @param walls The walls you want all positions for
|
||||
* @return A list of BoardElementContainers
|
||||
*/
|
||||
public List<BoardElementContainer<Wall>> getPositionsOfWallOnBoard(WallType... walls) {
|
||||
List<BoardElementContainer<Wall>> combinedList = new ArrayList<>();
|
||||
for (WallType wall : walls) {
|
||||
combinedList.addAll(makeTileList(wall, this.walls));
|
||||
}
|
||||
return combinedList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds all position of an obj and makes a list of BoardElementContainers
|
||||
* @param type Type of obj
|
||||
|
||||
@@ -252,22 +252,68 @@ public class RoboRallyGame implements IDrawableGame {
|
||||
* @throws InterruptedException If disturbed during sleep
|
||||
*/
|
||||
private void moveConveyorBelts(List<BoardElementContainer<Tile>> conveyorBelts) throws InterruptedException {
|
||||
List<BoardElementContainer<Tile>> conveyorBeltsWithRobotsThatShouldMove =
|
||||
conveyorBeltsThatCanMoveWithoutConflict(conveyorBelts);
|
||||
for (BoardElementContainer<Tile> conveyorBelt : conveyorBeltsWithRobotsThatShouldMove) {
|
||||
Direction currentDirection = conveyorBelt.getElement().getDirection();
|
||||
RobotID robot = gameBoard.getRobotOnPosition(conveyorBelt.getPosition());
|
||||
Position newPosition = gameBoard.getNewPosition(conveyorBelt.getPosition(), currentDirection);
|
||||
Tile nextTile = gameBoard.getTileOnPosition(newPosition);
|
||||
|
||||
doConveyorBeltMovement(robot, currentDirection, nextTile);
|
||||
}
|
||||
}
|
||||
|
||||
private List<BoardElementContainer<Tile>> conveyorBeltsThatCanMoveWithoutConflict(
|
||||
List<BoardElementContainer<Tile>> conveyorBelts) {
|
||||
|
||||
List<BoardElementContainer<Tile>> nonConflictConveyorBelts = new ArrayList<>();
|
||||
for (BoardElementContainer<Tile> conveyorBelt : conveyorBelts) {
|
||||
if (!gameBoard.hasRobotOnPosition(conveyorBelt.getPosition())) {
|
||||
continue;
|
||||
if (gameBoard.hasRobotOnPosition(conveyorBelt.getPosition())) {
|
||||
nonConflictConveyorBelts.add(conveyorBelt);
|
||||
}
|
||||
Position conveyorBeltPosition = conveyorBelt.getPosition();
|
||||
Tile conveyorBeltTile = conveyorBelt.getElement();
|
||||
}
|
||||
for (BoardElementContainer<Tile> conveyorBeltWithRobot : nonConflictConveyorBelts) {
|
||||
Position conveyorBeltPosition = conveyorBeltWithRobot.getPosition();
|
||||
Tile conveyorBeltTile = conveyorBeltWithRobot.getElement();
|
||||
|
||||
Position newPosition = gameBoard.getNewPosition(conveyorBeltPosition, conveyorBeltTile.getDirection());
|
||||
Tile nextTile = gameBoard.getTileOnPosition(newPosition);
|
||||
|
||||
Direction currentDirection = conveyorBeltTile.getDirection();
|
||||
RobotID robot = gameBoard.getRobotOnPosition(conveyorBeltPosition);
|
||||
Position beyondNextPositionStraight = gameBoard.getNewPosition(newPosition, conveyorBeltTile.getDirection());
|
||||
Tile beyondNextTileStraight = gameBoard.getTileOnPosition(beyondNextPositionStraight);
|
||||
|
||||
//TODO: Check whether the robot is able to move before moving. Alternatively: Save position and direction
|
||||
// of each robot and revert if a collision is found.
|
||||
doConveyorBeltMovement(robot, currentDirection, nextTile);
|
||||
Position beyondNextPositionLeft = gameBoard.getNewPosition(newPosition,
|
||||
Direction.getLeftRotatedDirection(conveyorBeltTile.getDirection()));
|
||||
Tile beyondNextTileLeft = gameBoard.getTileOnPosition(beyondNextPositionLeft);
|
||||
|
||||
Position beyondNextPositionRight = gameBoard.getNewPosition(newPosition,
|
||||
Direction.getRightRotatedDirection(conveyorBeltTile.getDirection()));
|
||||
Tile beyondNextTileRight = gameBoard.getTileOnPosition(beyondNextPositionRight);
|
||||
|
||||
|
||||
|
||||
if (conveyorBeltTile.getDirection() == Direction.getReverseDirection(nextTile.getDirection()) &&
|
||||
nonConflictConveyorBelts.contains(new BoardElementContainer<>(nextTile, newPosition))) {
|
||||
nonConflictConveyorBelts.remove(conveyorBeltWithRobot);
|
||||
}
|
||||
else if (conveyorBeltTile.getDirection() == Direction.getReverseDirection(
|
||||
beyondNextTileStraight.getDirection()) && nonConflictConveyorBelts.contains(
|
||||
new BoardElementContainer<>(beyondNextTileStraight, beyondNextPositionStraight))) {
|
||||
nonConflictConveyorBelts.remove(conveyorBeltWithRobot);
|
||||
}
|
||||
else if (conveyorBeltTile.getDirection() == Direction.getLeftRotatedDirection(
|
||||
beyondNextTileLeft.getDirection()) && nonConflictConveyorBelts.contains(
|
||||
new BoardElementContainer<>(beyondNextTileLeft, beyondNextPositionLeft))) {
|
||||
nonConflictConveyorBelts.remove(conveyorBeltWithRobot);
|
||||
}
|
||||
else if (conveyorBeltTile.getDirection() == Direction.getRightRotatedDirection(
|
||||
beyondNextTileRight.getDirection()) && nonConflictConveyorBelts.contains(
|
||||
new BoardElementContainer<>(beyondNextTileRight, beyondNextPositionRight))) {
|
||||
nonConflictConveyorBelts.remove(conveyorBeltWithRobot);
|
||||
}
|
||||
}
|
||||
return nonConflictConveyorBelts;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user