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(int width, int height, MazeGenerator maze, List<Position> obstacles, Supplier<Collection<Robot>> robots) -
Method Summary
Modifier and TypeMethodDescriptionfindRandomValidPosition(Random random, int attempts) Searches for a random position that is valid for a robot to occupygetRobotAt(int x, int y) booleanisObstacleAt(int x, int y) booleanA position is valid for a robot to occupy if it is within the bounds of the world, is not a wall, is not an obstacle, and is not already occupied by another robot.booleanisWallAt(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.
-
Constructor Details
-
WorldMap
public WorldMap(int width, int height, MazeGenerator maze, List<Position> obstacles, Supplier<Collection<Robot>> robots) - Parameters:
width- width of the world, in stepsheight- height of the world, in stepsmaze- the maze that knows which cells are wallsobstacles- 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.
-
-
Method Details
-
isValidPosition
A position is valid for a robot to occupy if it is within the bounds of the world, is not a wall, is not an obstacle, and is not already occupied by another robot. -
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. -
isObstacleAt
public boolean isObstacleAt(int x, int y) - Parameters:
x-y-- Returns:
-
getRobotAt
-
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
-