Anton Shestakov <engored@ya.ru>, Wed, 26 Feb 2014 02:23:06 +0900
Choice is a hand that is attached to someone.
rps.erl
Permissions: -rw-r--r--
-import(rps3, [get_rules/0, beats/1]). lobby/0, counter/0, printer/0, room/1, fool/0, copycat/0, gambler/0]). -define(BOTS, [fool, copycat, gambler]). % winner for 3 players is: % rock, rock, rock -> undefined % rock, paper, scissors -> undefined % rock, paper, paper -> undefined % rock, rock, paper -> {paper, Who} winner(Choices) when length(Choices) > 1 -> HSet = sets:from_list([Hand || {Hand, _} <- Choices]), RSet = sets:from_list(get_rules()), Absents = sets:subtract(RSet, HSet), case sets:size(Absents) of Absent = hd(sets:to_list(Absents)), Unbeaten = beats(Absent), Winners = [{Hand, Who} || {Hand, Who} <- Choices, Hand =:= Unbeaten], register(lobby, spawn(rps, lobby, [])), register(counter, spawn(rps, counter, [])), register(printer, spawn(rps, printer, [])), [spawn(rps, Bot, []) || Bot <- ?BOTS]. lobby(Players) when length(Players) =:= length(?BOTS) -> Room = spawn(rps, room, [Players]), lists:foreach(fun(Player) -> Player ! {room, Room} end, Players), counter(0, 0, dict:new()). counter(Plays, Draws, Wins) -> counter(Plays + 1, Draws + 1, Wins); counter(Plays + 1, Draws, dict:update_counter(Winner, 1, Wins)); Pid ! {stats, {plays, Plays, draws, Draws, wins, dict:to_list(Wins)}}, counter(Plays, Draws, Wins) io:format("Stats: ~w.~n", [Stats]) counter ! {sendstats, self()} lists:foreach(fun(Player) -> Player ! start end, Players), room(Players, Choices) when length(Players) =:= length(Choices) -> Winner = winner(Choices), lists:foreach(fun(Player) -> Player ! Msg end, Players), room(Players, Choices) -> ObjectIsOk = lists:any(fun(X) -> X =:= Hand end, get_rules()), ChoiceMade = lists:keymember(Who, 2, Choices), case ObjectIsOk andalso not ChoiceMade of room(Players, [{Hand, Who}|Choices]); io:format("~w picks ~w, not allowed.~n", [Who, Hand]), Hand = lists:nth(random:uniform(length(get_rules())), get_rules()), copycat(Room, hd(get_rules())) {winner, {NewHand, _}} -> Wins = dict:from_list([{C, 0} || C <- get_rules()]), F = fun({_, A}, {_, B}) -> A =< B end, {Hand, _} = hd(lists:sort(F, dict:to_list(Wins))), gambler(Room, dict:update_counter(Hand, 1, Wins))