2 -behaviour(gen_server).
20 -define(INTERVAL, 100). % In milliseconds
22 -define(CHAR_DEAD, 32). % Space
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()
39 %% ============================================================================
41 %% ============================================================================
43 start_link(X, Y, Cells) ->
44 ServerName = {local, ?MODULE},
47 gen_server:start_link(ServerName, ?MODULE, Args, Opts).
50 report_state(CellID, GenID, CellState) ->
51 gen_server:cast(?MODULE, {report_state, {CellID, GenID, CellState}}).
54 %% ============================================================================
56 %% ============================================================================
58 init([X, Y, Cells]) ->
62 ,num_cells = length(Cells)
67 ok = schedule_next_gen(),
71 terminate(_Reason, State) ->
75 code_change(_Old, State, _Other) ->
79 handle_call(_Msg, _From, State) ->
91 ok = cast_all(Cells, {next_gen, NewGenID}),
92 NewState = State#state{replies_pending=NumCells
99 handle_cast({report_state, {CellID, GenID, CellState}},
104 ,state_pairs=StatePairs
105 ,replies_pending=RepliesPending
110 NewStatePairs = [{CellID, CellState} | StatePairs],
111 NewRepliesPending = RepliesPending - 1,
112 {NewNDead, NewNAlive} = increment_dead_or_alive(CellState, NDead, NAlive),
113 NewState = State#state{replies_pending=NewRepliesPending
118 case NewRepliesPending of
120 SortedStatePairs = lists:sort(
121 fun({A, _}, {B, _}) -> A < B end,
124 StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
127 "X: ~b Y: ~b CELLS: ~b DEAD: ~b ALIVE: ~b GENERATION: ~b~n",
128 [X, Y, NumCells, NewNDead, NewNAlive, GenID]
130 ok = do_print_bar(X),
131 ok = do_print_state_chars(X, StateChars),
132 ok = timer:sleep(?INTERVAL),
133 ok = schedule_next_gen(),
134 {noreply, NewState#state{state_pairs=[]}};
137 {noreply, NewState#state{state_pairs=NewStatePairs}}
140 handle_cast(_Msg, State) ->
144 handle_info(_Msg, State) ->
148 %% ============================================================================
150 %% ============================================================================
152 increment_dead_or_alive(0, NDead, NAlive) -> {NDead + 1, NAlive};
153 increment_dead_or_alive(1, NDead, NAlive) -> {NDead, NAlive + 1}.
156 schedule_next_gen() ->
157 ok = gen_server:cast(?MODULE, next_gen).
160 cast_all([], _) -> ok;
161 cast_all([Server | Servers], Msg) ->
162 ok = gen_server:cast(Server, Msg),
163 cast_all(Servers, Msg).
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)]]).