Anton Shestakov <engored@ya.ru>, Mon, 03 Mar 2014 20:08:31 +0900
Rename #state from rps_rules.hrl to #ruleinfo.
src/rps_room.erl
Permissions: -rw-r--r--
-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( dict:append(Hand, Who, Acc) end, dict:new(), Choices), WHand = apply(Rules, winning_hand, [[Hand || {Hand, _} <- Choices]]), WHand =:= undefined -> []; WHand =/= undefined -> dict:fetch(WHand, PlayersByHand) 1 -> {WHand, hd(Players)}; 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), lists:foreach(fun(Player) -> Player ! Msg end, Players), room(State=#state{rules=Rules, choices=Choices}) -> 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 room(State#state{choices=[{Hand, Who}|Choices]}); io:format("~w picks ~w, not allowed.~n", [Who, Hand]),