| 1 | -module(life_time). |
| 2 | -behaviour(gen_server). |
| 3 | |
| 4 | |
| 5 | %% API |
| 6 | -export([start_link/2 |
| 7 | ,tock/2 |
| 8 | ]). |
| 9 | |
| 10 | %% Callbacks |
| 11 | -export([init/1 |
| 12 | ,handle_call/3 |
| 13 | ,handle_cast/2 |
| 14 | ,handle_info/2 |
| 15 | ,terminate/2 |
| 16 | ,code_change/3 |
| 17 | ]). |
| 18 | |
| 19 | |
| 20 | -define(INTERVAL, 100). % In milliseconds |
| 21 | |
| 22 | -define(CHAR_DEAD, 32). % Space |
| 23 | -define(CHAR_ALIVE, 111). % o |
| 24 | -define(CHAR_BAR, 45). % - |
| 25 | |
| 26 | |
| 27 | -record(state, {x :: integer() |
| 28 | ,cells :: list(atom()) |
| 29 | ,num_cells :: integer() |
| 30 | ,state_pairs :: list(tuple(integer(), integer())) | [] |
| 31 | ,replies_pending :: integer() |
| 32 | ,gen_count = 0 :: integer() |
| 33 | }). |
| 34 | |
| 35 | |
| 36 | %% ============================================================================ |
| 37 | %% API |
| 38 | %% ============================================================================ |
| 39 | |
| 40 | start_link(X, Cells) -> |
| 41 | ServerName = {local, ?MODULE}, |
| 42 | Args = [X, Cells], |
| 43 | Opts = [], |
| 44 | gen_server:start_link(ServerName, ?MODULE, Args, Opts). |
| 45 | |
| 46 | |
| 47 | tock(CellID, CellState) -> |
| 48 | gen_server:cast(?MODULE, {tock, {CellID, CellState}}). |
| 49 | |
| 50 | |
| 51 | %% ============================================================================ |
| 52 | %% Callbacks |
| 53 | %% ============================================================================ |
| 54 | |
| 55 | init([X, Cells]) -> |
| 56 | State = #state{x=X |
| 57 | ,cells=Cells |
| 58 | ,num_cells=length(Cells) |
| 59 | ,state_pairs=[] |
| 60 | ,replies_pending=0 |
| 61 | }, |
| 62 | schedule_next_tick(), |
| 63 | {ok, State}. |
| 64 | |
| 65 | |
| 66 | terminate(_Reason, State) -> |
| 67 | {ok, State}. |
| 68 | |
| 69 | |
| 70 | code_change(_Old, State, _Other) -> |
| 71 | {ok, State}. |
| 72 | |
| 73 | |
| 74 | handle_call(_Msg, _From, State) -> |
| 75 | {reply, ok, State}. |
| 76 | |
| 77 | |
| 78 | handle_cast(next_tick, |
| 79 | #state{cells=Cells |
| 80 | ,num_cells=NumCells |
| 81 | ,state_pairs=[] |
| 82 | }=State) -> |
| 83 | |
| 84 | ok = cast_all(Cells, tick), |
| 85 | {noreply, State#state{replies_pending=NumCells}}; |
| 86 | |
| 87 | handle_cast({tock, {ID, CellState}}, |
| 88 | #state{x=X |
| 89 | ,state_pairs=StatePairs |
| 90 | ,replies_pending=RepliesPending |
| 91 | ,gen_count=GenCount |
| 92 | ,num_cells=NumCells |
| 93 | }=State) -> |
| 94 | |
| 95 | NewStatePairs = [{ID, CellState} | StatePairs], |
| 96 | NewRepliesPending = RepliesPending - 1, |
| 97 | NewState = State#state{replies_pending=NewRepliesPending}, |
| 98 | |
| 99 | case NewRepliesPending of |
| 100 | 0 -> |
| 101 | NewGenCount = GenCount + 1, |
| 102 | SortedStatePairs = lists:sort( |
| 103 | fun({A, _}, {B, _}) -> A < B end, |
| 104 | NewStatePairs |
| 105 | ), |
| 106 | StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs], |
| 107 | |
| 108 | ok = io:format( |
| 109 | "CELLS: ~b GENERATIONS: ~b~n", |
| 110 | [NumCells, NewGenCount] |
| 111 | ), |
| 112 | ok = do_print_bar(X), |
| 113 | |
| 114 | ok = do_print_state_chars(X, StateChars), |
| 115 | |
| 116 | ok = timer:sleep(?INTERVAL), |
| 117 | schedule_next_tick(), |
| 118 | {noreply, NewState#state{state_pairs=[], gen_count=NewGenCount}}; |
| 119 | |
| 120 | _N -> |
| 121 | {noreply, NewState#state{state_pairs=NewStatePairs}} |
| 122 | end; |
| 123 | |
| 124 | handle_cast(_Msg, State) -> |
| 125 | {noreply, State}. |
| 126 | |
| 127 | |
| 128 | handle_info(_Msg, State) -> |
| 129 | {noreply, State}. |
| 130 | |
| 131 | |
| 132 | %% ============================================================================ |
| 133 | %% Internal |
| 134 | %% ============================================================================ |
| 135 | |
| 136 | schedule_next_tick() -> |
| 137 | gen_server:cast(?MODULE, next_tick). |
| 138 | |
| 139 | |
| 140 | cast_all([], _) -> ok; |
| 141 | cast_all([Server | Servers], Msg) -> |
| 142 | ok = gen_server:cast(Server, Msg), |
| 143 | cast_all(Servers, Msg). |
| 144 | |
| 145 | |
| 146 | state_to_char(0) -> ?CHAR_DEAD; |
| 147 | state_to_char(1) -> ?CHAR_ALIVE. |
| 148 | |
| 149 | |
| 150 | do_print_state_chars(_, []) -> ok; |
| 151 | do_print_state_chars(X, Chars) -> |
| 152 | {XChars, RestChars} = lists:split(X, Chars), |
| 153 | ok = io:format([XChars, $\n]), |
| 154 | do_print_state_chars(X, RestChars). |
| 155 | |
| 156 | |
| 157 | do_print_bar(X) -> |
| 158 | io:format("~s~n", [[?CHAR_BAR || _ <- lists:seq(1, X - 1)]]). |