--- a/src/rps.erl Mon Mar 03 22:17:50 2014 +0900
+++ b/src/rps.erl Mon Mar 03 22:19:53 2014 +0900
lobby/0, counter/0, printer/0]).
-define(BOTS, [fool, copycat, gambler]).
rps_nickserv: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
- winning_hand/1, test_winning_hand/0]).
-get_hands() -> [rock, paper, scissors, spock, lizard].
+-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").
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+ 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)}.
+handle_call(get_hands, _From, State=#ruleinfo{hands=Hands}) ->
+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_cast(_Msg, State) -> {noreply, State}.
- io:format("~w beats ~w.~n", [H, beats(H)])
+handle_info(_Msg, State) -> {noreply, State}.
+terminate(_Reason, _State) -> ok.
- io:format("~w loses to ~w.~n", [H, loses(H)])
+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
+ gen_server:call(?MODULE, get_hands).
- HSet = sets:from_list(Hands),
- case sets:is_disjoint(sets:from_list(Loses), HSet) of
+ gen_server:call(?MODULE, {winning_hand, Hands}).
- Exclude = fun(Hand) -> lists:delete(Hand, get_hands()) end,
- [rock, paper, scissors],
- |lists:map(Exclude, get_hands())
- Winner = winning_hand(Hands),
- io:format("~w wins in ~w.~n", [Winner, Hands])
+ 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
+-include_lib("eunit/include/eunit.hrl").
+ ?assertEqual(5, length(rps5:get_hands())).
+ ?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])).