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 handle_call(_Msg, _From, State) -> {reply, ok, State}.
59 handle_info(_Msg, State) -> {noreply, 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(),
89 ok = cast_all(Cells, {next_gen, NewGenID}),
90 NewState = State#state{replies_pending=NumCells
97 handle_cast({report_state, {CellID, GenID, CellState}},
102 ,state_pairs=StatePairs
103 ,replies_pending=RepliesPending
108 NewStatePairs = [{CellID, CellState} | StatePairs],
109 NewRepliesPending = RepliesPending - 1,
110 {NewNDead, NewNAlive} = increment_dead_or_alive(CellState, NDead, NAlive),
111 NewState = State#state{replies_pending=NewRepliesPending
116 case NewRepliesPending of
118 SortedStatePairs = lists:sort(
119 fun({A, _}, {B, _}) -> A < B end,
122 StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
125 "X: ~b Y: ~b CELLS: ~b DEAD: ~b ALIVE: ~b GENERATION: ~b~n",
126 [X, Y, NumCells, NewNDead, NewNAlive, GenID]
128 ok = do_print_bar(X),
129 ok = do_print_state_chars(X, StateChars),
130 ok = timer:sleep(?INTERVAL),
131 ok = schedule_next_gen(),
132 {noreply, NewState#state{state_pairs=[]}};
135 {noreply, NewState#state{state_pairs=NewStatePairs}}
138 handle_cast(_Msg, State) ->
142 %% ============================================================================
144 %% ============================================================================
146 increment_dead_or_alive(0, NDead, NAlive) -> {NDead + 1, NAlive};
147 increment_dead_or_alive(1, NDead, NAlive) -> {NDead, NAlive + 1}.
150 schedule_next_gen() ->
151 ok = gen_server:cast(?MODULE, next_gen).
154 cast_all([], _) -> ok;
155 cast_all([Server | Servers], Msg) ->
156 ok = gen_server:cast(Server, Msg),
157 cast_all(Servers, Msg).
160 state_to_char(0) -> ?CHAR_DEAD;
161 state_to_char(1) -> ?CHAR_ALIVE.
164 do_print_state_chars(_, []) -> ok;
165 do_print_state_chars(X, Chars) ->
166 {XChars, RestChars} = lists:split(X, Chars),
167 ok = io:format([XChars, $\n]),
168 do_print_state_chars(X, RestChars).
172 io:format("~s~n", [[?CHAR_BAR || _ <- lists:seq(1, X - 1)]]).