Skip to content

Instantly share code, notes, and snippets.

@mk mk/mischer.ex
Created Dec 15, 2015

Embed
What would you like to do?
Der Sauspiel-Mischer
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
@mk

This comment has been minimized.

Copy link
Owner Author

commented Dec 15, 2015

So werden auf Sauspiel die Schafkopf-Karten gemischt, siehe sauspiel.de/mischer.

Anleitung zum selber nachmischen: Elixir installieren und dann folgendes ausführen:

$ iex -r mischer.ex
Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.1.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Mischer.shuffle
["Ag", "9h", "As", "Xg", "Ae", "7e", "9g", "7h", "Og", "Ug", "Kh", "Ue", "8g",
 "Xe", "Xs", "9e", "9s", "8h", "7g", "Ah", "7s", "Kg", "Ke", "Oh", "Us", "8s",
 "Xh", "Uh", "8e", "Oe", "Os", "Ks"]

Frohes Mischen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.