Created
May 26, 2020 22:23
-
-
Save fjpse/1d613352fb3f52b9bf8072f41501ed2d to your computer and use it in GitHub Desktop.
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
-module(rps). | |
-export([ | |
beat/1, | |
lose/1, | |
result/2, | |
tournament/2 | |
]). | |
-include_lib("eunit/include/eunit.hrl"). | |
-type gesture() :: paper | scissors | rock. | |
-type result() :: -1 | 0 | 1. | |
-spec beat(gesture()) -> gesture(). | |
beat(rock) -> paper; | |
beat(paper) -> scissors; | |
beat(scissors) -> rock. | |
beat_test() -> | |
paper = beat(rock), | |
scissors = beat(paper), | |
rock = beat(scissors). | |
-spec lose(gesture()) -> gesture(). | |
lose(rock) -> scissors; | |
lose(scissors) -> paper; | |
lose(paper) -> rock. | |
lose_test() -> | |
scissors = lose(rock), | |
paper = lose(scissors), | |
rock = lose(paper). | |
-spec result(gesture(), gesture()) -> result(). | |
result(_X,_X) -> 0; | |
result(X,Y) -> | |
case beat(Y) of | |
X -> 1; | |
_ -> -1 | |
end. | |
result_test_() -> [ | |
?_assert(result(rock, rock) =:= 0), | |
?_assert(result(rock, paper) =:= -1), | |
?_assert(result(rock, scissors) =:= 1), | |
?_assert(result(paper, paper) =:= 0), | |
?_assert(result(paper, scissors) =:= -1), | |
?_assert(result(paper, rock) =:= 1), | |
?_assert(result(scissors, scissors) =:= 0), | |
?_assert(result(scissors, rock) =:= -1), | |
?_assert(result(scissors, paper) =:= 1) | |
]. | |
-spec tournament([gesture()], [gesture()]) -> integer(). | |
tournament(Left, Right) -> | |
Results = lists:zipwith(fun result/2, Left, Right), | |
lists:sum(Results). | |
tournament_test_() -> [ | |
?_assert(-1 =:= tournament([rock, rock, paper, paper], [rock, paper, scissors, rock])), | |
?_assert(-2 =:= tournament([rock, rock, rock], [rock, paper, paper])), | |
?_assert( 2 =:= tournament([rock, rock, rock], [scissors, rock, scissors])) | |
]. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok. Thanks for your review.