Skip to content

Instantly share code, notes, and snippets.

@nearbycoder
Last active July 19, 2016 16:53
Show Gist options
  • Save nearbycoder/78a2d9a463c4465cd9f5b6ef56b3671d to your computer and use it in GitHub Desktop.
Save nearbycoder/78a2d9a463c4465cd9f5b6ef56b3671d to your computer and use it in GitHub Desktop.
simple module that helps divy up cards
defmodule BlackjackAssist do
@suits ~w(hearts clubs spades diamonds)
@values ~w(A 2 3 4 5 6 7 8 9 10 J Q K)
def draw_card do
{Enum.random(@suits), Enum.random(@values)}
end
def draw_card({:reshuffle}) do
draw_card
end
def draw_card(cards) when is_tuple(cards) do
case draw_card do
card when cards == card ->
draw_card(cards)
card ->
[card | [cards]]
end
end
def draw_card(cards) when is_list(cards) and length(cards) == 52 do
{:reshuffle}
end
def draw_card(cards) when is_list(cards) do
card = draw_card
if Enum.member?(cards, card) do
draw_card(cards)
else
[card | cards]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment