Download:
child 7:14b33824d432
parent 5:0f9f2e3d2609
6:bb8fdb49d6ba
Anton Shestakov <engored@ya.ru>, Sun, 23 Feb 2014 16:38:28 +0900
Improve counter and stats format.

1 файлов изменено, 16 вставок(+), 11 удалений(-) [+]
rps.erl file | annotate | diff | comparison | revisions
--- a/rps.erl Sun Feb 23 15:02:21 2014 +0900
+++ b/rps.erl Sun Feb 23 16:38:28 2014 +0900
@@ -1,5 +1,9 @@
-module(rps).
--export([lobby/0, counter/2, printer/0, room/1, beats/1, winner/1, go/0, fool/0]).
+-export([
+ go/0,
+ beats/1, winner/1,
+ lobby/0, counter/0, printer/0, room/1,
+ fool/0]).
-define(RULES, [rock, paper, scissors]).
-define(BOTS, [fool, fool, fool]).
@@ -40,7 +44,7 @@
go() ->
register(lobby, spawn(rps, lobby, [])),
- register(counter, spawn(rps, counter, [[], []])),
+ register(counter, spawn(rps, counter, [])),
register(printer, spawn(rps, printer, [])),
[spawn(rps, Bot, []) || Bot <- ?BOTS].
@@ -58,17 +62,18 @@
lobby([Who|Players])
end.
-counter(Draws, Wins) ->
+counter() ->
+ counter(0, 0, dict:new()).
+
+counter(Plays, Draws, Wins) ->
receive
- {draw, Choices} ->
- %io:format("draw: ~w.~n", [Choices]),
- counter([Choices|Draws], Wins);
- {winner, Winner} ->
- %io:format("the winner is ~w.~n", [Winner]),
- counter(Draws, [Winner|Wins]);
+ {draw, _} ->
+ counter(Plays + 1, Draws + 1, Wins);
+ {winner, {_, Winner}} ->
+ counter(Plays + 1, Draws, dict:update_counter(Winner, 1, Wins));
{sendstats, Pid} ->
- Pid ! {stats, {draws, length(Draws), wins, length(Wins)}},
- counter(Draws, Wins)
+ Pid ! {stats, {plays, Plays, draws, Draws, wins, dict:to_list(Wins)}},
+ counter(Plays, Draws, Wins)
end.
printer() ->