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 to have 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.

Value 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.

See also:
trait Grid[Square]
class Object
trait Matchable
class Any

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).

Value 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.

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.

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.)

See also:

Causes the next robot to take a turn. The turn then immediately passes to the next robot.

Causes the next robot to take a turn. The turn then immediately passes to the next robot.

See also:

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.

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).

Returns the robot whose turn it is to act. 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. That is, returns the robot who will be the next one to act when advanceTurn or advanceFullRound is called.

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.

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.

(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.)

Returns:

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

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.

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.

Inherited methods

def allElements: Vector[Element]

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

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

Inherited from:
Grid

Returns a collection of all the locations on the grid.

Returns a collection of all the locations on the grid.

Inherited from:
Grid
def apply(location: GridPos): Element

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.)

Value parameters:
location

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

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).

Inherited from:
Grid
def elementAt(location: GridPos): Element

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.)

Value parameters:
location

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

Inherited from:
Grid
def neighbors(middleLoc: GridPos, includeDiagonals: Boolean): Vector[Element]

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.

Value 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

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.

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.

Value 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

Inherited from:
Grid

Inherited fields

val height: Int
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.)

Inherited from:
Grid
val width: Int
Inherited from:
Grid