Tabletop (tabletop v0.1.0)

Tabletop contains functions for playing a board game and querying the state of the game.

Taking Turns

Taking a turn involves calling the take_turn/2 function which follows these steps:

  1. Apply provided actions - e.g. add a piece, move a piece
  2. Apply board effects - e.g. check if a player has won
  3. Increment the turn counter

To find out more about actions or effects, check the Tabletop.Actions or Tabletop.Board modules respectively.

Link to this section Summary

Functions

Returns the piece on the board at position.

Checks if the provided position is within the bounds of the board.

Checks if the provided position on the board is occupied by a piece.

Applies the provided actions to the board and then advances to the next turn. Actions consist of a combination of the following

Link to this section Functions

Link to this function

get_piece(board, position)

Specs

get_piece(atom() | %{:pieces => map(), optional(any()) => any()}, any()) ::
  any()

Returns the piece on the board at position.

Examples

iex> %Tabletop.Board{pieces: %{1 => %Tabletop.Piece{id: "Rook"}}}
iex>   |> Tabletop.get_piece(1)
%Tabletop.Piece{attributes: %{}, id: "Rook"}

iex> Tabletop.Board.square(3)
iex>   |> Tabletop.get_piece({0, 0})
nil
Link to this function

in_bounds?(board, position)

Checks if the provided position is within the bounds of the board.

Examples

iex> Tabletop.Board.square(3)
iex>   |> Tabletop.in_bounds?({0, 0})
true

iex> Tabletop.Board.square(3)
iex>   |> Tabletop.in_bounds?({99, 99})
false
Link to this function

occupied?(board, position)

Checks if the provided position on the board is occupied by a piece.

Examples

iex> board = %Tabletop.Board{pieces: %{1 => %Tabletop.Piece{id: "Rook"}}}
iex> Tabletop.occupied?(board, 1)
true
Link to this function

take_turn(board, actions)

Applies the provided actions to the board and then advances to the next turn. Actions consist of a combination of the following:

  • :move => {from, to}
  • :add => {piece, position}
  • :remove => position
  • :assign => {position, attributes}

For example, a Chess turn might see a single :move action or two :move actions in the case of castling.

Examples

iex> %Tabletop.Board{}
iex>   |> Tabletop.take_turn(%{})
%Tabletop.Board{turn: 2}

iex> Tabletop.Board.square(3)
iex>   |> Tabletop.take_turn(add: {%Tabletop.Piece{id: "Rook"}, {0, 0}})
iex>   |> Tabletop.take_turn(move: {{0, 0}, {0, 1}})
iex>   |> Tabletop.get_piece({0, 1})
%Tabletop.Piece{id: "Rook"}