-
Notifications
You must be signed in to change notification settings - Fork 1
/
hand.ex
49 lines (43 loc) · 1.32 KB
/
hand.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
defmodule Dojo.Hand do
def score(cards) do
sorted_cards = Enum.sort(cards, fn c, n -> c.rank <= n.rank end)
evaluate(sorted_cards)
end
defp evaluate(cards) do
suits = Enum.map(cards, fn card -> card.suit end)
ranks = Enum.map(cards, fn card -> card.rank end)
cond do
# royal flush:
same_suit?(suits) && consecutive_ranks?(ranks) && Enum.at(ranks, 0) == 10 ->
:royal_flush
same_suit?(suits) && consecutive_ranks?(ranks) ->
:straight_flush
of_a_kind?(cards, 4) ->
:four_of_a_kind
of_a_kind?(cards, 3) && of_a_kind?(cards, 2) ->
:full_house
same_suit?(suits) ->
:flush
consecutive_ranks?(ranks) ->
:straight
of_a_kind?(cards, 3) ->
:three_of_a_kind
true ->
:high_card
end
end
def of_a_kind?(cards, x) do
cards
|> Enum.group_by(fn c -> c.rank end)
|> Stream.map(fn {rank, cards} -> {rank, Enum.count(cards)} end)
|> Enum.any?(fn {_rank, n} -> n == x end)
end
def same_suit?(suits) do
first_suit = Enum.fetch!(suits, 0)
Enum.all?(suits, fn suit -> suit == first_suit end)
end
def consecutive_ranks?([cur_rank, next_rank | rem_ranks]) do
next_rank - cur_rank == 1 && consecutive_ranks?([next_rank | rem_ranks])
end
def consecutive_ranks?(_), do: true
end