Skip to content

Instantly share code, notes, and snippets.

@jclem
Forked from nearbycoder/BlackjackAssist.exs
Last active July 19, 2016 16:36
Show Gist options
  • Save jclem/07ae736c1ade6d5426f743ec4d21520d to your computer and use it in GitHub Desktop.
Save jclem/07ae736c1ade6d5426f743ec4d21520d 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 card == cards ->
draw_card(cards)
card ->
List.insert_at([cards], -1, card)
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
case draw_card do
card when card in cards ->
draw_card(cards)
card when length(cards) < 52 ->
List.insert_at(cards, -1, card)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment