Robots Docs
Fall 2025
API

# RobotWorld

o1.robots.RobotWorld
class RobotWorld(`floorWidth`: Int, `floorHeight`: Int) extends Grid[Square]

An instance of the class `RobotWorld` represents a mutable, two-dimensional world that can be inhabited by virtual robots. This kind of “robot world” is a `Grid` whose elements are `Square` objects.

Robots — `RobotBody` objects — can be added to the robot world, and the robot world object maintains a listing of the added robots. It uses this list so that the robots take their turns in a round-robin fashion.

Apart from robots, a robot world can also contain walls. All robot worlds are bounded by walls on all sides: all the edge squares of all robot worlds are always unpassable by robots. Wall squares may also be added at other locations within a world.

## Parameters

floorHeight

  the height of the robot world, in squares, in addition to the walls at the top and at the bottom. The total height of the grid will be two plus this number.

floorWidth

  the width of the robot world, in squares, in addition to the walls on both sides. The total width of the grid will be two plus this number.

## Attributes

See also

  Square

Graph
Supertypes
  trait Grid[Square]
  class Object
  trait Matchable
  class Any

## Members list

### Value members

#### Concrete methods

def addRobot(`initialLocation`: GridPos, `initialFacing`: CompassDir): RobotBody

Creates a new robot into this robot world. The newly created robot body does not yet have a brain.

Creates a new robot into this robot world. The newly created robot body does not yet have a brain.

This method is responsible for several related things: creating the robot (body), adding the robot to the list of robots in this world (so it will get a turn to act), and informing the robot’s initial square that the robot is now there (by calling the square’s `addRobot` method).

## Parameters

initialFacing

  the direction that the robot is initially facing in

initialLocation

  the initial location of the new robot in this world. This method assumes that `location` points to an empty square.

## Attributes

Returns

  the newly created robot body, which has been placed in the indicated square

def addWall(`location`: GridPos): Unit

Marks a square in this robot world as being an unpassable wall square. This method assumes that the location of the wall, given as a parameter, points to an empty square.

Marks a square in this robot world as being an unpassable wall square. This method assumes that the location of the wall, given as a parameter, points to an empty square.

## Attributes

def advanceFullRound(): Unit

Causes all the robots in the world to take a turn, starting with the one whose turn it is next. (After this is done, the robot who originally was next up, will be that once again.)

Causes all the robots in the world to take a turn, starting with the one whose turn it is next. (After this is done, the robot who originally was next up, will be that once again.)

## Attributes

See also

  robotWithNextTurn

  advanceTurn

  RobotBody.takeTurn

def advanceTurn(): Unit

Causes a single robot to take a turn; the turn-taking robot is the one who’s next in line (as given by `robotWithNextTurn`). This method then immediately passes the turn to the following robot. If `robotWithNextTurn` returns `None` (because the world is empty of robots), this method does nothing.

Causes a single robot to take a turn; the turn-taking robot is the one who’s next in line (as given by `robotWithNextTurn`). This method then immediately passes the turn to the following robot. If `robotWithNextTurn` returns `None` (because the world is empty of robots), this method does nothing.

## Attributes

See also

  robotWithNextTurn

  RobotBody.takeTurn

def initialElements: Seq[Square]

Generates the elements that initially occupy the grid. In the case of a `RobotWorld` grid, an element is a `Square` object — either a Floor instance or the Wall singleton. Initially, all the edge squares of the robot world are walls and the inner squares are empty `Floor` squares. This method is automatically invoked by the superclass `Grid` to initialize the grid’s contents.

Generates the elements that initially occupy the grid. In the case of a `RobotWorld` grid, an element is a `Square` object — either a Floor instance or the Wall singleton. Initially, all the edge squares of the robot world are walls and the inner squares are empty `Floor` squares. This method is automatically invoked by the superclass `Grid` to initialize the grid’s contents.

## Attributes

Returns

  a collection that contains the initial grid elements. The first element will appear at `GridPos` (0,0), the second at (1,0), and so on, filling in the first row before continuing on the second row at (0,1).

def numberOfRobots: Int

Returns the number of robots (robot bodies) that have been added to this world.

Returns the number of robots (robot bodies) that have been added to this world.

## Attributes

def robotList: Vector[RobotBody]

Returns a collection of all the robots in this robot world, in the order they were added to the world.

Returns a collection of all the robots in this robot world, in the order they were added to the world.

## Attributes

def robotWithNextTurn: Option[RobotBody]

Returns the robot whose turn it is to act next. That is, returns the robot who will be the next one to act when advanceTurn or advanceFullRound is called.

Returns the robot whose turn it is to act next. That is, returns the robot who will be the next one to act when advanceTurn or advanceFullRound is called.

Note that calling this method does not actually cause any robots to act or change the state of the robot world in any way. The method merely returns the robot whose turn it is.

The robots take turns in a round-robin fashion: the first one to act is the first one that was added, the second to act is the second one to be added, and so on. When the robot that was added last has taken its turn, it becomes the first one’s turn again.

(Clarifications: If a robot is added to the world while the last robot in the “queue” has the turn, it is perfectly possible for the new robot to get its first turn very soon, as soon as the previously added robot has acted. A newly added robot never immediately gains the turn, however, unless it is the first one to be added and therefore the only robot in the whole world.)

## Attributes

Returns

  the robot whose turn it is next, wrapped in an `Option`; `None` if there are no robots in this world

#### Inherited methods

def allElements: Vector[Square]

Returns a collection of all the elements currently in the grid.

Returns a collection of all the elements currently in the grid.

## Attributes

Inherited from:
  Grid

def allPositions: Vector[GridPos]

Returns a collection of all the locations on the grid.

Returns a collection of all the locations on the grid.

## Attributes

Inherited from:
  Grid

def apply(location: GridPos): Square

Returns the element at the given pair of coordinates. (This does the same as `elementAt`.)

Returns the element at the given pair of coordinates. (This does the same as `elementAt`.)

## Parameters

location

  a location on the grid (which must be within range or this method will fail with an error)

## Attributes

Inherited from:
  Grid

def contains(location: GridPos): Boolean

Determines whether the grid contains the given pair of coordinates. For instance, a grid with a width and height of 5 will contain (0, 0) and (4, 4) but not (-1, -1), (4, 5) or (5, 4).

Determines whether the grid contains the given pair of coordinates. For instance, a grid with a width and height of 5 will contain (0, 0) and (4, 4) but not (-1, -1), (4, 5) or (5, 4).

## Attributes

Inherited from:
  Grid

def elementAt(location: GridPos): Square

Returns the element at the given pair of coordinates. (This does the same as `apply`.)

Returns the element at the given pair of coordinates. (This does the same as `apply`.)

## Parameters

location

  a location on the grid (which must be within range or this method will fail with an error)

## Attributes

Inherited from:
  Grid

def neighbors(middleLoc: GridPos, includeDiagonals: Boolean): Vector[Square]

Returns a vector of all the neighboring elements of the element indicated by the first parameter. Depending on the second parameter, either only the four neighbors in cardinal compass directions (north, east, south, west) are considered, or the four diagonals as well.

Returns a vector of all the neighboring elements of the element indicated by the first parameter. Depending on the second parameter, either only the four neighbors in cardinal compass directions (north, east, south, west) are considered, or the four diagonals as well.

Note that an element at the grid’s edge has fewer neighbors than one in the middle. For instance, the element at (0, 0) of a 5-by-5 grid has only three neighbors, diagonals included.

## Parameters

includeDiagonals

  `true` if diagonal neighbors also count (resulting in up to eight neighbors), `false` if only cardinal directions count (resulting in up to four)

middleLoc

  the location between the neighbors

## Attributes

Inherited from:
  Grid

def randomElement(): Square

Returns a random element from the grid. Uses the scala.util.Random singleton.

Returns a random element from the grid. Uses the scala.util.Random singleton.

## Attributes

Inherited from:
  Grid

def randomPos(): GridPos

Returns a random location from the grid. Uses the scala.util.Random singleton.

Returns a random location from the grid. Uses the scala.util.Random singleton.

## Attributes

Inherited from:
  Grid

def swap(location1: GridPos, location2: GridPos): Unit

Swaps the elements at two given locations on the grid. The given locations must be within range or this method will fail with an error.

Swaps the elements at two given locations on the grid. The given locations must be within range or this method will fail with an error.

## Attributes

Inherited from:
  Grid

def update(location: GridPos, newElement: Square): Unit

Modifies the grid by replacing the existing element at the given location with the new element.

Modifies the grid by replacing the existing element at the given location with the new element.

## Parameters

location

  a location on the grid (which must be within range or this method will fail with an error)

newElement

  the new element that replaces the old one at `location`

## Attributes

Inherited from:
  Grid

#### Inherited fields

val height: Int

## Attributes

Inherited from:
  Grid

val size: Int

the number of elements in this grid, in total. (Equals `width` times `height`.)

the number of elements in this grid, in total. (Equals `width` times `height`.)

## Attributes

Inherited from:
  Grid

val width: Int

## Attributes

Inherited from:
  Grid