Download:
child 15:36f639142522
parent 13:30f6287441d2
14:ea71408f41b3
Anton Shestakov <engored@ya.ru>, Wed, 26 Feb 2014 19:05:32 +0900
rps5 rules.

1 файлов изменено, 85 вставок(+), 0 удалений(-) [+]
rps5.erl file | annotate | diff | comparison | revisions
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/rps5.erl Wed Feb 26 19:05:32 2014 +0900
@@ -0,0 +1,85 @@
+-module(rps5).
+-export([
+ get_rules/0,
+ beats/1, test_beats/0,
+ loses/1, test_loses/0,
+ winning_hand/1, test_winning_hand/0]).
+
+get_rules() -> [rock, paper, scissors, spock, lizard].
+
+beats(rock) ->
+ [scissors, lizard];
+
+beats(paper) ->
+ [spock, rock];
+
+beats(scissors) ->
+ [lizard, paper];
+
+beats(spock) ->
+ [rock, scissors];
+
+beats(lizard) ->
+ [paper, spock].
+
+loses(lizard) ->
+ [scissors, rock];
+
+loses(spock) ->
+ [paper, lizard];
+
+loses(scissors) ->
+ [rock, spock];
+
+loses(paper) ->
+ [lizard, scissors];
+
+loses(rock) ->
+ [spock, paper].
+
+test_beats() ->
+ lists:foreach(
+ fun(H) ->
+ io:format("~w beats ~w.~n", [H, beats(H)])
+ end,
+ get_rules()).
+
+test_loses() ->
+ lists:foreach(
+ fun(H) ->
+ io:format("~w loses to ~w.~n", [H, loses(H)])
+ end,
+ get_rules()).
+
+% winner for 3 hands is:
+% rock, rock, rock -> rock
+% rock, paper, scissors -> undefined
+% rock, paper, paper -> paper
+% rock, rock, paper -> paper
+
+winning_hand(Hands) ->
+ HSet = sets:from_list(Hands),
+ sets:fold(
+ fun(Hand, Acc) ->
+ Loses = loses(Hand),
+ case sets:is_disjoint(sets:from_list(Loses), HSet) of
+ true -> Hand;
+ false -> Acc
+ end
+ end, undefined, HSet).
+
+test_winning_hand() ->
+ Exclude = fun(Hand) -> lists:delete(Hand, get_rules()) end,
+ Cases = [
+ [rock, rock, rock],
+ [rock, paper, scissors],
+ [rock, paper, paper],
+ [rock, rock, paper]
+ |lists:map(Exclude, get_rules())
+ ],
+ lists:foreach(
+ fun(Hands) ->
+ Winner = winning_hand(Hands),
+ io:format("~w wins in ~w.~n", [Winner, Hands])
+ end,
+ Cases).