-
Notifications
You must be signed in to change notification settings - Fork 0
/
cards.ex
74 lines (58 loc) · 1.3 KB
/
cards.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
defmodule Cards do
@moduledoc """
Creates and deal cards.
"""
@doc """
Creates a new deck.
"""
def create_deck() do
values = ["Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Queen", "Jack", "King"]
suits = ["Hearts", "Spades", "Clubs", "Diamonds"]
for suit <- suits, value <- values do
"#{value} of #{suit}"
end
end
@doc """
Suffles a `deck`.
"""
def shuffle(deck) do
Enum.shuffle(deck)
end
@doc """
Check if there is a `card` in the `deck`.
"""
def contains?(deck, card) do
Enum.member?(deck, card)
end
@doc """
Divides a deck into a hand of cards and the rest.
The `hand_size` argument indicates the number of cards of the hand.
"""
def deal(deck, hand_size) do
Enum.split(deck, hand_size)
end
@doc """
Saves a deck in file named with `filename`
"""
def save(deck, filename) do
binary = :erlang.term_to_binary(deck)
File.write(filename, binary)
end
@doc """
Loads a deck from a file with the name `filename`.
"""
def load(filename) do
case File.read(filename) do
{:ok, binary} -> :erlang.binary_to_term(binary)
{:error, _reason} -> "That file does not exist"
end
end
@doc """
Creates a hand based on `hand_size`.
"""
def create_hand(hand_size) do
Cards.create_deck
|>Cards.shuffle
|>Cards.deal(hand_size)
end
end