Download:
child 34:19dac34d9a51
parent 32:e528c326cf10
33:654a2f3e9ccf
Anton Shestakov <engored@ya.ru>, Mon, 03 Mar 2014 15:37:24 +0900
Each room has its own set of rules.

3 файлов изменено, 28 вставок(+), 26 удалений(-) [+]
src/rps.erl file | annotate | diff | comparison | revisions
src/rps_bots.erl file | annotate | diff | comparison | revisions
src/rps_room.erl file | annotate | diff | comparison | revisions
--- a/src/rps.erl Mon Mar 03 15:09:55 2014 +0900
+++ b/src/rps.erl Mon Mar 03 15:37:24 2014 +0900
@@ -4,6 +4,7 @@
lobby/0, counter/0, printer/0]).
-define(BOTS, [fool, copycat, gambler]).
+-define(RULES, rps5).
go() ->
rps_nickserv:start_link(),
@@ -16,8 +17,8 @@
lobby([]).
lobby(Players) when length(Players) =:= length(?BOTS) ->
- Room = spawn_link(rps_room, room, [Players]),
- lists:foreach(fun(Player) -> Player ! {room, Room} end, Players),
+ Room = spawn_link(rps_room, room, [?RULES, Players]),
+ lists:foreach(fun(Player) -> Player ! {room, Room, ?RULES} end, Players),
lobby([]);
lobby(Players) ->
--- a/src/rps_bots.erl Mon Mar 03 15:09:55 2014 +0900
+++ b/src/rps_bots.erl Mon Mar 03 15:37:24 2014 +0900
@@ -1,5 +1,4 @@
-module(rps_bots).
--import(rps3, [get_rules/0]).
-export([fool/0, copycat/0, gambler/0]).
fool() ->
@@ -9,19 +8,20 @@
fool(joined) ->
receive
- {room, Room} ->
+ {room, Room, Rules} ->
random:seed(now()),
- fool(Room)
- end;
+ Hands = apply(Rules, get_rules, []),
+ fool(Room, Hands)
+ end.
-fool(Room) ->
+fool(Room, Hands) ->
receive
start ->
- Hand = lists:nth(random:uniform(length(get_rules())), get_rules()),
+ Hand = lists:nth(random:uniform(length(Hands)), Hands),
Room ! {Hand, self()},
- fool(Room);
+ fool(Room, Hands);
{winner, _} ->
- fool(Room)
+ fool(Room, Hands)
end.
copycat() ->
@@ -31,8 +31,8 @@
copycat(joined) ->
receive
- {room, Room} ->
- copycat(Room, hd(get_rules()))
+ {room, Room, Rules} ->
+ copycat(Room, hd(apply(Rules, get_rules, [])))
end.
copycat(Room, Hand) ->
@@ -51,8 +51,9 @@
gambler(joined) ->
receive
- {room, Room} ->
- Wins = dict:from_list([{C, 0} || C <- get_rules()]),
+ {room, Room, Rules} ->
+ R = apply(Rules, get_rules, []),
+ Wins = dict:from_list([{C, 0} || C <- R]),
gambler(Room, Wins)
end.
--- a/src/rps_room.erl Mon Mar 03 15:09:55 2014 +0900
+++ b/src/rps_room.erl Mon Mar 03 15:37:24 2014 +0900
@@ -1,8 +1,7 @@
-module(rps_room).
--import(rps3, [get_rules/0, winning_hand/1]).
--export([room/1]).
+-export([room/2]).
--record(state, {players=[], choices=[]}).
+-record(state, {rules, players=[], choices=[]}).
% winner for 3 players is:
% rock, rock, rock -> undefined
@@ -10,12 +9,12 @@
% rock, paper, paper -> undefined
% rock, rock, paper -> {paper, Who}
-winner(Choices) when length(Choices) > 1 ->
+winner(Rules, 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]),
+ WHand = apply(Rules, winning_hand, [[Hand || {Hand, _} <- Choices]]),
Players = if
WHand =:= undefined -> [];
WHand =/= undefined -> dict:fetch(WHand, PlayersByHand)
@@ -25,12 +24,12 @@
_ -> undefined
end.
-room(Players) when is_list(Players) ->
+room(Rules, Players) when is_list(Players) ->
lists:foreach(fun(Player) -> Player ! start end, Players),
- room(#state{players=Players});
+ room(#state{rules=Rules, players=Players}).
-room(#state{players=Players, choices=Choices}) when length(Players) =:= length(Choices) ->
- Winner = winner(Choices),
+room(#state{rules=Rules, players=Players, choices=Choices}) when length(Players) =:= length(Choices) ->
+ Winner = winner(Rules, Choices),
Msg = if
Winner =:= undefined ->
{draw, Choices};
@@ -39,12 +38,13 @@
end,
counter ! Msg,
lists:foreach(fun(Player) -> Player ! Msg end, Players),
- room(Players);
+ room(Rules, Players);
-room(State=#state{choices=Choices}) ->
+room(State=#state{rules=Rules, choices=Choices}) ->
receive
{Hand, Who} ->
- ObjectIsOk = lists:any(fun(X) -> X =:= Hand end, get_rules()),
+ R = apply(Rules, get_rules, []),
+ ObjectIsOk = lists:any(fun(X) -> X =:= Hand end, R),
ChoiceMade = lists:keymember(Who, 2, Choices),
case ObjectIsOk andalso not ChoiceMade of
true ->