35:1963573a943e
Anton Shestakov <engored@ya.ru>, Mon, 03 Mar 2014 19:46:02 +0900
Make rps3 a gen_server under the hood.

previous change 34:19dac34d9a51

src/rps_room.erl

Permissions: -rw-r--r--

Other formats: Feeds:
-module(rps_room).
-export([room/2]).
-record(state, {rules, players=[], choices=[]}).
% winner for 3 players is:
% rock, rock, rock -> undefined
% rock, paper, scissors -> undefined
% rock, paper, paper -> undefined
% rock, rock, paper -> {paper, Who}
winner(Rules, Choices) when length(Choices) > 1 ->
PlayersByHand = lists:foldl(
fun({Hand, Who}, Acc) ->
dict:append(Hand, Who, Acc)
end, dict:new(), Choices),
WHand = apply(Rules, winning_hand, [[Hand || {Hand, _} <- Choices]]),
Players = if
WHand =:= undefined -> [];
WHand =/= undefined -> dict:fetch(WHand, PlayersByHand)
end,
case length(Players) of
1 -> {WHand, hd(Players)};
_ -> undefined
end.
room(Rules, Players) when is_list(Players) ->
lists:foreach(fun(Player) -> Player ! start end, Players),
room(#state{rules=Rules, players=Players}).
room(#state{rules=Rules, players=Players, choices=Choices}) when length(Players) =:= length(Choices) ->
Winner = winner(Rules, Choices),
Msg = if
Winner =:= undefined ->
{draw, Choices};
true ->
{winner, Winner}
end,
counter ! Msg,
lists:foreach(fun(Player) -> Player ! Msg end, Players),
room(Rules, Players);
room(State=#state{rules=Rules, choices=Choices}) ->
receive
{Hand, Who} ->
Hands = apply(Rules, get_hands, []),
ObjectIsOk = lists:any(fun(X) -> X =:= Hand end, Hands),
ChoiceMade = lists:keymember(Who, 2, Choices),
case ObjectIsOk andalso not ChoiceMade of
true ->
room(State#state{choices=[{Hand, Who}|Choices]});
false ->
io:format("~w picks ~w, not allowed.~n", [Who, Hand]),
room(State)
end
end.