defmodule MarsExplorer.HighlandGrid do @moduledoc """ Module responsible for managing a highland grid """ defstruct [:north_limit, :east_limit] @type t :: %__MODULE__{ north_limit: integer(), east_limit: integer() } @doc """ Generates the highland grid ## Examples iex> alias MarsExplorer.HighlandGrid MarsExplorer.HighlandGrid iex> HighlandGrid.build(%{north: 5, east: 5}) %HighlandGrid{north_limit: 5, east_limit: 5} """ @spec build(%{north: integer(), east: integer()}) :: t() def build(%{north: north, east: east}), do: %__MODULE__{north_limit: north, east_limit: east} @doc """ Determines if a position would be within the highland grid's limits ## Examples iex> alias MarsExplorer.HighlandGrid MarsExplorer.HighlandGrid iex> grid = %HighlandGrid{north_limit: 5, east_limit: 5} %HighlandGrid{north_limit: 5, east_limit: 5} iex> grid |> HighlandGrid.valid_position?(%{north: 0, east: 0}) true iex> grid |> HighlandGrid.valid_position?(%{north: 5, east: 5}) true iex> grid |> HighlandGrid.valid_position?(%{north: 0, east: 6}) false iex> grid |> HighlandGrid.valid_position?(%{north: 6, east: 0}) false """ @spec valid_position?(t(), %{north: integer(), east: integer()}) :: boolean() def valid_position?( %__MODULE__{north_limit: north_limit, east_limit: east_limit}, %{north: north, east: east} ) do north >= 0 && north <= north_limit && east >= 0 && east <= east_limit end end