Fikser opp i moveConveyorBelts og fikser skille mellom raske og trege transportbånd

Bytter navn fra TransportBand til ConveyorBelt
Bytter ut listContainsTile med en generisk predikattester
This commit is contained in:
Kristian Knarvik 2020-03-16 19:37:21 +01:00
parent 10e31033e0
commit ee753764eb
5 changed files with 86 additions and 100 deletions

View File

@ -8,18 +8,18 @@ public enum TileType {
HOLE (2), HOLE (2),
COGWHEEL_RIGHT (3), COGWHEEL_RIGHT (3),
COGWHEEL_LEFT (4), COGWHEEL_LEFT (4),
TRANSPORT_BAND_SLOW (5), CONVEYOR_BELT_SLOW(5),
TRANSPORT_BAND_SLOW_RIGHT (6), CONVEYOR_BELT_SLOW_RIGHT(6),
TRANSPORT_BAND_SLOW_LEFT (7), CONVEYOR_BELT_SLOW_LEFT(7),
TRANSPORT_BAND_SLOW_SIDE_ENTRANCES (8), CONVEYOR_BELT_SLOW_SIDE_ENTRANCES(8),
TRANSPORT_BAND_SLOW_SIDE_ENTRANCE_LEFT (9), CONVEYOR_BELT_SLOW_SIDE_ENTRANCE_LEFT(9),
TRANSPORT_BAND_SLOW_SIDE_ENTRANCE_RIGHT (10), CONVEYOR_BELT_SLOW_SIDE_ENTRANCE_RIGHT(10),
TRANSPORT_BAND_FAST (11), CONVEYOR_BELT_FAST(11),
TRANSPORT_BAND_FAST_RIGHT (12), CONVEYOR_BELT_FAST_RIGHT(12),
TRANSPORT_BAND_FAST_LEFT (13), CONVEYOR_BELT_FAST_LEFT(13),
TRANSPORT_BAND_FAST_SIDE_ENTRANCES (14), CONVEYOR_BELT_FAST_SIDE_ENTRANCES(14),
TRANSPORT_BAND_FAST_SIDE_ENTRANCE_LEFT (15), CONVEYOR_BELT_FAST_SIDE_ENTRANCE_LEFT(15),
TRANSPORT_BAND_FAST_SIDE_ENTRANCE_RIGHT (16), CONVEYOR_BELT_FAST_SIDE_ENTRANCE_RIGHT(16),
FLAG_1 (17), FLAG_1 (17),
FLAG_2 (18), FLAG_2 (18),
FLAG_3 (19), FLAG_3 (19),

View File

@ -8,6 +8,7 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
/** /**
* This class represent a game which is drawable using libgdx * This class represent a game which is drawable using libgdx
@ -16,6 +17,7 @@ public class RoboRallyGame implements IDrawableGame {
private Board gameBoard; private Board gameBoard;
private List<BoardElementContainer<Tile>> cogwheels; private List<BoardElementContainer<Tile>> cogwheels;
private List<BoardElementContainer<Tile>> conveyorBelts; private List<BoardElementContainer<Tile>> conveyorBelts;
private List<BoardElementContainer<Tile>> fastConveyorBelts;
public RoboRallyGame(boolean debug) { public RoboRallyGame(boolean debug) {
if (debug) { if (debug) {
@ -89,12 +91,18 @@ public class RoboRallyGame implements IDrawableGame {
gameBoard = BoardLoaderUtil.loadBoard("boards/Checkmate.txt", robots); gameBoard = BoardLoaderUtil.loadBoard("boards/Checkmate.txt", robots);
cogwheels = gameBoard.getPositionsOfTileOnBoard(TileType.COGWHEEL_RIGHT, cogwheels = gameBoard.getPositionsOfTileOnBoard(TileType.COGWHEEL_RIGHT,
TileType.COGWHEEL_LEFT); TileType.COGWHEEL_LEFT);
conveyorBelts = gameBoard.getPositionsOfTileOnBoard(TileType.TRANSPORT_BAND_FAST, fastConveyorBelts = gameBoard.getPositionsOfTileOnBoard(TileType.CONVEYOR_BELT_FAST,
TileType.TRANSPORT_BAND_SLOW, TileType.TRANSPORT_BAND_FAST_SIDE_ENTRANCE_RIGHT, TileType.CONVEYOR_BELT_FAST_RIGHT, TileType.CONVEYOR_BELT_FAST_LEFT,
TileType.TRANSPORT_BAND_FAST_RIGHT, TileType.TRANSPORT_BAND_SLOW_RIGHT, TileType.CONVEYOR_BELT_FAST_SIDE_ENTRANCE_RIGHT,
TileType.TRANSPORT_BAND_SLOW_SIDE_ENTRANCE_RIGHT, TileType.TRANSPORT_BAND_FAST_SIDE_ENTRANCE_LEFT, TileType.CONVEYOR_BELT_FAST_SIDE_ENTRANCE_LEFT,
TileType.TRANSPORT_BAND_FAST_LEFT, TileType.TRANSPORT_BAND_SLOW_LEFT, TileType.CONVEYOR_BELT_FAST_SIDE_ENTRANCES);
TileType.TRANSPORT_BAND_SLOW_SIDE_ENTRANCE_LEFT); conveyorBelts = new ArrayList<>();
conveyorBelts.addAll(fastConveyorBelts);
conveyorBelts.addAll(gameBoard.getPositionsOfTileOnBoard(TileType.CONVEYOR_BELT_SLOW,
TileType.CONVEYOR_BELT_SLOW_RIGHT, TileType.CONVEYOR_BELT_SLOW_LEFT,
TileType.CONVEYOR_BELT_SLOW_SIDE_ENTRANCE_RIGHT,
TileType.CONVEYOR_BELT_SLOW_SIDE_ENTRANCE_LEFT,
TileType.CONVEYOR_BELT_SLOW_SIDE_ENTRANCES));
new Thread(() -> { new Thread(() -> {
try { try {
@ -211,87 +219,65 @@ public class RoboRallyGame implements IDrawableGame {
} }
} }
private Boolean listContainsTile(Tile tile) { /**
boolean containsTile = false; * Checks whether a given list has at least one element as defined by the predicate
for (BoardElementContainer<Tile> conveyorBelt : conveyorBelts) { * @param list The list to check
if (conveyorBelt.getObject() == tile) { * @param predicate The predicate to test
containsTile = true; * @param <T> The type of the list
break; * @return True if the list has at least one element passing the test of the predicate
*/
private static <T> boolean testPredicate(List<T> list, Predicate<T> predicate) {
for (T object : list) {
if (predicate.test(object)) {
return true;
} }
} }
return containsTile; return false;
} }
/** /**
* Moves robots standing on conveyor belts in the direction of the conveyor belt. * 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. * In addition, the function rotates appropriately when arriving at any non-straight conveyor belt
*
* @throws InterruptedException If disturbed during sleep
*/ */
private void moveConveyorBelts() throws InterruptedException { private void moveAllConveyorBelts() throws InterruptedException {
moveConveyorBelts(fastConveyorBelts);
moveConveyorBelts(conveyorBelts);
}
/**
* Moves all conveyor belts in the input list
* @param conveyorBelts A list of conveyor belts to move
* @throws InterruptedException If disturbed during sleep
*/
private void moveConveyorBelts(List<BoardElementContainer<Tile>> conveyorBelts) throws InterruptedException {
for (BoardElementContainer<Tile> conveyorBelt : conveyorBelts) { for (BoardElementContainer<Tile> conveyorBelt : conveyorBelts) {
if (!gameBoard.hasRobotOnPosition(conveyorBelt.getPosition())) { if (!gameBoard.hasRobotOnPosition(conveyorBelt.getPosition())) {
continue; continue;
} }
Position newPosition = gameBoard.getNewPosition(conveyorBelt.getPosition(), Position conveyorBeltPosition = conveyorBelt.getPosition();
conveyorBelt.getObject().getDirection()); Tile conveyorBeltTile = conveyorBelt.getObject();
Position newPosition = gameBoard.getNewPosition(conveyorBeltPosition, conveyorBeltTile.getDirection());
Tile nextTile = gameBoard.getTileOnPosition(newPosition); Tile nextTile = gameBoard.getTileOnPosition(newPosition);
Direction currentDirection = conveyorBelt.getObject().getDirection();
Direction currentDirection = conveyorBeltTile.getDirection();
Direction nextDirection = nextTile.getDirection(); Direction nextDirection = nextTile.getDirection();
RobotID robot = gameBoard.getRobotOnPosition(conveyorBelt.getPosition()); RobotID robot = gameBoard.getRobotOnPosition(conveyorBeltPosition);
if (listContainsTile(nextTile) && currentDirection != nextDirection) {
if (currentDirection.equals(Direction.NORTH)) { //TODO: Check whether the robot is able to move before moving. Alternatively: Save position and direction
if (nextDirection.equals(Direction.WEST)) { // of each robot and revert if a collision is found.
sleep(); sleep();
gameBoard.moveRobot(robot, currentDirection); gameBoard.moveRobot(robot, currentDirection);
sleep(); if (testPredicate(conveyorBelts, (container) -> container.getObject() == nextTile)) {
gameBoard.rotateRobotLeft(robot); if (Direction.getRightRotatedDirection(nextDirection) == currentDirection) {
} else { sleep();
sleep(); gameBoard.rotateRobotLeft(robot);
gameBoard.moveRobot(robot, currentDirection); } else if (Direction.getLeftRotatedDirection(nextDirection) == currentDirection) {
sleep(); sleep();
gameBoard.rotateRobotRight(robot); 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());
} }
} }
} }

View File

@ -2,18 +2,18 @@ TILE 4 0
HOLE 5 0 HOLE 5 0
COGWHEEL_RIGHT 5 6 COGWHEEL_RIGHT 5 6
COGWHEEL_LEFT 4 6 COGWHEEL_LEFT 4 6
TRANSPORT_BAND_SLOW 0 6 3 6 1 6 2 6 CONVEYOR_BELT_SLOW 0 6 3 6 1 6 2 6
TRANSPORT_BAND_SLOW_RIGHT 2 5 2 4 3 4 3 5 CONVEYOR_BELT_SLOW_RIGHT 2 5 2 4 3 4 3 5
TRANSPORT_BAND_SLOW_LEFT 1 5 0 5 0 4 1 4 CONVEYOR_BELT_SLOW_LEFT 1 5 0 5 0 4 1 4
TRANSPORT_BAND_SLOW_SIDE_ENTRANCES 4 8 4 7 5 7 5 8 CONVEYOR_BELT_SLOW_SIDE_ENTRANCES 4 8 4 7 5 7 5 8
TRANSPORT_BAND_SLOW_SIDE_ENTRANCE_LEFT 0 7 1 7 2 7 3 7 CONVEYOR_BELT_SLOW_SIDE_ENTRANCE_LEFT 0 7 1 7 2 7 3 7
TRANSPORT_BAND_SLOW_SIDE_ENTRANCE_RIGHT 0 8 1 8 2 8 3 8 CONVEYOR_BELT_SLOW_SIDE_ENTRANCE_RIGHT 0 8 1 8 2 8 3 8
TRANSPORT_BAND_FAST 4 1 5 1 4 2 5 2 CONVEYOR_BELT_FAST 4 1 5 1 4 2 5 2
TRANSPORT_BAND_FAST_RIGHT 2 3 2 2 3 2 3 3 CONVEYOR_BELT_FAST_RIGHT 2 3 2 2 3 2 3 3
TRANSPORT_BAND_FAST_LEFT 1 3 0 3 0 2 1 2 CONVEYOR_BELT_FAST_LEFT 1 3 0 3 0 2 1 2
TRANSPORT_BAND_FAST_SIDE_ENTRANCES 3 10 0 10 1 10 2 10 CONVEYOR_BELT_FAST_SIDE_ENTRANCES 3 10 0 10 1 10 2 10
TRANSPORT_BAND_FAST_SIDE_ENTRANCE_RIGHT 4 9 5 9 5 10 4 10 CONVEYOR_BELT_FAST_SIDE_ENTRANCE_RIGHT 4 9 5 9 5 10 4 10
TRANSPORT_BAND_FAST_SIDE_ENTRANCE_LEFT 0 9 1 9 2 9 3 9 CONVEYOR_BELT_FAST_SIDE_ENTRANCE_LEFT 0 9 1 9 2 9 3 9
FLAG_1 6 6 FLAG_1 6 6
FLAG_2 6 7 FLAG_2 6 7
FLAG_3 6 8 FLAG_3 6 8

View File

@ -40,6 +40,6 @@ public class TileTest {
@Test (expected = IllegalArgumentException.class) @Test (expected = IllegalArgumentException.class)
public void invalidTileThrowsException() { public void invalidTileThrowsException() {
new Tile(TileType.TRANSPORT_BAND_FAST, Direction.NORTH_EAST); new Tile(TileType.CONVEYOR_BELT_FAST, Direction.NORTH_EAST);
} }
} }

View File

@ -26,7 +26,7 @@ public class TextureConverterUtilTest {
public void setUp() { public void setUp() {
tileNorth = new Tile(TileType.TILE, Direction.NORTH); tileNorth = new Tile(TileType.TILE, Direction.NORTH);
holeNorth = new Tile(TileType.HOLE, Direction.NORTH); holeNorth = new Tile(TileType.HOLE, Direction.NORTH);
transportBandSlowEast = new Tile(TileType.TRANSPORT_BAND_SLOW, Direction.EAST); transportBandSlowEast = new Tile(TileType.CONVEYOR_BELT_SLOW, Direction.EAST);
tileTextureRegion = TextureConverterUtil.convertElement(tileNorth); tileTextureRegion = TextureConverterUtil.convertElement(tileNorth);
holeTextureRegion = TextureConverterUtil.convertElement(holeNorth); holeTextureRegion = TextureConverterUtil.convertElement(holeNorth);
transportBandSlowEastTextureRegion = TextureConverterUtil.convertElement(transportBandSlowEast); transportBandSlowEastTextureRegion = TextureConverterUtil.convertElement(transportBandSlowEast);