--- a/rps.erl Wed Feb 26 01:46:12 2014 +0900
+++ b/rps.erl Wed Feb 26 02:23:06 2014 +0900
% rock, rock, paper -> {paper, Who}
winner(Choices) when length(Choices) > 1 ->
- CSet = sets:from_list([C || {C, _} <- Choices]),
+ HSet = sets:from_list([Hand || {Hand, _} <- Choices]),
RSet = sets:from_list(get_rules()),
- Absents = sets:subtract(RSet, CSet),
+ Absents = sets:subtract(RSet, HSet),
case sets:size(Absents) of
Absent = hd(sets:to_list(Absents)),
Unbeaten = beats(Absent),
- Winners = [{C, Who} || {C, Who} <- Choices, C =:= Unbeaten],
+ Winners = [{Hand, Who} || {Hand, Who} <- Choices, Hand =:= Unbeaten],
room(Players, Choices) ->
- ObjectIsOk = lists:any(fun(X) -> X =:= Object end, get_rules()),
+ ObjectIsOk = lists:any(fun(X) -> X =:= Hand end, get_rules()),
ChoiceMade = lists:keymember(Who, 2, Choices),
case ObjectIsOk andalso not ChoiceMade of
- room(Players, [{Object, Who}|Choices]);
+ room(Players, [{Hand, Who}|Choices]);
- io:format("~w picks ~w, not allowed.~n", [Who, Object]),
+ io:format("~w picks ~w, not allowed.~n", [Who, Hand]),
- Choice = lists:nth(random:uniform(length(get_rules())), get_rules()),
- Room ! {Choice, self()},
+ Hand = lists:nth(random:uniform(length(get_rules())), get_rules()),
copycat(Room, hd(get_rules()))
-copycat(Room, Choice) ->
- Room ! {Choice, self()},
- {winner, {NewChoice, _}} ->
- copycat(Room, NewChoice)
+ {winner, {NewHand, _}} ->
F = fun({_, A}, {_, B}) -> A =< B end,
- {Choice, _} = hd(lists:sort(F, dict:to_list(Wins))),
- Room ! {Choice, self()},
+ {Hand, _} = hd(lists:sort(F, dict:to_list(Wins))),
- {winner, {Choice, _}} ->
- gambler(Room, dict:update_counter(Choice, 1, Wins))
+ gambler(Room, dict:update_counter(Hand, 1, Wins))