Download:
child 2:ecb57261bc30
parent 0:58a061003687
1:f44d2a7c91f4
Anton Shestakov <engored@ya.ru>, Sun, 23 Feb 2014 00:17:17 +0900
Register lobby process instead of passing it every time.

1 файлов изменено, 11 вставок(+), 11 удалений(-) [+]
rps.erl file | annotate | diff | comparison | revisions
--- a/rps.erl Sun Feb 23 00:04:28 2014 +0900
+++ b/rps.erl Sun Feb 23 00:17:17 2014 +0900
@@ -1,5 +1,5 @@
-module(rps).
--export([lobby/0, room/1, beats/1, winner/1, go/0, fool/1]).
+-export([lobby/0, room/1, beats/1, winner/1, go/0, fool/0]).
-define(RULES, [rock, paper, scissors]).
@@ -38,8 +38,8 @@
end.
go() ->
- Lobby = spawn(rps, lobby, []),
- [spawn(rps, fool, [Lobby]) || _ <- lists:seq(1, 3)].
+ register(lobby, spawn(rps, lobby, [])),
+ [spawn(rps, fool, []) || _ <- lists:seq(1, 3)].
lobby() ->
lobby([]).
@@ -86,23 +86,23 @@
end
end.
-fool(Lobby) ->
- Lobby ! {join, self()},
- fool(Lobby, undefined).
+fool() ->
+ lobby ! {join, self()},
+ fool(undefined).
-fool(Lobby, undefined) ->
+fool(undefined) ->
receive
{room, Room} ->
random:seed(now()),
- fool(Lobby, Room)
+ fool(Room)
end;
-fool(Lobby, Room) ->
+fool(Room) ->
receive
start ->
Choice = lists:nth(random:uniform(length(?RULES)), ?RULES),
Room ! {Choice, self()},
- fool(Lobby, Room);
+ fool(Room);
{winner, _} ->
- fool(Lobby, Room)
+ fool(Room)
end.