Last active
May 15, 2018 21:50
-
-
Save ryanwinchester/4ca8e26821971fd59f40de677a11fcd7 to your computer and use it in GitHub Desktop.
Just having fun...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Poker do | |
| @deck ~w( | |
| A♣ 2♣ 3♣ 4♣ 5♣ 6♣ 7♣ 8♣ 9♣ 10♣ J♣ Q♣ K♣ | |
| A◆ 2◆ 3◆ 4◆ 5◆ 6◆ 7◆ 8◆ 9◆ 10◆ J◆ Q◆ K◆ | |
| A♠ 2♠ 3♠ 4♠ 5♠ 6♠ 7♠ 8♠ 9♠ 10♠ J♠ Q♠ K♠ | |
| A♥ 2♥ 3♥ 4♥ 5♥ 6♥ 7♥ 8♥ 9♥ 10♥ J♥ Q♥ K♥ | |
| ) | |
| def deck, do: Enum.with_index(@deck) | |
| def deal(hand_count \\ 1) do | |
| deck() | |
| |> Enum.shuffle() | |
| |> Enum.chunk_every(5) | |
| |> Enum.take(hand_count) | |
| end | |
| def straight?(hand) do | |
| hand | |
| |> Enum.map(&(elem(&1, 1) |> rem(13))) | |
| |> Enum.sort() | |
| |> Enum.reduce(fn | |
| x, {n, seq?} -> {x, seq? && x == n + 1} | |
| x, n -> {x, x == n + 1} | |
| end) | |
| |> elem(1) | |
| end | |
| def flush?(hand) do | |
| hand | |
| |> Enum.map(&(elem(&1, 0) |> String.last())) | |
| |> Enum.uniq() | |
| |> (&(Enum.count(&1) === 1)).() | |
| end | |
| def straight_flush?(hand), do: straight?(hand) and flush?(hand) | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check it:
Output: