--- a/src/rps.erl Mon Mar 03 15:09:55 2014 +0900
+++ b/src/rps.erl Mon Mar 03 15:37:24 2014 +0900
lobby/0, counter/0, printer/0]).
-define(BOTS, [fool, copycat, gambler]).
rps_nickserv:start_link(),
lobby(Players) when length(Players) =:= length(?BOTS) ->
- Room = spawn_link(rps_room, room, [Players]),
- lists:foreach(fun(Player) -> Player ! {room, Room} end, Players),
+ Room = spawn_link(rps_room, room, [?RULES, Players]),
+ lists:foreach(fun(Player) -> Player ! {room, Room, ?RULES} end, Players),
--- a/src/rps_bots.erl Mon Mar 03 15:09:55 2014 +0900
+++ b/src/rps_bots.erl Mon Mar 03 15:37:24 2014 +0900
--import(rps3, [get_rules/0]).
-export([fool/0, copycat/0, gambler/0]).
+ Hands = apply(Rules, get_rules, []),
- Hand = lists:nth(random:uniform(length(get_rules())), get_rules()),
+ Hand = lists:nth(random:uniform(length(Hands)), Hands),
- copycat(Room, hd(get_rules()))
+ copycat(Room, hd(apply(Rules, get_rules, [])))
- Wins = dict:from_list([{C, 0} || C <- get_rules()]),
+ R = apply(Rules, get_rules, []),
+ Wins = dict:from_list([{C, 0} || C <- R]),
--- a/src/rps_room.erl Mon Mar 03 15:09:55 2014 +0900
+++ b/src/rps_room.erl Mon Mar 03 15:37:24 2014 +0900
--import(rps3, [get_rules/0, winning_hand/1]).
--record(state, {players=[], choices=[]}).
+-record(state, {rules, players=[], choices=[]}).
% winner for 3 players is:
% rock, rock, rock -> undefined
% rock, paper, paper -> undefined
% rock, rock, paper -> {paper, Who}
-winner(Choices) when length(Choices) > 1 ->
+winner(Rules, Choices) when length(Choices) > 1 ->
PlayersByHand = lists:foldl(
dict:append(Hand, Who, Acc)
end, dict:new(), Choices),
- WHand = winning_hand([Hand || {Hand, _} <- Choices]),
+ WHand = apply(Rules, winning_hand, [[Hand || {Hand, _} <- Choices]]),
WHand =:= undefined -> [];
WHand =/= undefined -> dict:fetch(WHand, PlayersByHand)
-room(Players) when is_list(Players) ->
+room(Rules, Players) when is_list(Players) ->
lists:foreach(fun(Player) -> Player ! start end, Players),
- room(#state{players=Players});
+ room(#state{rules=Rules, players=Players}).
-room(#state{players=Players, choices=Choices}) when length(Players) =:= length(Choices) ->
- Winner = winner(Choices),
+room(#state{rules=Rules, players=Players, choices=Choices}) when length(Players) =:= length(Choices) ->
+ Winner = winner(Rules, Choices),
lists:foreach(fun(Player) -> Player ! Msg end, Players),
-room(State=#state{choices=Choices}) ->
+room(State=#state{rules=Rules, choices=Choices}) ->
- ObjectIsOk = lists:any(fun(X) -> X =:= Hand end, get_rules()),
+ R = apply(Rules, get_rules, []),
+ ObjectIsOk = lists:any(fun(X) -> X =:= Hand end, R),
ChoiceMade = lists:keymember(Who, 2, Choices),
case ObjectIsOk andalso not ChoiceMade of