Download:
child 27:19ca2615be8f
parent 25:e7df778c2cdc
26:6b3a3b22ea78
Anton Shestakov <engored@ya.ru>, Sun, 02 Mar 2014 17:39:33 +0900
Use state record for room.

1 файлов изменено, 8 вставок(+), 6 удалений(-) [+]
src/rps_room.erl file | annotate | diff | comparison | revisions
--- a/src/rps_room.erl Sun Mar 02 15:32:17 2014 +0900
+++ b/src/rps_room.erl Sun Mar 02 17:39:33 2014 +0900
@@ -2,6 +2,8 @@
-import(rps3, [get_rules/0, winning_hand/1]).
-export([room/1]).
+-record(state, {players=[], choices=[]}).
+
% winner for 3 players is:
% rock, rock, rock -> undefined
% rock, paper, scissors -> undefined
@@ -23,11 +25,11 @@
_ -> undefined
end.
-room(Players) ->
+room(Players) when is_list(Players) ->
lists:foreach(fun(Player) -> Player ! start end, Players),
- room(Players, []).
+ room(#state{players=Players});
-room(Players, Choices) when length(Players) =:= length(Choices) ->
+room(#state{players=Players, choices=Choices}) when length(Players) =:= length(Choices) ->
Winner = winner(Choices),
Msg = if
Winner =:= undefined ->
@@ -39,16 +41,16 @@
lists:foreach(fun(Player) -> Player ! Msg end, Players),
room(Players);
-room(Players, Choices) ->
+room(State=#state{choices=Choices}) ->
receive
{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, [{Hand, Who}|Choices]);
+ room(State#state{choices=[{Hand, Who}|Choices]});
false ->
io:format("~w picks ~w, not allowed.~n", [Who, Hand]),
- room(Players, Choices)
+ room(State)
end
end.