Download:
child 22:8a7719ddce58
parent 20:3f27b5c2f402
21:4979118a5e61
Anton Shestakov <engored@ya.ru>, Sat, 01 Mar 2014 23:01:29 +0900
Separate rps_room.erl.

2 файлов изменено, 58 вставок(+), 56 удалений(-) [+]
src/rps.erl file | annotate | diff | comparison | revisions
src/rps_room.erl file | annotate | diff | comparison | revisions
--- a/src/rps.erl Sat Mar 01 22:46:52 2014 +0900
+++ b/src/rps.erl Sat Mar 01 23:01:29 2014 +0900
@@ -1,45 +1,23 @@
-module(rps).
--import(rps3, [get_rules/0, winning_hand/1]).
+-import(rps3, [get_rules/0]).
-export([
go/0,
- winner/1,
- lobby/0, counter/0, printer/0, room/1,
+ lobby/0, counter/0, printer/0,
fool/0, copycat/0, gambler/0]).
-define(BOTS, [fool, copycat, gambler]).
-% winner for 3 players is:
-% rock, rock, rock -> undefined
-% rock, paper, scissors -> undefined
-% rock, paper, paper -> undefined
-% rock, rock, paper -> {paper, Who}
-
-winner(Choices) when length(Choices) > 1 ->
- PlayersByHand = lists:foldl(
- fun({Hand, Who}, Acc) ->
- dict:append(Hand, Who, Acc)
- end, dict:new(), Choices),
- WHand = winning_hand([Hand || {Hand, _} <- Choices]),
- Players = if
- WHand =:= undefined -> [];
- WHand =/= undefined -> dict:fetch(WHand, PlayersByHand)
- end,
- case length(Players) of
- 1 -> {WHand, hd(Players)};
- _ -> undefined
- end.
-
go() ->
register(lobby, spawn_link(?MODULE, lobby, [])),
register(counter, spawn_link(?MODULE, counter, [])),
register(printer, spawn_link(?MODULE, printer, [])),
[spawn_link(?MODULE, Bot, []) || Bot <- ?BOTS].
-
+
lobby() ->
lobby([]).
lobby(Players) when length(Players) =:= length(?BOTS) ->
- Room = spawn_link(?MODULE, room, [Players]),
+ Room = spawn_link(rps_room, room, [Players]),
lists:foreach(fun(Player) -> Player ! {room, Room} end, Players),
lobby([]);
@@ -78,36 +56,6 @@
end,
printer().
-room(Players) ->
- lists:foreach(fun(Player) -> Player ! start end, Players),
- room(Players, []).
-
-room(Players, Choices) when length(Players) =:= length(Choices) ->
- Winner = winner(Choices),
- Msg = if
- Winner =:= undefined ->
- {draw, Choices};
- true ->
- {winner, Winner}
- end,
- counter ! Msg,
- lists:foreach(fun(Player) -> Player ! Msg end, Players),
- room(Players);
-
-room(Players, Choices) ->
- receive
- {Hand, Who} ->
- ObjectIsOk = lists:any(fun(X) -> X =:= Hand end, get_rules()),
- ChoiceMade = lists:keymember(Who, 2, Choices),
- case ObjectIsOk andalso not ChoiceMade of
- true ->
- room(Players, [{Hand, Who}|Choices]);
- false ->
- io:format("~w picks ~w, not allowed.~n", [Who, Hand]),
- room(Players, Choices)
- end
- end.
-
fool() ->
lobby ! {join, self()},
fool(joined).
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/rps_room.erl Sat Mar 01 23:01:29 2014 +0900
@@ -0,0 +1,54 @@
+-module(rps_room).
+-import(rps3, [get_rules/0, winning_hand/1]).
+-export([winner/1, room/1]).
+
+% winner for 3 players is:
+% rock, rock, rock -> undefined
+% rock, paper, scissors -> undefined
+% rock, paper, paper -> undefined
+% rock, rock, paper -> {paper, Who}
+
+winner(Choices) when length(Choices) > 1 ->
+ PlayersByHand = lists:foldl(
+ fun({Hand, Who}, Acc) ->
+ dict:append(Hand, Who, Acc)
+ end, dict:new(), Choices),
+ WHand = winning_hand([Hand || {Hand, _} <- Choices]),
+ Players = if
+ WHand =:= undefined -> [];
+ WHand =/= undefined -> dict:fetch(WHand, PlayersByHand)
+ end,
+ case length(Players) of
+ 1 -> {WHand, hd(Players)};
+ _ -> undefined
+ end.
+
+room(Players) ->
+ lists:foreach(fun(Player) -> Player ! start end, Players),
+ room(Players, []).
+
+room(Players, Choices) when length(Players) =:= length(Choices) ->
+ Winner = winner(Choices),
+ Msg = if
+ Winner =:= undefined ->
+ {draw, Choices};
+ true ->
+ {winner, Winner}
+ end,
+ counter ! Msg,
+ lists:foreach(fun(Player) -> Player ! Msg end, Players),
+ room(Players);
+
+room(Players, Choices) ->
+ receive
+ {Hand, Who} ->
+ ObjectIsOk = lists:any(fun(X) -> X =:= Hand end, get_rules()),
+ ChoiceMade = lists:keymember(Who, 2, Choices),
+ case ObjectIsOk andalso not ChoiceMade of
+ true ->
+ room(Players, [{Hand, Who}|Choices]);
+ false ->
+ io:format("~w picks ~w, not allowed.~n", [Who, Hand]),
+ room(Players, Choices)
+ end
+ end.