View Source Perudex.Game (Perudex v0.6.0)

Provides functions to manipulate a game of Perudex.

Link to this section Summary

Functions

Play a Perudo move on the current game.

Initialize a game of Perudo with players_ids and specified max_dice a player can hold.

Link to this section Types

@type bid() :: {:count, :die}
@type instruction() :: {:notify_player, player_id(), player_instruction()}
@type move() :: {:outbid, bid()} | :calza | :dudo
@type move_result() :: {:outbid, bid()} | {:calza, boolean()} | {:dudo, boolean()}
@type player_id() :: any()
@type player_instruction() ::
  {:move, Perudex.Hand.t()}
  | {:reveal_players_hands, [{player_id(), Perudex.Hand.t()}],
     {integer(), integer()}}
  | {:last_move, player_id(), move_result()}
  | :unauthorized_move
  | :invalid_bid
  | :illegal_move
  | {:new_hand, Perudex.Hand.t()}
  | {:winner, player_id()}
  | {:loser, player_id()}
  | {:game_started, [player_id()]}
@opaque t()

Link to this section Functions

Link to this function

play_move(game, player_id, move)

View Source
@spec play_move(t(), player_id(), move()) :: {[instruction()], t()}

Play a Perudo move on the current game.

A move can either be an outbid, a calza (exactly the same amount of dice as the previous bid) or a dudo (bid is too ambitious).

examples

Examples

iex> Perudex.Game.play_move(
...> %Perudex.Game{
...>    all_players: [1, 2],
...>    current_bid: {2, 3},
...>    current_player_id: 2,
...>    instructions: [],
...>    max_dice: 5,
...>    players_hands: [
...>      %{
...>        hand: %Perudex.Hand{dice: [2, 4, 2, 5, 6], remaining_dice: 5},
...>        player_id: 1
...>      },
...>      %{
...>        hand: %Perudex.Hand{dice: [1, 3, 4, 4, 5], remaining_dice: 5},
...>        player_id: 2
...>      }
...>    ],
...>    remaining_players: [1, 2]
...>  },
...>  1,
...>  {:outbid, {2, 3}})

{[
  {:notify_player, 1, {:last_move, 1, {:outbid, {2, 3}}}},
  {:notify_player, 2, {:last_move, 1, {:outbid, {2, 3}}}},
  {:notify_player, 2, :move}
],
%Perudex.Game{
  all_players: [1, 2],
  current_bid: {2, 3},
  current_player_id: 2,
  instructions: [],
  max_dice: 5,
  players_hands: [
    %{
      hand: %Perudex.Hand{dice: [2, 4, 2, 5, 6], remaining_dice: 5},
      player_id: 1
    },
    %{
      hand: %Perudex.Hand{dice: [1, 3, 4, 4, 5], remaining_dice: 5},
      player_id: 2
    }
  ],
  remaining_players: [1, 2]
}}
Link to this function

start(player_ids, max_dice)

View Source
@spec start([player_id()], integer()) :: {[player_instruction()], t()}

Initialize a game of Perudo with players_ids and specified max_dice a player can hold.

Returns a tuple containing a list of Perudex.Game.player_instruction() and a Perudex.Game struct.

examples

Examples

iex>
:rand.seed(:exsplus, {101, 102, 103})
Perudex.Game.start([1, 2], 5)
{[
  {:notify_player, 1, {:game_started, [1, 2]}},
  {:notify_player, 2, {:game_started, [1, 2]}},
  {:notify_player, 1, {:new_hand, %Perudex.Hand{dice: [5, 5, 2, 6, 4], remaining_dice: 5}}},
  {:notify_player, 2, {:new_hand, %Perudex.Hand{dice: [1, 3, 6, 4, 2], remaining_dice: 5}}},
  {:notify_player, 1, {:move, %Perudex.Hand{dice: [5, 5, 2, 6, 4]}}
],
%Perudex.Game{
  all_players: [1, 2],
  current_bid: {0, 0},
  current_player_id: 1,
  instructions: [],
  max_dice: 5,
  players_hands: [
    %{
      hand: %Perudex.Hand{dice: [5, 5, 2, 6, 4], remaining_dice: 5},
      player_id: 1
    },
    %{
      hand: %Perudex.Hand{dice: [1, 3, 6, 4, 2], remaining_dice: 5},
      player_id: 2
    }
  ],
  remaining_players: [1, 2]
}}