Download:
child 12:23a27ec9f362
parent 10:aa2a22dbe1e7
11:9dd5bb0a4c0c
Anton Shestakov <engored@ya.ru>, Wed, 26 Feb 2014 02:23:06 +0900
Choice is a hand that is attached to someone.

1 файлов изменено, 18 вставок(+), 18 удалений(-) [+]
rps.erl file | annotate | diff | comparison | revisions
--- a/rps.erl Wed Feb 26 01:46:12 2014 +0900
+++ b/rps.erl Wed Feb 26 02:23:06 2014 +0900
@@ -15,14 +15,14 @@
% 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
1 ->
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],
case length(Winners) of
1 -> hd(Winners);
_ -> undefined
@@ -91,14 +91,14 @@
room(Players, Choices) ->
receive
- {Object, Who} ->
- ObjectIsOk = lists:any(fun(X) -> X =:= Object end, get_rules()),
+ {Hand, Who} ->
+ ObjectIsOk = lists:any(fun(X) -> X =:= Hand end, get_rules()),
ChoiceMade = lists:keymember(Who, 2, Choices),
case ObjectIsOk andalso not ChoiceMade of
true ->
- room(Players, [{Object, Who}|Choices]);
+ room(Players, [{Hand, Who}|Choices]);
false ->
- io:format("~w picks ~w, not allowed.~n", [Who, Object]),
+ io:format("~w picks ~w, not allowed.~n", [Who, Hand]),
room(Players, Choices)
end
end.
@@ -117,8 +117,8 @@
fool(Room) ->
receive
start ->
- Choice = lists:nth(random:uniform(length(get_rules())), get_rules()),
- Room ! {Choice, self()},
+ Hand = lists:nth(random:uniform(length(get_rules())), get_rules()),
+ Room ! {Hand, self()},
fool(Room);
{winner, _} ->
fool(Room)
@@ -134,13 +134,13 @@
copycat(Room, hd(get_rules()))
end.
-copycat(Room, Choice) ->
+copycat(Room, Hand) ->
receive
start ->
- Room ! {Choice, self()},
- copycat(Room, Choice);
- {winner, {NewChoice, _}} ->
- copycat(Room, NewChoice)
+ Room ! {Hand, self()},
+ copycat(Room, Hand);
+ {winner, {NewHand, _}} ->
+ copycat(Room, NewHand)
end.
gambler() ->
@@ -158,9 +158,9 @@
receive
start ->
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))),
+ Room ! {Hand, self()},
gambler(Room, Wins);
- {winner, {Choice, _}} ->
- gambler(Room, dict:update_counter(Choice, 1, Wins))
+ {winner, {Hand, _}} ->
+ gambler(Room, dict:update_counter(Hand, 1, Wins))
end.