33:654a2f3e9ccf
Anton Shestakov <engored@ya.ru>, Mon, 03 Mar 2014 15:37:24 +0900
Each room has its own set of rules.

next change 34:19dac34d9a51
previous change 24:19a7ba960f8e

src/rps3.erl

Permissions: -rw-r--r--

Other formats: Feeds:
-module(rps3).
-export([get_rules/0, winning_hand/1]).
get_rules() -> [rock, paper, scissors].
beats(H, [H|Tail]) ->
lists:last(Tail);
beats(H, [B,H|_]) ->
B;
beats(H, [_|T]) ->
beats(H, T).
loses(H) ->
beats(H, lists:reverse(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) ->
case sets:is_element(loses(Hand), HSet) of
true -> Acc;
false -> Hand
end
end, undefined, HSet).