37:674e9537b6ab
Anton Shestakov <engored@ya.ru>, Mon, 03 Mar 2014 20:06:02 +0900
Move #state record definition to rps_rules.hrl.

previous change 34:19dac34d9a51

src/rps_bots.erl

Permissions: -rw-r--r--

Other formats: Feeds:
-module(rps_bots).
-export([fool/0, copycat/0, gambler/0]).
fool() ->
rps_nickserv:nick(self(), "fool"),
lobby ! {join, self()},
fool(joined).
fool(joined) ->
receive
{room, Room, Rules} ->
random:seed(now()),
Hands = apply(Rules, get_hands, []),
fool(Room, Hands)
end.
fool(Room, Hands) ->
receive
start ->
Hand = lists:nth(random:uniform(length(Hands)), Hands),
Room ! {Hand, self()},
fool(Room, Hands);
{winner, _} ->
fool(Room, Hands)
end.
copycat() ->
rps_nickserv:nick(self(), "copycat"),
lobby ! {join, self()},
copycat(joined).
copycat(joined) ->
receive
{room, Room, Rules} ->
copycat(Room, hd(apply(Rules, get_hands, [])))
end.
copycat(Room, Hand) ->
receive
start ->
Room ! {Hand, self()},
copycat(Room, Hand);
{winner, {NewHand, _}} ->
copycat(Room, NewHand)
end.
gambler() ->
rps_nickserv:nick(self(), "gambler"),
lobby ! {join, self()},
gambler(joined).
gambler(joined) ->
receive
{room, Room, Rules} ->
Hands = apply(Rules, get_hands, []),
Wins = dict:from_list([{Hand, 0} || Hand <- Hands]),
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.