Converted to OTP app.
[cellular-automata.git] / 001 / src / time.erl
diff --git a/001/src/time.erl b/001/src/time.erl
new file mode 100644 (file)
index 0000000..d710926
--- /dev/null
@@ -0,0 +1,136 @@
+-module(time).
+-behaviour(gen_server).
+
+
+%% API
+-export([start_link/2
+        ,cast/1
+        ]).
+
+%% Callbacks
+-export([init/1
+        ,handle_call/3
+        ,handle_cast/2
+        ,handle_info/2
+        ,terminate/2
+        ,code_change/3
+        ]).
+
+
+-define(INTERVAL, 0).  % In milliseconds
+
+-define(CHAR_DEAD,   32).  % Space
+-define(CHAR_ALIVE, 111).  % o
+-define(CHAR_BAR,    61).  % =
+
+
+-record(state, {x               :: integer()
+               ,cells           :: list(atom())
+               ,num_cells       :: integer()
+               ,state_pairs     :: list(tuple(integer(), integer())) | []
+               ,replies_pending :: integer()
+               }).
+
+
+%% ============================================================================
+%% API
+%% ============================================================================
+
+start_link(X, Cells) ->
+    ServerName = {local, ?MODULE},
+    Args = [X, Cells],
+    Opts = [],
+    gen_server:start_link(ServerName, ?MODULE, Args, Opts).
+
+
+cast(Msg) ->
+    gen_server:cast(?MODULE, Msg).
+
+
+%% ============================================================================
+%% Callbacks
+%% ============================================================================
+
+init([X, Cells]) ->
+    State = #state{x=X
+                  ,cells=Cells
+                  ,num_cells=length(Cells)
+                  ,state_pairs=[]
+                  ,replies_pending=0
+                  },
+    cast(next_tick),
+    {ok, State}.
+
+
+terminate(_Reason, State) ->
+    {ok, State}.
+
+
+code_change(_Old, State, _Other) ->
+    {ok, State}.
+
+
+handle_call(_Msg, _From, State) ->
+    {reply, ok, State}.
+
+
+handle_cast(next_tick, #state{cells=Cells, num_cells=NumCells, state_pairs=[]}=State) ->
+    ok = send_all(Cells, tick),
+    {noreply, State#state{replies_pending=NumCells}};
+
+handle_cast({tock, {ID, CellState}},
+    #state{x=X
+          ,state_pairs=StatePairs
+          ,replies_pending=RepliesPending
+          }=State) ->
+
+    NewStatePairs = [{ID, CellState} | StatePairs],
+    NewRepliesPending = RepliesPending - 1,
+    NewState = State#state{replies_pending=NewRepliesPending},
+
+    case NewRepliesPending of
+        0 ->
+            SortedStatePairs = lists:sort(NewStatePairs),
+            StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
+            ok = do_print_bar(X),
+            ok = do_print_state_chars(X, StateChars),
+            ok = do_print_bar(X),
+            ok = timer:sleep(?INTERVAL),
+            cast(next_tick),
+            {noreply, NewState#state{state_pairs=[]}};
+
+        _N ->
+            {noreply, NewState#state{state_pairs=NewStatePairs}}
+    end;
+
+handle_cast(_Msg, State) ->
+    {noreply, State}.
+
+
+handle_info(_Msg, State) ->
+    {noreply, State}.
+
+
+%% ============================================================================
+%% Internal
+%% ============================================================================
+
+send_all([], _) -> ok;
+send_all([PID | PIDs], Msg) ->
+    PID ! Msg,
+    send_all(PIDs, Msg).
+
+
+state_to_char(0) -> ?CHAR_DEAD;
+state_to_char(1) -> ?CHAR_ALIVE.
+
+
+do_print_state_chars(_, []) -> ok;
+do_print_state_chars(X, Chars) ->
+    {XChars, RestChars} = lists:split(X, Chars),
+    ok = io:format([XChars, $\n]),
+    do_print_state_chars(X, RestChars).
+
+
+do_print_bar(X) ->
+    io:format("~s~n", [[?CHAR_BAR || _ <- lists:seq(1, X - 1)]]).
This page took 0.029538 seconds and 4 git commands to generate.