Class WorldMap
java.lang.Object
za.co.wethinkcode.robots.server.world.WorldMap
WorldMap answers questions about a single location in the world: is it in
bounds, is it a wall, is it an obstacle, is a robot standing there, and is
it therefore a valid place for a robot to be.
This logic used to live directly on World as isValidPosition
and its supporting methods.
-
Constructor Summary
ConstructorsConstructorDescriptionWorldMap(SavedWorld savedWorld) Create a new worldMap from a previously saved world -
Method Summary
Modifier and TypeMethodDescriptionvoidaddObstacleToMap(Obstacle obstacle, Position position) Add aObstaclesomewhere on the mapvoidFind the spawn of a robot, if viable, and addvoidaddRobotToMap(Robot robot, Position position) findRandomValidPosition(Random random, int attempts) Searches for a random position that is valid for a robot to occupyorg.apache.commons.collections4.bidimap.DualHashBidiMap<Position, WorldObject> org.apache.commons.collections4.bidimap.DualHashBidiMap<Position, WorldObject> Given aWorldObjectreturn thePositiontheregetRobotAt(int x, int y) getSymbolFromPosition(Position position, String fillerSymbol) getWorldObjectFromPosition(Position position) Given aPositionreturn the world Object that is found therebooleanisObstacleAt(int x, int y) booleanbooleanbooleanisWallAt(int x, int y) A position out of bounds counts as a wall so that callers don't need to separately bounds-check before asking about walls.booleanvoidUtility method to remove all obstaclesvoidremoveRobotFromMap(Robot robot) safelyMoveRobot(Robot robot, Position newPosition) voidsetWhereEverythingIs(org.apache.commons.collections4.bidimap.DualHashBidiMap map) voidupdateRobotPositionOnMap(Robot robot, Position nextPosition)
-
Constructor Details
-
WorldMap
public WorldMap(int width, int height, List<Position> obstacles, Supplier<Collection<Robot>> robots) - Parameters:
width- width of the world, in stepsheight- height of the world, in stepsobstacles- the list of obstacle positions in the worldrobots- supplies the current collection of robots in the world. A Supplier is used (rather than a plain Collection) so this class always sees the up-to-date set of robots, even though robots come and go.
-
WorldMap
Create a new worldMap from a previously saved world
-
-
Method Details
-
isValidPosition
-
isValidSpawn
-
isWallAt
public boolean isWallAt(int x, int y) A position out of bounds counts as a wall so that callers don't need to separately bounds-check before asking about walls. -
isWallAt
-
isObstacleAt
public boolean isObstacleAt(int x, int y) - Parameters:
x- x coord of positiony- y coord of position- Returns:
- if there is an obstacle there
-
getRobotAt
- Parameters:
x- coord of the roboty- coord of the robot- Returns:
- the
Robotat thatPosition - Throws:
MissingWorldObjectException- thrown if a robot is not found at this position
-
findRandomValidPosition
Searches for a random position that is valid for a robot to occupy- Parameters:
random- source of randomness for picking candidate positionsattempts- how many random positions to try before giving up- Returns:
- a valid random Position or Optional empty() if none was found within attempts tries
-
getWorldObjectFromPosition
Given aPositionreturn the world Object that is found there- Parameters:
position- the position to search for- Returns:
- the
WorldObjectfound there - Throws:
MissingWorldObjectException
-
getPositionFromWorldObject
Given aWorldObjectreturn thePositionthere- Parameters:
obj- the WorldObject to search For- Returns:
- the
Positionof theWorldObject - Throws:
MissingWorldObjectException
-
addRobotToMap
public void addRobotToMap(Robot robot, Position position) throws ObstructionException, TooManyOfYouException Add aRobotto theWorldMapat a givenPositionPlease note that this is a distinct method from
updateRobotPositionOnMap(Robot, Position)- Parameters:
robot- the robot being added to the mapposition- where theRobotis being added- Throws:
MissingWorldObjectExceptionObstructionExceptionTooManyOfYouException
-
addObstacleToMap
Add aObstaclesomewhere on the map- Parameters:
obstacle- the obstacle being addedposition- where its being added
-
getSymbolFromPosition
-
updateRobotPositionOnMap
public void updateRobotPositionOnMap(Robot robot, Position nextPosition) throws MissingWorldObjectException - Parameters:
robot- theRobotbeing movednextPosition- thePositionthat the robot is being moved to.- Throws:
MissingWorldObjectException- ifRobotis not currently in the world}
-
removeRobotFromMap
- Parameters:
robot- theRobotbeing removed- Throws:
MissingWorldObjectException- if theRobotis not found
-
safelyMoveRobot
public Robot safelyMoveRobot(Robot robot, Position newPosition) throws MissingWorldObjectException, BeyondBoundryException - Parameters:
robot- theRobotbeing movednewPosition- where theRobotis being moved to- Returns:
Robotwith it's changed state- Throws:
MissingWorldObjectExceptionBeyondBoundryException
-
getAllWorldObjects
public org.apache.commons.collections4.bidimap.DualHashBidiMap<Position,WorldObject> getAllWorldObjects() -
setWhereEverythingIs
public void setWhereEverythingIs(org.apache.commons.collections4.bidimap.DualHashBidiMap map) -
addRobot
Find the spawn of a robot, if viable, and add -
getRandomSpawn
-
getAllObstacles
public org.apache.commons.collections4.bidimap.DualHashBidiMap<Position,WorldObject> getAllObstacles() -
removeAllObstacles
public void removeAllObstacles()Utility method to remove all obstacles
-