Class WorldMap

java.lang.Object
za.co.wethinkcode.robots.server.world.WorldMap

public class WorldMap extends Object
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 Details

    • WorldMap

      public WorldMap(int width, int height, MazeGenerator maze, List<Position> obstacles, Supplier<Collection<Robot>> robots)
      Parameters:
      width - width of the world, in steps
      height - height of the world, in steps
      maze - the maze that knows which cells are walls
      obstacles - the list of obstacle positions in the world
      robots - 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

      public boolean isValidPosition(Position p)
      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

      public Robot getRobotAt(int x, int y)
    • findRandomValidPosition

      public Optional<Position> findRandomValidPosition(Random random, int attempts)
      Searches for a random position that is valid for a robot to occupy
      Parameters:
      random - source of randomness for picking candidate positions
      attempts - how many random positions to try before giving up
      Returns:
      a valid random Position or Optional empty() if none was found within attempts tries