MarsExplorer.Exploration (SWAP - Mars Explorer v0.1.0) View Source

Module responsible for managing a exploration

Link to this section Summary

Functions

Moves the probe forward one unit in the direction that it is facing, unless that position is past the limits of the grid.

Places a probe on a grid

Returns the probe's current position.

Turns the probe left.

Turns the probe left.

Link to this section Types

Specs

placement() :: %{
  north: integer(),
  east: integer(),
  direction: :west | :north | :south | :east
}

Specs

t() :: %MarsExplorer.Exploration{
  grid: MarsExplorer.HighlandGrid.t(),
  probe: MarsExplorer.Probe.t()
}

Link to this section Functions

Specs

move(t()) :: {:ok, t()} | {:error, :at_grid_limit}

Moves the probe forward one unit in the direction that it is facing, unless that position is past the limits of the grid.

Examples

A valid movement

iex> alias MarsExplorer.{Probe, HighlandGrid, Exploration}
[MarsExplorer.Probe, MarsExplorer.HighlandGrid, MarsExplorer.Exploration]
iex> grid = HighlandGrid.build(%{north: 5, east: 5})
%HighlandGrid{north_limit: 5, east_limit: 5}
iex> exploration = %Exploration{
...> grid: grid,
...> probe: %Probe{north: 0, east: 0, direction: :north}
...> }
iex> exploration |> Exploration.move
{:ok, %Exploration{
  grid: grid,
  probe: %Probe{north: 1, east: 0, direction: :north}
}}

An invalid movement:

iex> alias MarsExplorer.{Probe, HighlandGrid, Exploration}
[MarsExplorer.Probe, MarsExplorer.HighlandGrid, MarsExplorer.Exploration]
iex> grid = HighlandGrid.build(%{north: 5, east: 5})
%HighlandGrid{north_limit: 5, east_limit: 5}
iex> exploration = %Exploration{
...> grid: grid,
...> probe: %Probe{north: 5, east: 0, direction: :north}
...> }
iex> exploration |> Exploration.move()
{:error, :at_grid_limit}

Specs

place(MarsExplorer.HighlandGrid.t(), placement()) ::
  {:ok, t()} | {:error, :invalid_placement}

Places a probe on a grid

Examples

When the probe is placed in a valid position

iex> alias MarsExplorer.{Probe, Exploration, HighlandGrid}
[MarsExplorer.Probe, MarsExplorer.Exploration, MarsExplorer.HighlandGrid]
iex> grid = HighlandGrid.build(%{north: 5, east: 5})
%HighlandGrid{north_limit: 5, east_limit: 5}
iex> Exploration.place(grid, %{north: 0, east: 0, direction: :north})
{
  :ok,
  %Exploration{
    grid: grid,
    probe: %Probe{north: 0, east: 0, direction: :north}
  }
}

When the probe is placed in a invalid position

iex> alias MarsExplorer.{Exploration, HighlandGrid}
[MarsExplorer.Exploration, MarsExplorer.HighlandGrid]
iex> grid = HighlandGrid.build(%{north: 5, east: 5})
%HighlandGrid{north_limit: 5, east_limit: 5}
iex> Exploration.place(grid, %{north: 6, east: 6, direction: :north})
{:error, :invalid_placement}

Specs

report(t()) :: {:ok, MarsExplorer.Probe.t()}

Returns the probe's current position.

Examples

iex> alias MarsExplorer.{Probe, HighlandGrid, Exploration}
[MarsExplorer.Probe, MarsExplorer.HighlandGrid, MarsExplorer.Exploration]
iex> grid = HighlandGrid.build(%{north: 5, east: 5})
%HighlandGrid{north_limit: 5, east_limit: 5}
iex> exploration = %Exploration{
...> grid: grid,
...> probe: %Probe{north: 0, east: 0, direction: :north}
...> }
iex> exploration |> Exploration.report
%Probe{north: 0, east: 0, direction: :north}

Specs

turn_left(t()) :: {:ok, t()}

Turns the probe left.

Examples

iex> alias MarsExplorer.{Probe, HighlandGrid, Exploration}
[MarsExplorer.Probe, MarsExplorer.HighlandGrid, MarsExplorer.Exploration]
iex> grid = HighlandGrid.build(%{north: 5, east: 5})
%HighlandGrid{north_limit: 5, east_limit: 5}
iex> exploration = %Exploration{
...> grid: grid,
...> probe: %Probe{north: 0, east: 0, direction: :north}
...> }
iex> exploration |> Exploration.turn_left
{:ok, %Exploration{
  grid: grid,
  probe: %Probe{north: 0, east: 0, direction: :west}
}}

Specs

turn_right(t()) :: {:ok, t()}

Turns the probe left.

Examples

iex> alias MarsExplorer.{Probe, HighlandGrid, Exploration}
[MarsExplorer.Probe, MarsExplorer.HighlandGrid, MarsExplorer.Exploration]
iex> grid = HighlandGrid.build(%{north: 5, east: 5})
%HighlandGrid{north_limit: 5, east_limit: 5}
iex> exploration = %Exploration{
...> grid: grid,
...> probe: %Probe{north: 0, east: 0, direction: :north}
...> }
iex> exploration |> Exploration.turn_right
{:ok, %Exploration{
  grid: grid,
  probe: %Probe{north: 0, east: 0, direction: :east}
}}