CitySim Docs
Fall 2025
API

# CityMap

o1.city.CityMap
class CityMap(val `homesPerSide`: Int, `populations`: Vector[(Demographic, Int)]) extends Grid[Demographic]

A `CityMap` is a square grid that represents a simplified map of a city. It is a Grid whose elements are Demographic objects. That is, each GridPos (“street address”) on a `CityMap` is either `Vacant` or stores a household that belongs to aparticular demographic.

A `CityMap` is mutable. As a household moves, the corresponding `Demographic` object moves to a different `GridPos` on the `CityMap`.

## Parameters

homesPerSide

  the number of street addresses in each row and each column of the city grid (which is always square)

populations

  a vector of pairs in which each `Demographic` is matched with a count of how many times it appears on this city map

## Attributes

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

## Members list

### Value members

#### Concrete methods

def initialElements: Seq[Demographic]

Generates the elements that initially occupy the grid. In the case of a `CityMap` grid, this means generating new `Demographic` objects at random locations so that their distribution matches the `populations` parameter of the `CityMap`.

Generates the elements that initially occupy the grid. In the case of a `CityMap` grid, this means generating new `Demographic` objects at random locations so that their distribution matches the `populations` parameter of the `CityMap`.

## Attributes

def matches(`address`: GridPos, `householdType`: Demographic): Boolean

Determines whether the household at the given “street address” (`GridPos`) belongs to the given demographic.

Determines whether the household at the given “street address” (`GridPos`) belongs to the given demographic.

## Parameters

address

  a location on the city grid to examine

householdType

  a demographic that the household at the given address is compared to (may be `Vacant`, too, in which case this method checks if the address is vacant)

## Attributes

#### Inherited methods

def allElements: Vector[Demographic]

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): Demographic

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): Demographic

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[Demographic]

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(): Demographic

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: Demographic): 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

#### Concrete fields

val allAddresses: Vector[GridPos]

Returns all the locations in the city.

Returns all the locations in the city.

## Attributes

val homesPerSide: Int

#### 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