Sashite.Pon (Sashite.Pon v1.0.0)

Copy Markdown View Source

PON (Position Object Notation) implementation for Elixir.

PON encodes a Position of a two-player, turn-based board game as a JSON object with four fields: board, hands, styles and turn. This library converts PON documents to and from Qi positions, the model that Sashite.Feen also uses, so a position can move between the two formats.

See the PON Specification v1.0.0.

Examples

iex> {:ok, position} =
...>   Sashite.Pon.parse(~s({
...>     "board": [["k^", null], [null, "K^"]],
...>     "hands": {"first": ["P"], "second": []},
...>     "styles": {"first": "W", "second": "w"},
...>     "turn": "first"
...>   }))
iex> position.shape
[2, 2]
iex> position.first_player_hand
%{"P" => 1}
iex> Sashite.Pon.dump(position)
~s({"board":[["k^",null],[null,"K^"]],"hands":{"first":["P"],"second":[]},"styles":{"first":"W","second":"w"},"turn":"first"})

iex> Sashite.Pon.valid?(~s({"board": [], "hands": {"first": [], "second": []}, "styles": {"first": "W", "second": "w"}, "turn": "first"}))
false

Summary

Functions

Serializes a Qi position into a compact PON document.

Returns the maximum size of a PON document, in bytes.

Parses a PON document into a Qi position.

Parses a PON document into a Qi position, raising ArgumentError if it is not valid.

Reports whether the input is a valid PON document.

Functions

dump(position)

@spec dump(Qi.t()) :: String.t()

Serializes a Qi position into a compact PON document.

Hand items are written in byte order of their EPIN tokens, so that a position has a single serialization. A Qi position can hold any string, so the position is checked first: every piece, on the board or in a hand, must be an EPIN token, the first player style an uppercase SIN token and the second player style a lowercase one.

Examples

iex> Qi.new([4], first_player_style: "W", second_player_style: "w")
...> |> Qi.board_diff([{0, "K^"}, {3, "k^"}])
...> |> Qi.second_player_hand_diff([{"q", 1}, {"P", 1}])
...> |> Qi.toggle()
...> |> Sashite.Pon.dump()
~s({"board":["K^",null,null,"k^"],"hands":{"first":[],"second":["P","q"]},"styles":{"first":"W","second":"w"},"turn":"second"})

iex> Qi.new([2], first_player_style: "W", second_player_style: "w")
...> |> Qi.board_diff([{1, "W:K"}])
...> |> Sashite.Pon.dump()
** (ArgumentError) square 1 holds "W:K", which is not an EPIN token

Errors

Raises ArgumentError if the position holds a piece or a style that PON cannot encode.

max_string_length()

@spec max_string_length() :: pos_integer()

Returns the maximum size of a PON document, in bytes.

Larger inputs are rejected before they are decoded. The limit leaves room for the largest board that Qi can hold (65,025 squares) in an indented document.

Examples

iex> Sashite.Pon.max_string_length()
1_048_576

parse(input)

@spec parse(String.t()) :: {:ok, Qi.t()} | {:error, atom()}

Parses a PON document into a Qi position.

Returns {:ok, position}, or {:error, reason} where reason is an atom naming the first check that failed:

ReasonCause
:not_a_stringThe input is not a binary
:input_too_longThe input exceeds max_string_length/0 bytes
:invalid_jsonThe input is not a JSON text (RFC 8259)
:duplicate_keyAn object has two members with the same name
:not_an_objectThe root is not a JSON object
:missing_propertyboard, hands, styles or turn is missing
:unknown_propertyThe root has a property other than these four
:invalid_boardboard is not an array
:exceeds_max_dimensionsThe board has more than 3 dimensions
:dimension_size_exceeds_limitA dimension has more than 255 elements
:too_many_squaresThe board has more than 65,025 squares
:dimensional_coherence_violationThe board is not rectangular
:empty_boardThe board has no square
:invalid_handshands is not an object with the arrays first and second only
:invalid_piece_tokenA square is neither null nor an EPIN token, or a hand item is not an EPIN token
:invalid_stylesstyles is not an object with first and second only
:invalid_style_tokenstyles.first is not an uppercase SIN token, or styles.second not a lowercase one
:invalid_turnturn is neither "first" nor "second"
:too_many_piecesThere are more pieces than squares

Examples

iex> {:ok, position} =
...>   Sashite.Pon.parse(~s({
...>     "board": [null, "P", null],
...>     "hands": {"first": [], "second": ["p", "p"]},
...>     "styles": {"first": "W", "second": "w"},
...>     "turn": "second"
...>   }))
iex> position.board
{nil, "P", nil}
iex> position.second_player_hand
%{"p" => 2}
iex> position.turn
:second

iex> Sashite.Pon.parse(~s({
...>   "board": [["a", "b"], ["c"]],
...>   "hands": {"first": [], "second": []},
...>   "styles": {"first": "W", "second": "w"},
...>   "turn": "first"
...> }))
{:error, :dimensional_coherence_violation}

iex> Sashite.Pon.parse(~s({"turn": "first", "turn": "second"}))
{:error, :duplicate_key}

parse!(input)

@spec parse!(String.t()) :: Qi.t()

Parses a PON document into a Qi position, raising ArgumentError if it is not valid.

Examples

iex> position =
...>   Sashite.Pon.parse!(~s({
...>     "board": [null],
...>     "hands": {"first": [], "second": []},
...>     "styles": {"first": "C", "second": "c"},
...>     "turn": "first"
...>   }))
iex> position.shape
[1]

iex> Sashite.Pon.parse!("[]")
** (ArgumentError) invalid PON document: not_an_object

valid?(input)

@spec valid?(term()) :: boolean()

Reports whether the input is a valid PON document.

Never raises: any term other than a valid PON document returns false.

Examples

iex> Sashite.Pon.valid?(~s({
...>   "board": [[null, null], [null, null]],
...>   "hands": {"first": [], "second": []},
...>   "styles": {"first": "J", "second": "s"},
...>   "turn": "first"
...> }))
true

iex> Sashite.Pon.valid?(nil)
false