Download:
child 28:44aa20fdf779
parent 26:6b3a3b22ea78
27:19ca2615be8f
Anton Shestakov <engored@ya.ru>, Sun, 02 Mar 2014 17:58:19 +0900
Nicknames for bots.

2 файлов изменено, 7 вставок(+), 2 удалений(-) [+]
src/rps_bots.erl file | annotate | diff | comparison | revisions
src/rps_room.erl file | annotate | diff | comparison | revisions
--- a/src/rps_bots.erl Sun Mar 02 17:39:33 2014 +0900
+++ b/src/rps_bots.erl Sun Mar 02 17:58:19 2014 +0900
@@ -9,6 +9,7 @@
fool(joined) ->
receive
{room, Room} ->
+ Room ! {nickname, self(), "fool"},
random:seed(now()),
fool(Room)
end;
@@ -30,6 +31,7 @@
copycat(joined) ->
receive
{room, Room} ->
+ Room ! {nickname, self(), "copycat"},
copycat(Room, hd(get_rules()))
end.
@@ -49,6 +51,7 @@
gambler(joined) ->
receive
{room, Room} ->
+ Room ! {nickname, self(), "gambler"},
Wins = dict:from_list([{C, 0} || C <- get_rules()]),
gambler(Room, Wins)
end.
--- a/src/rps_room.erl Sun Mar 02 17:39:33 2014 +0900
+++ b/src/rps_room.erl Sun Mar 02 17:58:19 2014 +0900
@@ -2,7 +2,7 @@
-import(rps3, [get_rules/0, winning_hand/1]).
-export([room/1]).
--record(state, {players=[], choices=[]}).
+-record(state, {players=[], choices=[], nicknames=dict:new()}).
% winner for 3 players is:
% rock, rock, rock -> undefined
@@ -41,8 +41,10 @@
lists:foreach(fun(Player) -> Player ! Msg end, Players),
room(Players);
-room(State=#state{choices=Choices}) ->
+room(State=#state{choices=Choices, nicknames=Nicknames}) ->
receive
+ {nickname, Who, Nickname} ->
+ room(State#state{nicknames=dict:store(Who, Nickname, Nicknames)});
{Hand, Who} ->
ObjectIsOk = lists:any(fun(X) -> X =:= Hand end, get_rules()),
ChoiceMade = lists:keymember(Who, 2, Choices),