Islands.Board (Islands Board v0.1.44)

Copy Markdown View Source

A board struct and functions for the Game of Islands.

The board struct contains the fields:

  • islands
  • misses

representing the properties of a board in the Game of Islands.

Based on the book Functional Web Development by Lance Halvorsen.

Summary

Types

A map assigning islands to their island types

t()

A board struct for the Game of Islands

Functions

Checks if all islands have been positioned on board.

Returns a list of island types for forested islands.

Returns a map assigning the CSS grid position of each island on board to its island type.

Checks if guess has hit any island on board and returns a response tuple.

Returns a map assigning the list of hit "cells" of each island on board to its island type.

Returns board's total number of hits.

Returns a map assigning to :squares the list of square numbers from board's misses.

Returns board's total number of misses.

Returns an empty board struct.

Positions island on board and returns an updated board or {:error, :overlapping_island} if island overlaps another island.

Types

islands()

@type islands() :: %{required(Islands.Island.type()) => Islands.Island.t()}

A map assigning islands to their island types

t()

@type t() :: %Islands.Board{islands: islands(), misses: Islands.Island.coords()}

A board struct for the Game of Islands

Functions

all_islands_positioned?(board)

@spec all_islands_positioned?(t()) :: boolean()

Checks if all islands have been positioned on board.

forested_types(board)

@spec forested_types(t()) :: [Islands.Island.type()]

Returns a list of island types for forested islands.

grid_positions(board)

@spec grid_positions(t()) :: %{
  required(Islands.Island.type()) => Islands.Island.grid_position()
}

Returns a map assigning the CSS grid position of each island on board to its island type.

Examples

iex> alias Islands.{Board, Coord, Island}
iex> {:ok, dot_origin} = Coord.new(9, 9)
iex> {:ok, square_origin} = Coord.new(9, 5)
iex> {:ok, dot} = Island.new(:dot, dot_origin)
iex> {:ok, square} = Island.new(:square, square_origin)
iex> incomplete =
...>   Board.new() |>
...>   Board.position_island(square) |>
...>   Board.position_island(dot)
iex> Board.grid_positions(incomplete)
%{
  dot: %{gridColumnStart: 9, gridRowStart: 9},
  square: %{gridColumnStart: 5, gridRowStart: 9}
}

guess(board, guess)

@spec guess(t(), Islands.Coord.t()) :: Islands.Board.Response.t()

Checks if guess has hit any island on board and returns a response tuple.

hit_cells(board)

@spec hit_cells(t()) :: %{
  required(Islands.Island.type()) => [Islands.Island.grid_cell()]
}

Returns a map assigning the list of hit "cells" of each island on board to its island type.

Examples

iex> alias Islands.{Board, Coord, Island}
iex> {:ok, atoll_origin} = Coord.new(1, 1)
iex> {:ok, dot_origin} = Coord.new(9, 9)
iex> {:ok, atoll} = Island.new(:atoll, atoll_origin)
iex> {:ok, dot} = Island.new(:dot, dot_origin)
iex> {:ok, b1} = Coord.new(1, 2)
iex> {:ok, b2} = Coord.new(2, 2)
iex> {:ok, b3} = Coord.new(3, 2)
iex> {:ok, a1} = Coord.new(9, 9)
iex> incomplete =
...>   Board.new() |>
...>   Board.position_island(atoll) |>
...>   Board.position_island(dot)
iex> {:hit, :none, :no_win, incomplete} = Board.guess(incomplete, b1)
iex> {:hit, :none, :no_win, incomplete} = Board.guess(incomplete, b2)
iex> {:hit, :none, :no_win, incomplete} = Board.guess(incomplete, b3)
iex> {:hit, :dot, :no_win, incomplete} = Board.guess(incomplete, a1)
iex> Board.hit_cells(incomplete)
%{
  atoll: ["b1", "b2", "b3"],
  dot: ["a1"]
}

hits(board)

@spec hits(t()) :: non_neg_integer()

Returns board's total number of hits.

miss_squares(board)

@spec miss_squares(t()) :: %{squares: [Islands.Coord.square()]}

Returns a map assigning to :squares the list of square numbers from board's misses.

misses(board)

@spec misses(t()) :: non_neg_integer()

Returns board's total number of misses.

new()

@spec new() :: t()

Returns an empty board struct.

position_island(board, island)

@spec position_island(t(), Islands.Island.t()) :: t() | {:error, :overlapping_island}

Positions island on board and returns an updated board or {:error, :overlapping_island} if island overlaps another island.