View Source Briscola.Game (Briscola v0.1.1)

Briscola.Game module implements a struct that represents a game state, and functions to manipulate the game state according to the stages of the game.

Summary

Types

Options for creating a new game. Players is the number of players, can be 2 or 4. Goes first is the index of the player who goes first (zero indexed)

Possible errors for the play/2 function.

t()

Functions

Check if the game is over, i.e. the deck is empty and all players have no cards.

Retrieve the lead suit of the current trick. If there are no cards in the trick, return nil. Otherwise, return the suit of the first card played in the trick.

Return players in order of highest score to lowest.

Check if we should redeal, i.e. the trick has been scored, players need cards, and the deck has enough cards to deal to each player.

Create a new game of Briscola.

Play a card for the current player. Returns an error if the trick is over, or the card is not valid. Cards can be played by the index of the player's hand or by a Card struct.

Redistribute one card to each player, call this after scoring a trick.

Score the current trick, moving the cards to the winning player's pile. Also clears the trick and sets the action to the winning player.

Check if the trick is over, i.e. all players have played a card.

Retrieve the trump suit of the current game (the suit of the briscola card).

Types

new_game_options()

@type new_game_options() :: [players: 2 | 4, goes_first: non_neg_integer()]

Options for creating a new game. Players is the number of players, can be 2 or 4. Goes first is the index of the player who goes first (zero indexed)

play_error()

@type play_error() :: :trick_over | :must_redeal | :invalid_card

Possible errors for the play/2 function.

t()

@type t() :: %Briscola.Game{
  action_on: integer(),
  briscola: Briscola.Card.t(),
  deck: Briscola.Deck.t(),
  players: [Briscola.Player.t()],
  trick: [Briscola.Card.t()]
}

Functions

game_over?(game)

@spec game_over?(t()) :: boolean()

Check if the game is over, i.e. the deck is empty and all players have no cards.

lead_suit(game)

@spec lead_suit(t()) :: Briscola.Card.suit() | nil

Retrieve the lead suit of the current trick. If there are no cards in the trick, return nil. Otherwise, return the suit of the first card played in the trick.

leaders(game)

@spec leaders(t()) :: [Briscola.Player.t()]

Return players in order of highest score to lowest.

needs_redeal?(game)

@spec needs_redeal?(t()) :: boolean()

Check if we should redeal, i.e. the trick has been scored, players need cards, and the deck has enough cards to deal to each player.

new(opts \\ [])

@spec new(new_game_options()) :: t()

Create a new game of Briscola.

play(game, card)

@spec play(t(), Briscola.Card.t() | non_neg_integer()) ::
  {:ok, t()} | {:error, play_error()}

Play a card for the current player. Returns an error if the trick is over, or the card is not valid. Cards can be played by the index of the player's hand or by a Card struct.

redeal(game)

@spec redeal(t()) :: t() | {:error, :trick_not_scored | :not_enough_cards}

Redistribute one card to each player, call this after scoring a trick.

Since the last few turns of the game don't have enough cards to deal to all players, it's normal to get back {:error, :not_enough_cards} on the last few turns.

score_trick(game)

@spec score_trick(t()) :: {:ok, t(), non_neg_integer()} | {:error, :trick_not_over}

Score the current trick, moving the cards to the winning player's pile. Also clears the trick and sets the action to the winning player.

should_score_trick?(game)

@spec should_score_trick?(t()) :: boolean()

Check if the trick is over, i.e. all players have played a card.

trump_suit(game)

@spec trump_suit(t()) :: Briscola.Card.suit()

Retrieve the trump suit of the current game (the suit of the briscola card).