defmodule Cards do @moduledoc """ Provides methods for creating and handleing a deck of cards """ @doc """ Creates a list of strings that represents the deck of cards """ def create_deck do values = ["Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "J", "Q", "K"] suits = ["Spades", "Clubs", "Hearts", "Diamonds"] for value <- values, suit <- suits do "#{value} of #{suit}" end end def shuffle(deck) do Enum.shuffle(deck) end @doc """ Divides a deck into a hand and the rest of the cards. The `hand_size` argument determine how many cards will be in the hand ** Examples ** iex> deck = Cards.create_deck iex> {hand, _deck} = Cards.deal(deck, 1) iex> hand ["Ace of Spades"] """ def deal(deck, hand_size) do Enum.split(deck, hand_size) end @doc """ Determines whether a dack contains a given card throught the `card` argument ** Example ** iex> deck = Cards.create_deck iex> Cards.contains?(deck, "Ace of Spades") true """ def contains?(deck, card) do Enum.member?(deck, card) end def save(deck, filename) do binary = :erlang.term_to_binary(deck) File.write(filename, binary) end def load(filename) do # in this case statement we're doing two operations at the # same time using patterns matching (comparison and assignment) case File.read(filename) do {:ok, binary} -> :erlang.binary_to_term(binary) {:error, _reason} -> "That file does not exist" end end def create_hand(hand_size) do Cards.create_deck |> Cards.shuffle |> Cards.deal(hand_size) end end