Skip to content

Instantly share code, notes, and snippets.

@rockwood
Created January 17, 2018 12:28
Show Gist options
  • Save rockwood/70cf79fa3ffdfb67f9fa08ecb5462dde to your computer and use it in GitHub Desktop.
Save rockwood/70cf79fa3ffdfb67f9fa08ecb5462dde to your computer and use it in GitHub Desktop.
Rock Paper Scissors
defmodule Rps do
def run(input) do
player1 = String.split(input, "")
player2 = Enum.shuffle(["r", "p", "s"])
result =
[player1, player2]
|> Enum.zip()
|> Enum.map(&result/1)
|> Enum.sum()
|> case do
0 -> "Draw"
result when result > 0 -> "Win"
result when result < 0 -> "Lose"
end
"#{player2}#{result}"
end
def result({a, a}), do: 0
def result({"r", "p"}), do: -1
def result({"p", "s"}), do: -1
def result({"s", "r"}), do: -1
def result({"r", "s"}), do: 1
def result({"p", "r"}), do: 1
def result({"s", "p"}), do: 1
def result(_), do: raise "Invalid play"
end
IO.gets("Input: ")
|> String.trim()
|> Rps.run()
|> IO.puts()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment