Created
March 18, 2017 21:29
-
-
Save jason-carter/0418b20ccf9e8279954e94f012a0b394 to your computer and use it in GitHub Desktop.
FutureLearn Functional Programming In Erlang 3.8: Modelling the basics of 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
-module(rps). | |
-export([result/2]). | |
-export([tournament/2]). | |
% Win and Lose | |
beat(rock) -> paper; | |
beat(paper) -> scissors; | |
beat(scissors) -> rock. | |
lose(rock) -> scissors; | |
lose(paper) -> rock; | |
lose(scissors) -> paper. | |
% A round | |
result(Match, Match) -> draw; | |
result(Play1, Play2) -> | |
case lose(Play1) == Play2 of | |
true -> win; | |
false -> lose | |
end. | |
% A tournament | |
% > 0 Win for left | |
% 0 draw | |
% < 0 Win for right | |
% | |
% e.g. | |
% rps:tournament([rock,rock,paper,paper],[rock,paper,scissors,rock] = -1 | |
tournament(Left, Right) -> lists:foldr(fun(X,Y) -> X+Y end, 0, | |
lists:map(fun resultpoint/1, | |
lists:zipwith(fun result/2, Left, Right))). | |
resultpoint(Result) -> | |
% converts a win, lose or draw to an integer representation | |
case Result of | |
win -> 1; | |
draw -> 0; | |
lose -> -1 | |
end. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment