Tabletop.Actions (tabletop v1.1.2)

This module provides the fundamental actions involved in any tabletop game. Each action consists of a key and then an arguments value:

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

A game will take a combination of these actions which will then form a turn. For more information see the Tabletop.take_turn/2 function.

Link to this section Summary

Functions

Applies a particular action to the board. The type of action is determined by the atom provided with arg2. This will then be mapped to a specific behaviour such as Moving or Adding a piece. If the type of action has not been implemented then the board will be returned with no changes made.

Link to this section Functions

Link to this function

apply(board, arg2, position)

Applies a particular action to the board. The type of action is determined by the atom provided with arg2. This will then be mapped to a specific behaviour such as Moving or Adding a piece. If the type of action has not been implemented then the board will be returned with no changes made.

Moving a Piece

  • arg2 => :move
  • arguments => {from, to}

Moves the piece at the from position to the to position. Any existing pieces at the to position will be removed from the board and replaced by the moving piece.

  • arg2 => :step
  • arguments => {piece_id, direction}

Moves the first piece matching piece_id from its current position and in the provided direction.

In the case that there is no piece to move, nothing will happen and the unchanged board struct will be returned.

Adding a Piece

  • arg2 => :add
  • arguments => {%Tabletop.Piece{}, position}

Adds the provided piece to the position on the board. Existing pieces will be removed from the board and replaced.

If the position is not within the bounds of the board, the unchanged board will be returned.

Removing a Piece

  • arg2 => :remove
  • arguments => position

Removes the piece at the provided position if it is occupied. Does nothing to the board if the position does not contain a piece.

Assigning Attributes to a Piece

  • arg2 => :assign
  • arguments => %{}

Assigns the provided attributes to the piece at the provided position. If it does not exist then the unchanged board will be returned.