36:4d40c4b8e8b0
Anton Shestakov <engored@ya.ru>, Mon, 03 Mar 2014 20:00:26 +0900
Move parse_rules and winning_hand to rps_rules.

next change 37:674e9537b6ab
previous change 35:1963573a943e

src/rps3.erl

Permissions: -rw-r--r--

Other formats: Feeds:
-module(rps3).
-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]).
-record(state, {hands=[], beats=dict:new(), loses=dict:new(), why=dict:new()}).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
init([]) ->
Text = "scissors cut paper covers rock crushes scissors",
{ok, rps_rules:parse(Text)}.
handle_call(get_hands, _From, State=#state{hands=Hands}) ->
{reply, Hands, State};
% winner for 3 hands is:
% rock, rock, rock -> rock
% rock, paper, scissors -> undefined
% rock, paper, paper -> paper
% rock, rock, paper -> paper
handle_call({winning_hand, Hands}, _From, State) ->
{reply, rps_rules:winning_hand(Hands, State), State};
handle_call({why, H1, H2}, _From, State=#state{why=Why}) ->
{reply, dict:fetch({H1, H2}, Why), State}.
handle_cast(_Msg, State) -> {noreply, State}.
handle_info(_Msg, State) -> {noreply, State}.
terminate(_Reason, _State) -> ok.
code_change(_OldVersion, State, _Extra) -> {ok, State}.
get_hands() ->
gen_server:call(?MODULE, get_hands).
winning_hand(Hands) ->
gen_server:call(?MODULE, {winning_hand, Hands}).
why(H1, H2) ->
gen_server:call(?MODULE, {why, H1, H2}).