Anton Shestakov <engored@ya.ru>, Sun, 23 Feb 2014 00:23:54 +0900
Define bots.
rps.erl
Permissions: -rw-r--r--
-export([lobby/0, room/1, beats/1, winner/1, go/0, fool/0]). -define(RULES, [rock, paper, scissors]). -define(BOTS, [fool, fool, fool]). % 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 -> CSet = sets:from_list([C || {C, _} <- Choices]), RSet = sets:from_list(?RULES), Absents = sets:subtract(RSet, CSet), case sets:size(Absents) of Absent = hd(sets:to_list(Absents)), Unbeaten = beats(Absent), Winners = [{C, Who} || {C, Who} <- Choices, C =:= Unbeaten], register(lobby, spawn(rps, lobby, [])), [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), lists:foreach(fun(Player) -> Player ! start end, Players), room(Players, Choices) when length(Players) =:= length(Choices) -> io:format("draw: ~w.~n", [Choices]), io:format("the winner is ~w.~n", [Best]), lists:foreach(fun(Player) -> Player ! Msg end, Players), room(Players, Choices) -> ObjectIsOk = lists:any(fun(X) -> X =:= Object end, ?RULES), ChoiceMade = lists:any(fun({_, Player}) -> Player =:= Who end, Choices), case ObjectIsOk andalso not ChoiceMade of room(Players, [{Object, Who}|Choices]); io:format("~w picks ~w, not allowed.~n", [Who, Object]), Choice = lists:nth(random:uniform(length(?RULES)), ?RULES),