Download:
child 24:19a7ba960f8e
parent 22:8a7719ddce58
23:af20828828e5
Anton Shestakov <engored@ya.ru>, Sat, 01 Mar 2014 23:07:50 +0900
Separate rps_bots.erl.

2 файлов изменено, 67 вставок(+), 66 удалений(-) [+]
src/rps.erl file | annotate | diff | comparison | revisions
src/rps_bots.erl file | annotate | diff | comparison | revisions
--- a/src/rps.erl Sat Mar 01 23:02:31 2014 +0900
+++ b/src/rps.erl Sat Mar 01 23:07:50 2014 +0900
@@ -1,9 +1,7 @@
-module(rps).
--import(rps3, [get_rules/0]).
-export([
go/0,
- lobby/0, counter/0, printer/0,
- fool/0, copycat/0, gambler/0]).
+ lobby/0, counter/0, printer/0]).
-define(BOTS, [fool, copycat, gambler]).
@@ -11,7 +9,7 @@
register(lobby, spawn_link(?MODULE, lobby, [])),
register(counter, spawn_link(?MODULE, counter, [])),
register(printer, spawn_link(?MODULE, printer, [])),
- [spawn_link(?MODULE, Bot, []) || Bot <- ?BOTS].
+ [spawn_link(rps_bots, Bot, []) || Bot <- ?BOTS].
lobby() ->
lobby([]).
@@ -55,65 +53,3 @@
counter ! {sendstats, self()}
end,
printer().
-
-fool() ->
- lobby ! {join, self()},
- fool(joined).
-
-fool(joined) ->
- receive
- {room, Room} ->
- random:seed(now()),
- fool(Room)
- end;
-
-fool(Room) ->
- receive
- start ->
- Hand = lists:nth(random:uniform(length(get_rules())), get_rules()),
- Room ! {Hand, self()},
- fool(Room);
- {winner, _} ->
- fool(Room)
- end.
-
-copycat() ->
- lobby ! {join, self()},
- copycat(joined).
-
-copycat(joined) ->
- receive
- {room, Room} ->
- copycat(Room, hd(get_rules()))
- end.
-
-copycat(Room, Hand) ->
- receive
- start ->
- Room ! {Hand, self()},
- copycat(Room, Hand);
- {winner, {NewHand, _}} ->
- copycat(Room, NewHand)
- end.
-
-gambler() ->
- lobby ! {join, self()},
- gambler(joined).
-
-gambler(joined) ->
- receive
- {room, Room} ->
- Wins = dict:from_list([{C, 0} || C <- get_rules()]),
- gambler(Room, Wins)
- end.
-
-gambler(Room, Wins) ->
- receive
- start ->
- F = fun({_, A}, {_, B}) -> A =< B end,
- {Hand, _} = hd(lists:sort(F, dict:to_list(Wins))),
- Room ! {Hand, self()},
- gambler(Room, Wins);
- {winner, {Hand, _}} ->
- gambler(Room, dict:update_counter(Hand, 1, Wins))
- end.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/rps_bots.erl Sat Mar 01 23:07:50 2014 +0900
@@ -0,0 +1,65 @@
+-module(rps_bots).
+-import(rps3, [get_rules/0]).
+-export([fool/0, copycat/0, gambler/0]).
+
+fool() ->
+ lobby ! {join, self()},
+ fool(joined).
+
+fool(joined) ->
+ receive
+ {room, Room} ->
+ random:seed(now()),
+ fool(Room)
+ end;
+
+fool(Room) ->
+ receive
+ start ->
+ Hand = lists:nth(random:uniform(length(get_rules())), get_rules()),
+ Room ! {Hand, self()},
+ fool(Room);
+ {winner, _} ->
+ fool(Room)
+ end.
+
+copycat() ->
+ lobby ! {join, self()},
+ copycat(joined).
+
+copycat(joined) ->
+ receive
+ {room, Room} ->
+ copycat(Room, hd(get_rules()))
+ end.
+
+copycat(Room, Hand) ->
+ receive
+ start ->
+ Room ! {Hand, self()},
+ copycat(Room, Hand);
+ {winner, {NewHand, _}} ->
+ copycat(Room, NewHand)
+ end.
+
+gambler() ->
+ lobby ! {join, self()},
+ gambler(joined).
+
+gambler(joined) ->
+ receive
+ {room, Room} ->
+ Wins = dict:from_list([{C, 0} || C <- get_rules()]),
+ gambler(Room, Wins)
+ end.
+
+gambler(Room, Wins) ->
+ receive
+ start ->
+ F = fun({_, A}, {_, B}) -> A =< B end,
+ {Hand, _} = hd(lists:sort(F, dict:to_list(Wins))),
+ Room ! {Hand, self()},
+ gambler(Room, Wins);
+ {winner, {Hand, _}} ->
+ gambler(Room, dict:update_counter(Hand, 1, Wins))
+ end.