Commits

Anton Shestakov committed 6b3a3b2

Use state record for room.

Comments (0)

Files changed (1)

 -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
         _ -> 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 ->
     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.