26:6b3a3b22ea78
Anton Shestakov <engored@ya.ru>, Sun, 02 Mar 2014 17:39:33 +0900
Use state record for room.

next change 27:19ca2615be8f
previous change 23:af20828828e5

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} ->
random:seed(now()),
fool(Room)
end;
fool(Room) ->
receive
start ->
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} ->
copycat(Room, hd(get_rules()))
end.
copycat(Room, Hand) ->
receive
start ->
Room ! {Hand, self()},
copycat(Room, Hand);
{winner, {NewHand, _}} ->
copycat(Room, NewHand)
end.
gambler() ->
lobby ! {join, self()},
gambler(joined).
gambler(joined) ->
receive
{room, Room} ->
Wins = dict:from_list([{C, 0} || C <- get_rules()]),
gambler(Room, Wins)
end.
gambler(Room, Wins) ->
receive
start ->
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.