Download:
child 52:d9139e36aa28
parent 50:3f31a4eec418
51:24ba56a575b1
Anton Shestakov <engored@ya.ru>, Sat, 08 Mar 2014 23:37:19 +0900
rps_rules:why/3, use it in rps3 and rps5.

3 файлов изменено, 12 вставок(+), 5 удалений(-) [+]
src/rps3.erl file | annotate | diff | comparison | revisions
src/rps5.erl file | annotate | diff | comparison | revisions
src/rps_rules.erl file | annotate | diff | comparison | revisions
--- a/src/rps3.erl Sat Mar 08 23:36:15 2014 +0900
+++ b/src/rps3.erl Sat Mar 08 23:37:19 2014 +0900
@@ -17,8 +17,8 @@
handle_call({winning_hand, Hands}, _From, State) ->
{reply, rps_rules:winning_hand(Hands, State), State};
-handle_call({why, H1, H2}, _From, State=#ruleinfo{why=Why}) ->
- {reply, dict:fetch({H1, H2}, Why), State}.
+handle_call({why, H1, H2}, _From, State=#ruleinfo{}) ->
+ {reply, rps_rules:why(H1, H2, State), State}.
handle_cast(_Msg, State) -> {noreply, State}.
--- a/src/rps5.erl Sat Mar 08 23:36:15 2014 +0900
+++ b/src/rps5.erl Sat Mar 08 23:37:19 2014 +0900
@@ -17,8 +17,8 @@
handle_call({winning_hand, Hands}, _From, State) ->
{reply, rps_rules:winning_hand(Hands, State), State};
-handle_call({why, H1, H2}, _From, State=#ruleinfo{why=Why}) ->
- {reply, dict:fetch({H1, H2}, Why), State}.
+handle_call({why, H1, H2}, _From, State=#ruleinfo{}) ->
+ {reply, rps_rules:why(H1, H2, State), State}.
handle_cast(_Msg, State) -> {noreply, State}.
--- a/src/rps_rules.erl Sat Mar 08 23:36:15 2014 +0900
+++ b/src/rps_rules.erl Sat Mar 08 23:37:19 2014 +0900
@@ -1,5 +1,5 @@
-module(rps_rules).
--export([parse/1, winning_hand/2]).
+-export([parse/1, winning_hand/2, why/3]).
-include("rps_rules.hrl").
add_unique(Elem, List) ->
@@ -39,3 +39,10 @@
true -> Hand
end
end, undefined, HSet).
+
+why(H1, H2, State=#ruleinfo{}) when is_atom(H2) ->
+ atom_to_list(dict:fetch({H1, H2}, State#ruleinfo.why));
+
+why(H1, Hands, State=#ruleinfo{}) when is_list(Hands) ->
+ F = fun(H2) -> why(H1, H2, State) ++ " " ++ atom_to_list(H2) end,
+ atom_to_list(H1) ++ " " ++ string:join(lists:map(F, Hands), ", ").