Skip to content

Instantly share code, notes, and snippets.

@rockwood
Created January 17, 2018 12:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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