Created
December 15, 2015 16:51
-
-
Save mk/a871d1bba37a08db769f to your computer and use it in GitHub Desktop.
Der Sauspiel-Mischer
This file contains 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 Mischer do | |
def deck do | |
suits = ["s","h","g","e"] | |
ranks = ["7","8","9","X","U","O","K","A"] | |
for rank <- ranks, suit <- suits, do: rank <> suit | |
end | |
def shuffle(cards \\ deck) do | |
card_count = Enum.count(cards) | |
Enum.reduce 0..(card_count - 1), cards, fn(i, shuffled_cards) -> | |
swap(shuffled_cards, i, rand_uniform(i, card_count)) | |
end | |
end | |
defp swap(list, i, j) do | |
i_element = Enum.at(list, i) | |
j_element = Enum.at(list, j) | |
list |> List.replace_at(i, j_element) |> List.replace_at(j, i_element) | |
end | |
defp rand_uniform(lo, hi) do | |
:crypto.rand_uniform(lo, hi) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So werden auf Sauspiel die Schafkopf-Karten gemischt, siehe sauspiel.de/mischer.
Anleitung zum selber nachmischen: Elixir installieren und dann folgendes ausführen:
Frohes Mischen!