2 -behaviour(gen_server).
20 -define(INTERVAL, 100). % In milliseconds
22 -define(CHAR_DEAD, 32). % " "
23 -define(CHAR_ALIVE, 111). % "o"
24 -define(CHAR_BAR, 45). % "-"
27 -record(state, {x :: integer()
29 ,cells :: list(atom())
30 ,num_cells :: integer()
31 ,num_dead :: integer()
32 ,num_alive :: integer()
33 ,state_pairs :: list(tuple(integer(), integer())) | []
34 ,replies_pending :: integer()
36 ,gen_began :: erlang:timestamp()
40 %% ============================================================================
42 %% ============================================================================
44 start_link(X, Y, Cells) ->
45 ServerName = {local, ?MODULE},
48 gen_server:start_link(ServerName, ?MODULE, Args, Opts).
51 report_state(CellID, GenID, CellState) ->
52 gen_server:cast(?MODULE, {report_state, {CellID, GenID, CellState}}).
55 %% ============================================================================
57 %% ============================================================================
59 handle_call(_Msg, _From, State) -> {reply, ok, State}.
60 code_change(_Old, State, _Other) -> {ok, State}.
61 terminate(_Reason, State) -> {ok, State}.
64 %% ============================================================================
66 %% ============================================================================
68 init([X, Y, Cells]) ->
72 ,num_cells = length(Cells)
77 ok = schedule_next_gen(),
88 GenBegan = os:timestamp(),
90 ok = life_lib:cast_one2all(Cells, {next_gen, NewGenID}),
91 NewState = State#state{replies_pending=NumCells
99 handle_info(_Msg, State) ->
103 handle_cast({report_state, {CellID, GenID, CellState}},
108 ,state_pairs=StatePairs
109 ,replies_pending=RepliesPending
115 NewStatePairs = [{CellID, CellState} | StatePairs],
116 NewRepliesPending = RepliesPending - 1,
117 {NewNDead, NewNAlive} = increment_dead_or_alive(CellState, NDead, NAlive),
118 NewState = State#state{replies_pending=NewRepliesPending
123 case NewRepliesPending of
125 SortedStatePairs = lists:sort(
126 fun({A, _}, {B, _}) -> A < B end,
129 StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
131 GenDuration = timer:now_diff(os:timestamp(), GenBegan) / 1000000,
133 ok = life_observer:log_generation(GenID, GenDuration, NewNDead, NewNAlive),
136 "X: ~b Y: ~b CELLS: ~b DEAD: ~b ALIVE: ~b GENERATION: ~b DURATION: ~f~n",
137 [X, Y, NumCells, NewNDead, NewNAlive, GenID, GenDuration]
139 ok = do_print_bar(X),
140 ok = do_print_state_chars(X, StateChars),
142 ok = schedule_next_gen(),
143 {noreply, NewState#state{state_pairs=[]}};
146 {noreply, NewState#state{state_pairs=NewStatePairs}}
149 handle_cast(_Msg, State) ->
153 %% ============================================================================
155 %% ============================================================================
157 increment_dead_or_alive(0, NDead, NAlive) -> {NDead + 1, NAlive};
158 increment_dead_or_alive(1, NDead, NAlive) -> {NDead, NAlive + 1}.
161 schedule_next_gen() ->
162 erlang:send_after(?INTERVAL, self(), next_gen),
166 state_to_char(0) -> ?CHAR_DEAD;
167 state_to_char(1) -> ?CHAR_ALIVE.
170 do_print_state_chars(_, []) -> ok;
171 do_print_state_chars(X, Chars) ->
172 {XChars, RestChars} = lists:split(X, Chars),
173 ok = io:format([XChars, $\n]),
174 do_print_state_chars(X, RestChars).
178 io:format("~s~n", [[?CHAR_BAR || _ <- lists:seq(1, X - 1)]]).