Tabletop (tabletop v0.1.2)
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:
- Apply provided actions - e.g. add a piece, move a piece
- Apply board effects - e.g. check if a player has won
- 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
get_piece(board, position)
Specs
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
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
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
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"}