perudox v0.1.0 Perudox.Player.State

This module defines state for a player and facilitates interactions with it.

Summary

Functions

Removes a dice from a player’s hand. If their last dice is being taken, the state is also maked as game over

Counts occurences of each value in a hand

Rolls the hand in the given state

Adds a dice to a hand, unless that player is out or his hand is maxed out

Types

count()
count() :: non_neg_integer
dice()
dice() :: integer
hand()
hand() :: [dice]
occurences()
occurences() :: %{optional(1) => count, optional(2) => count, optional(3) => count, optional(4) => count, optional(5) => count, optional(6) => count}
t()
t() :: %Perudox.Player.State{game_over: boolean, hand: hand, nickname: String.t}

Functions

lose_a_dice(state)
lose_a_dice(Perudox.Player.State.t) ::
  Perudox.Player.State.t |
  {:error, atom}

Removes a dice from a player’s hand. If their last dice is being taken, the state is also maked as game over.

Examples

iex> hand = [1, 2, 3, 4]
  iex> state = %Perudox.Player.State{hand: hand}
  iex> %{hand: new_hand} = Perudox.Player.State.lose_a_dice state
  iex> hand != new_hand && length(hand) == length(new_hand) + 1
  true
occurences(map)

Counts occurences of each value in a hand.

Examples

iex> hand = [1, 2, 2, 3, 3, 3]
  iex> state = %Perudox.Player.State{hand: hand}
  iex> Perudox.Player.State.occurences state
  %{1 => 1, 2 => 2, 3 => 3, 4 => 0, 5 => 0, 6 => 0}

Rolls the hand in the given state.

Examples

iex> hand = [5, 6, 7]
  iex> state = %Perudox.Player.State{hand: hand}
  iex> %{hand: new_hand} = Perudox.Player.State.roll state
  iex> hand != new_hand && length(hand) == length(new_hand)
  true
win_a_dice(state)
win_a_dice(Perudox.Player.State.t) ::
  Perudox.Player.State.t |
  {:error, atom}

Adds a dice to a hand, unless that player is out or his hand is maxed out.

Examples

iex> hand = [1, 2, 3, 4]
  iex> state = %Perudox.Player.State{hand: hand}
  iex> %{hand: new_hand} = Perudox.Player.State.win_a_dice state
  iex> hand != new_hand && length(hand) == length(new_hand) - 1
  true