Download:
child 43:a8365c989b1c
parent 41:f32e096c5433
42:82a465d8a5c1
Anton Shestakov <engored@ya.ru>, Mon, 03 Mar 2014 22:19:53 +0900
rps5 as a gen_server.

3 файлов изменено, 41 вставок(+), 73 удалений(-) [+]
src/rps.erl file | annotate | diff | comparison | revisions
src/rps5.erl file | annotate | diff | comparison | revisions
test/rps5_tests.erl file | annotate | diff | comparison | revisions
--- a/src/rps.erl Mon Mar 03 22:17:50 2014 +0900
+++ b/src/rps.erl Mon Mar 03 22:19:53 2014 +0900
@@ -4,11 +4,12 @@
lobby/0, counter/0, printer/0]).
-define(BOTS, [fool, copycat, gambler]).
--define(RULES, rps3).
+-define(RULES, rps5).
go() ->
rps_nickserv:start_link(),
rps3:start_link(),
+ rps5:start_link(),
register(lobby, spawn_link(?MODULE, lobby, [])),
register(counter, spawn_link(?MODULE, counter, [])),
register(printer, spawn_link(?MODULE, printer, [])),
--- a/src/rps5.erl Mon Mar 03 22:17:50 2014 +0900
+++ b/src/rps5.erl Mon Mar 03 22:19:53 2014 +0900
@@ -1,85 +1,38 @@
-module(rps5).
--export([
- get_hands/0,
- beats/1, test_beats/0,
- loses/1, test_loses/0,
- winning_hand/1, test_winning_hand/0]).
-
-get_hands() -> [rock, paper, scissors, spock, lizard].
-
-beats(rock) ->
- [scissors, lizard];
+-behaviour(gen_server).
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
+-export([start_link/0, get_hands/0, winning_hand/1, why/2]).
+-include("rps_rules.hrl").
-beats(paper) ->
- [spock, rock];
-
-beats(scissors) ->
- [lizard, paper];
+start_link() ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
-beats(spock) ->
- [rock, scissors];
+init([]) ->
+ Text = "scissors cut paper covers rock crushes lizard poisons spock smashes scissors decapitate lizard eats paper disproves spock vaporizes rock crushes scissors",
+ {ok, rps_rules:parse(Text)}.
-beats(lizard) ->
- [paper, spock].
-
-loses(lizard) ->
- [scissors, rock];
+handle_call(get_hands, _From, State=#ruleinfo{hands=Hands}) ->
+ {reply, Hands, State};
-loses(spock) ->
- [paper, lizard];
-
-loses(scissors) ->
- [rock, spock];
+handle_call({winning_hand, Hands}, _From, State) ->
+ {reply, rps_rules:winning_hand(Hands, State), State};
-loses(paper) ->
- [lizard, scissors];
+handle_call({why, H1, H2}, _From, State=#ruleinfo{why=Why}) ->
+ {reply, dict:fetch({H1, H2}, Why), State}.
-loses(rock) ->
- [spock, paper].
+handle_cast(_Msg, State) -> {noreply, State}.
-test_beats() ->
- lists:foreach(
- fun(H) ->
- io:format("~w beats ~w.~n", [H, beats(H)])
- end,
- get_hands()).
+handle_info(_Msg, State) -> {noreply, State}.
+
+terminate(_Reason, _State) -> ok.
-test_loses() ->
- lists:foreach(
- fun(H) ->
- io:format("~w loses to ~w.~n", [H, loses(H)])
- end,
- get_hands()).
+code_change(_OldVersion, State, _Extra) -> {ok, State}.
-% winner for 3 hands is:
-% rock, rock, rock -> rock
-% rock, paper, scissors -> undefined
-% rock, paper, paper -> paper
-% rock, rock, paper -> paper
+get_hands() ->
+ gen_server:call(?MODULE, get_hands).
winning_hand(Hands) ->
- HSet = sets:from_list(Hands),
- sets:fold(
- fun(Hand, Acc) ->
- Loses = loses(Hand),
- case sets:is_disjoint(sets:from_list(Loses), HSet) of
- true -> Hand;
- false -> Acc
- end
- end, undefined, HSet).
+ gen_server:call(?MODULE, {winning_hand, Hands}).
-test_winning_hand() ->
- Exclude = fun(Hand) -> lists:delete(Hand, get_hands()) end,
- Cases = [
- [rock, rock, rock],
- [rock, paper, scissors],
- [rock, paper, paper],
- [rock, rock, paper]
- |lists:map(Exclude, get_hands())
- ],
- lists:foreach(
- fun(Hands) ->
- Winner = winning_hand(Hands),
- io:format("~w wins in ~w.~n", [Winner, Hands])
- end,
- Cases).
+why(H1, H2) ->
+ gen_server:call(?MODULE, {why, H1, H2}).
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/rps5_tests.erl Mon Mar 03 22:19:53 2014 +0900
@@ -0,0 +1,14 @@
+-module(rps5_tests).
+-include_lib("eunit/include/eunit.hrl").
+
+get_hands_test() ->
+ ?assertEqual(5, length(rps5:get_hands())).
+
+winning_hand_test() ->
+ ?assertEqual(paper, rps5:winning_hand([rock, paper])),
+ ?assertEqual(scissors, rps5:winning_hand([paper, scissors])),
+ ?assertEqual(rock, rps5:winning_hand([scissors, rock])),
+ ?assertEqual(rock, rps5:winning_hand([rock, rock, rock])),
+ ?assertEqual(undefined, rps5:winning_hand([rock, paper, scissors])),
+ ?assertEqual(paper, rps5:winning_hand([rock, paper, paper])),
+ ?assertEqual(paper, rps5:winning_hand([rock, rock, paper])).