Download:
child 10:aa2a22dbe1e7
parent 8:94027114ff65
9:4b4d65b99174
Anton Shestakov <engored@ya.ru>, Wed, 26 Feb 2014 01:38:26 +0900
Move rules to a separate module.

2 файлов изменено, 30 вставок(+), 19 удалений(-) [+]
rps.erl file | annotate | diff | comparison | revisions
rps3.erl file | annotate | diff | comparison | revisions
--- a/rps.erl Tue Feb 25 23:55:18 2014 +0900
+++ b/rps.erl Wed Feb 26 01:38:26 2014 +0900
@@ -1,25 +1,13 @@
-module(rps).
+-import(rps3, [get_rules/0, beats/1]).
-export([
go/0,
- beats/1, winner/1,
+ winner/1,
lobby/0, counter/0, printer/0, room/1,
fool/0, copycat/0, gambler/0]).
--define(RULES, [rock, paper, scissors]).
-define(BOTS, [fool, copycat, gambler]).
-beats(C) ->
- beats(C, ?RULES).
-
-beats(C, [C|Tail]) ->
- lists:last(Tail);
-
-beats(C, [B,C|_]) ->
- B;
-
-beats(C, [_|T]) ->
- beats(C, T).
-
% winner for 3 players is:
% rock, rock, rock -> undefined
% rock, paper, scissors -> undefined
@@ -28,7 +16,7 @@
winner(Choices) when length(Choices) > 1 ->
CSet = sets:from_list([C || {C, _} <- Choices]),
- RSet = sets:from_list(?RULES),
+ RSet = sets:from_list(get_rules()),
Absents = sets:subtract(RSet, CSet),
case sets:size(Absents) of
1 ->
@@ -104,7 +92,7 @@
room(Players, Choices) ->
receive
{Object, Who} ->
- ObjectIsOk = lists:any(fun(X) -> X =:= Object end, ?RULES),
+ ObjectIsOk = lists:any(fun(X) -> X =:= Object end, get_rules()),
ChoiceMade = lists:any(fun({_, Player}) -> Player =:= Who end, Choices),
case ObjectIsOk andalso not ChoiceMade of
true ->
@@ -129,7 +117,7 @@
fool(Room) ->
receive
start ->
- Choice = lists:nth(random:uniform(length(?RULES)), ?RULES),
+ Choice = lists:nth(random:uniform(length(get_rules())), get_rules()),
Room ! {Choice, self()},
fool(Room);
{winner, _} ->
@@ -143,7 +131,7 @@
copycat(joined) ->
receive
{room, Room} ->
- copycat(Room, hd(?RULES))
+ copycat(Room, hd(get_rules()))
end.
copycat(Room, Choice) ->
@@ -162,7 +150,7 @@
gambler(joined) ->
receive
{room, Room} ->
- Wins = dict:from_list([{C, 0} || C <- ?RULES]),
+ Wins = dict:from_list([{C, 0} || C <- get_rules()]),
gambler(Room, Wins)
end.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/rps3.erl Wed Feb 26 01:38:26 2014 +0900
@@ -0,0 +1,23 @@
+-module(rps3).
+-export([get_rules/0, beats/1, test_beats/0]).
+
+get_rules() -> [rock, paper, scissors].
+
+beats(C) ->
+ beats(C, get_rules()).
+
+beats(C, [C|Tail]) ->
+ lists:last(Tail);
+
+beats(C, [B,C|_]) ->
+ B;
+
+beats(C, [_|T]) ->
+ beats(C, T).
+
+test_beats() ->
+ lists:foreach(
+ fun(C) ->
+ io:format("~w beats ~w.~n", [C, beats(C)])
+ end,
+ get_rules()).