7:14b33824d432
Anton Shestakov <engored@ya.ru>, Tue, 25 Feb 2014 23:16:04 +0900
Add copycat bot. It copies the winning hand all the time.

next change 8:94027114ff65
previous change 6:bb8fdb49d6ba

rps.erl

Permissions: -rw-r--r--

Other formats: Feeds:
-module(rps).
-export([
go/0,
beats/1, winner/1,
lobby/0, counter/0, printer/0, room/1,
fool/0, copycat/0]).
-define(RULES, [rock, paper, scissors]).
-define(BOTS, [fool, copycat, fool]).
beats(C) ->
beats(C, ?RULES).
beats(C, [C|Tail]) ->
lists:last(Tail);
beats(C, [B,C|_]) ->
B;
beats(C, [_|T]) ->
beats(C, T).
% 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
1 ->
Absent = hd(sets:to_list(Absents)),
Unbeaten = beats(Absent),
Winners = [{C, Who} || {C, Who} <- Choices, C =:= Unbeaten],
case length(Winners) of
1 -> hd(Winners);
_ -> undefined
end;
_ -> undefined
end.
go() ->
register(lobby, spawn(rps, lobby, [])),
register(counter, spawn(rps, counter, [])),
register(printer, spawn(rps, printer, [])),
[spawn(rps, Bot, []) || Bot <- ?BOTS].
lobby() ->
lobby([]).
lobby(Players) when length(Players) =:= length(?BOTS) ->
Room = spawn(rps, room, [Players]),
lists:foreach(fun(Player) -> Player ! {room, Room} end, Players),
lobby([]);
lobby(Players) ->
receive
{join, Who} ->
lobby([Who|Players])
end.
counter() ->
counter(0, 0, dict:new()).
counter(Plays, Draws, Wins) ->
receive
{draw, _} ->
counter(Plays + 1, Draws + 1, Wins);
{winner, {_, Winner}} ->
counter(Plays + 1, Draws, dict:update_counter(Winner, 1, Wins));
{sendstats, Pid} ->
Pid ! {stats, {plays, Plays, draws, Draws, wins, dict:to_list(Wins)}},
counter(Plays, Draws, Wins)
end.
printer() ->
receive
{stats, Stats} ->
io:format("Stats: ~w.~n", [Stats])
after 1000 ->
counter ! {sendstats, self()}
end,
printer().
room(Players) ->
lists:foreach(fun(Player) -> Player ! start end, Players),
room(Players, []).
room(Players, Choices) when length(Players) =:= length(Choices) ->
Winner = winner(Choices),
Msg = if
Winner =:= undefined ->
{draw, Choices};
true ->
{winner, Winner}
end,
counter ! Msg,
lists:foreach(fun(Player) -> Player ! Msg end, Players),
room(Players);
room(Players, Choices) ->
receive
{Object, Who} ->
ObjectIsOk = lists:any(fun(X) -> X =:= Object end, ?RULES),
ChoiceMade = lists:any(fun({_, Player}) -> Player =:= Who end, Choices),
case ObjectIsOk andalso not ChoiceMade of
true ->
room(Players, [{Object, Who}|Choices]);
false ->
io:format("~w picks ~w, not allowed.~n", [Who, Object]),
room(Players, Choices)
end
end.
fool() ->
lobby ! {join, self()},
fool(joined).
fool(joined) ->
receive
{room, Room} ->
random:seed(now()),
fool(Room)
end;
fool(Room) ->
receive
start ->
Choice = lists:nth(random:uniform(length(?RULES)), ?RULES),
Room ! {Choice, self()},
fool(Room);
{winner, _} ->
fool(Room)
end.
copycat() ->
lobby ! {join, self()},
copycat(joined).
copycat(joined) ->
receive
{room, Room} ->
copycat(Room, hd(?RULES))
end.
copycat(Room, Choice) ->
receive
start ->
Room ! {Choice, self()},
copycat(Room, Choice);
{winner, {NewChoice, _}} ->
copycat(Room, NewChoice)
end.