28:44aa20fdf779
Anton Shestakov <engored@ya.ru>, Sun, 02 Mar 2014 18:00:14 +0900
Bots must know rules of current room (only rps3 at the moment).

next change 29:0f29304f36f4
previous change 27:19ca2615be8f

src/rps_bots.erl

Permissions: -rw-r--r--

Other formats: Feeds:
-module(rps_bots).
-import(rps3, [get_rules/0]).
-export([fool/0, copycat/0, gambler/0]).
fool() ->
lobby ! {join, self()},
fool(joined).
fool(joined) ->
receive
{room, Room} ->
Room ! {nickname, self(), "fool"},
random:seed(now()),
fool(Room)
end;
fool(Room) ->
receive
{start, _Rules} ->
Hand = lists:nth(random:uniform(length(get_rules())), get_rules()),
Room ! {Hand, self()},
fool(Room);
{winner, _} ->
fool(Room)
end.
copycat() ->
lobby ! {join, self()},
copycat(joined).
copycat(joined) ->
receive
{room, Room} ->
Room ! {nickname, self(), "copycat"},
copycat(Room, hd(get_rules()))
end.
copycat(Room, Hand) ->
receive
{start, _Rules} ->
Room ! {Hand, self()},
copycat(Room, Hand);
{winner, {NewHand, _}} ->
copycat(Room, NewHand)
end.
gambler() ->
lobby ! {join, self()},
gambler(joined).
gambler(joined) ->
receive
{room, Room} ->
Room ! {nickname, self(), "gambler"},
Wins = dict:from_list([{C, 0} || C <- get_rules()]),
gambler(Room, Wins)
end.
gambler(Room, Wins) ->
receive
{start, _Rules} ->
F = fun({_, A}, {_, B}) -> A =< B end,
{Hand, _} = hd(lists:sort(F, dict:to_list(Wins))),
Room ! {Hand, self()},
gambler(Room, Wins);
{winner, {Hand, _}} ->
gambler(Room, dict:update_counter(Hand, 1, Wins))
end.