A minimal, format-agnostic library for representing positions in two-player, turn-based board games.
Qi models the four components of a position as defined by the
Sashité Game Protocol:
- Board — a flat tuple in row-major order (1D, 2D, or 3D)
where each element is either empty (
nil) or occupied by a piece (String.t()). - Hands —
%{String.t() => pos_integer()}maps of held pieces for each player. - Styles — one style
String.t()per player side. - Turn — which player is active (
:firstor:second).
Pieces and styles must be strings. Non-string values are rejected at the boundary.
Construction
Qi.new/2 creates a position with an empty board, empty hands, and
the turn set to :first:
pos = Qi.new([8, 8], first_player_style: "C", second_player_style: "c")Transformations
All transformation functions return a new %Qi{} struct. The original
is never modified. Transformations compose naturally with the pipe
operator:
pos2 =
pos
|> Qi.board_diff([{12, nil}, {28, "P"}])
|> Qi.first_player_hand_diff([{"p", 1}])
|> Qi.toggle()Constraints
| Constraint | Value | Rationale |
|---|---|---|
| Max dimensions | 3 | Covers 1D, 2D, 3D boards |
| Max dimension size | 255 | Fits in 8-bit integer; covers 255×255×255 |
| Max square count | 65025 | Caps memory allocation (255² = 65025) |
| Max piece bytesize | 255 | Bounds piece string memory |
| Max style bytesize | 255 | Bounds style string memory |
| Board non-empty | n ≥ 1 | A board must contain at least one square |
| Piece cardinality | p ≤ n | Pieces cannot exceed the number of squares |
Summary
Functions
Returns a new position with modified squares on the board.
Returns a new position with the first player's hand modified.
Maximum size of any single dimension.
Maximum number of board dimensions.
Maximum bytesize of a piece string.
Maximum total number of squares on a board.
Maximum bytesize of a style string.
Creates a position with an empty board.
Returns a new position with the second player's hand modified.
Converts the flat board tuple into a nested list matching the shape.
Returns a new position with the active player swapped.
Types
Functions
@spec board_diff(t(), [{non_neg_integer(), String.t() | nil}]) :: t()
Returns a new position with modified squares on the board.
Accepts a list of {flat_index, piece} tuples where each flat index
is a 0-based integer in row-major order, and each piece is a string
(at most 255 bytes) or nil (empty square).
Examples
iex> pos = Qi.new([4], first_player_style: "C", second_player_style: "c")
iex> pos2 = Qi.board_diff(pos, [{0, "K"}, {3, "k"}])
iex> elem(pos2.board, 0)
"K"
iex> elem(pos2.board, 3)
"k"
iex> pos = Qi.new([2], first_player_style: "C", second_player_style: "c")
iex> pos = Qi.board_diff(pos, [{0, "a"}, {1, "b"}])
iex> Qi.first_player_hand_diff(pos, [{"c", 1}])
** (ArgumentError) too many pieces for board size (3 pieces, 2 squares)
Returns a new position with the first player's hand modified.
Accepts a list of {piece, delta} tuples where each piece is a
string (at most 255 bytes) and each delta is an integer (positive
to add, negative to remove, zero is a no-op).
Examples
iex> pos = Qi.new([4], first_player_style: "C", second_player_style: "c")
iex> pos2 = Qi.first_player_hand_diff(pos, [{"P", 2}, {"B", 1}])
iex> pos2.first_player_hand
%{"P" => 2, "B" => 1}
@spec max_dimension_size() :: pos_integer()
Maximum size of any single dimension.
@spec max_dimensions() :: pos_integer()
Maximum number of board dimensions.
@spec max_piece_bytesize() :: pos_integer()
Maximum bytesize of a piece string.
@spec max_square_count() :: pos_integer()
Maximum total number of squares on a board.
@spec max_style_bytesize() :: pos_integer()
Maximum bytesize of a style string.
@spec new([pos_integer()], first_player_style: String.t(), second_player_style: String.t() ) :: t()
Creates a position with an empty board.
The board starts with all squares empty (nil), both hands start
empty, and the turn defaults to :first.
Validation order is guaranteed: shape (dimensions, then total square count), then styles (first, then second; nil, then type, then bytesize). When multiple errors exist, the first failing check determines the error.
Parameters
shape— a list of 1 to 3 integer dimension sizes (each 1–255). The total number of squares must not exceed 65025.:first_player_style— style for the first player (non-nil string, at most 255 bytes).:second_player_style— style for the second player (non-nil string, at most 255 bytes).
Examples
iex> pos = Qi.new([8, 8], first_player_style: "C", second_player_style: "c")
iex> pos.turn
:first
iex> pos.shape
[8, 8]
iex> pos.first_player_style
"C"
iex> Qi.new([], first_player_style: "C", second_player_style: "c")
** (ArgumentError) at least one dimension is required
iex> Qi.new([8, 8], first_player_style: nil, second_player_style: "c")
** (ArgumentError) first player style must not be nil
Returns a new position with the second player's hand modified.
Accepts a list of {piece, delta} tuples where each piece is a
string (at most 255 bytes) and each delta is an integer (positive
to add, negative to remove, zero is a no-op).
Examples
iex> pos = Qi.new([4], first_player_style: "C", second_player_style: "c")
iex> pos2 = Qi.second_player_hand_diff(pos, [{"p", 1}])
iex> pos2.second_player_hand
%{"p" => 1}
Converts the flat board tuple into a nested list matching the shape.
This is an O(n) operation intended for display or serialization, not for the hot path.
Examples
iex> pos = Qi.new([2, 3], first_player_style: "C", second_player_style: "c")
iex> pos = Qi.board_diff(pos, [{0, "a"}, {5, "b"}])
iex> Qi.to_nested(pos)
[["a", nil, nil], [nil, nil, "b"]]
Returns a new position with the active player swapped.
All other fields are preserved unchanged.
Examples
iex> pos = Qi.new([8, 8], first_player_style: "C", second_player_style: "c")
iex> pos.turn
:first
iex> Qi.toggle(pos).turn
:second
iex> pos |> Qi.toggle() |> Qi.toggle() |> Map.get(:turn)
:first