Created
February 10, 2023 18:43
-
-
Save somoza/2c233e01d89a84d60af161c8238b4441 to your computer and use it in GitHub Desktop.
rock paper scissors
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 Game do | |
@win_combinations [rock: :scissors, paper: :rock, scissors: :paper] | |
@valid_options [:rock, :paper, :scissors] | |
def play(player1, player2) do | |
with {player1, player2} = plays <- validate_params({player1, player2}) do | |
winner(plays) | |
else | |
_ -> "Tiraste fruta" | |
end | |
end | |
defp winner(plays) when plays in @win_combinations do | |
"Player1" | |
end | |
defp winner({player1, player2}) when player1 == player2 do | |
"Draw" | |
end | |
defp winner(_plays) do | |
"Player2" | |
end | |
defp validate_params(plays) do | |
case plays do | |
{player1, player2} when player1 in @valid_options and player2 in @valid_options -> | |
plays | |
_ -> | |
"Invalid play" | |
end | |
end | |
end | |
Game.play(:rock, :scissors) | |
Game.play(:paper, :scissors) | |
Game.play(:paper, :paper) | |
Game.play(:lagarto, :spok) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment