From 4c899605bdb94c254589de3d878caf73c135a7bc Mon Sep 17 00:00:00 2001 From: robstallion Date: Fri, 23 Jun 2017 17:19:27 +0100 Subject: [PATCH] the best commit message ever. written by cleop --- lib/dojo/hand.ex | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/dojo/hand.ex b/lib/dojo/hand.ex index 58260f2..5d0c1e3 100644 --- a/lib/dojo/hand.ex +++ b/lib/dojo/hand.ex @@ -5,5 +5,38 @@ defmodule Dojo.Hand do evaluate(cards) end - defp evaluate(_), do: :high_card + defp sort_cards(cards) do + Enum.sort(cards) + end + + defp evaluate(hand) do + sorted_hand = sort_cards(hand) + [first_card | tail] = sorted_hand + check_suit(first_card, tail, hand) + end + + defp check_suit(card, [], hand) do + # suit is all the same + :flush + end + + defp check_suit(first_card, list, hand) do + [next_card | rest] = list + case first_card.suit == next_card.suit do + true -> + check_suit(next_card, rest, hand) + false -> + # not all suits are same go here + :high_card + end + end + end + +# cards = [ +# %{rank: 13, suit: :hearts}, +# %{rank: 10, suit: :hearts}, +# %{rank: 11, suit: :hearts}, +# %{rank: 12, suit: :hearts}, +# %{rank: 14, suit: :hearts} +# ]