Some terminology changes.
[cellular-automata.git] / 001 / src / life_time.erl
CommitLineData
982ec720 1-module(life_time).
d2a0e2f9
SK
2-behaviour(gen_server).
3
4
5%% API
1aa9333c 6-export([start_link/3
0a9b8c17 7 ,report_state/2
d2a0e2f9
SK
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
a43ebd2e 20-define(INTERVAL, 100). % In milliseconds
d2a0e2f9
SK
21
22-define(CHAR_DEAD, 32). % Space
23-define(CHAR_ALIVE, 111). % o
eec4cd1f 24-define(CHAR_BAR, 45). % -
d2a0e2f9
SK
25
26
27-record(state, {x :: integer()
1aa9333c 28 ,y :: integer()
d2a0e2f9
SK
29 ,cells :: list(atom())
30 ,num_cells :: integer()
31 ,state_pairs :: list(tuple(integer(), integer())) | []
32 ,replies_pending :: integer()
0a9b8c17 33 ,gen_id :: integer()
d2a0e2f9
SK
34 }).
35
36
37%% ============================================================================
38%% API
39%% ============================================================================
40
1aa9333c 41start_link(X, Y, Cells) ->
d2a0e2f9 42 ServerName = {local, ?MODULE},
1aa9333c 43 Args = [X, Y, Cells],
d2a0e2f9
SK
44 Opts = [],
45 gen_server:start_link(ServerName, ?MODULE, Args, Opts).
46
47
0a9b8c17
SK
48report_state(CellID, CellState) ->
49 gen_server:cast(?MODULE, {report_state, {CellID, CellState}}).
d2a0e2f9
SK
50
51
52%% ============================================================================
53%% Callbacks
54%% ============================================================================
55
1aa9333c 56init([X, Y, Cells]) ->
0a9b8c17
SK
57 State = #state{x = X
58 ,y = Y
59 ,cells = Cells
60 ,num_cells = length(Cells)
61 ,state_pairs = []
62 ,replies_pending = 0
63 ,gen_id = 0
d2a0e2f9 64 },
0a9b8c17 65 schedule_next_gen(),
d2a0e2f9
SK
66 {ok, State}.
67
68
69terminate(_Reason, State) ->
70 {ok, State}.
71
72
73code_change(_Old, State, _Other) ->
74 {ok, State}.
75
76
77handle_call(_Msg, _From, State) ->
78 {reply, ok, State}.
79
80
0a9b8c17 81handle_cast(next_gen,
172421cb
SK
82 #state{cells=Cells
83 ,num_cells=NumCells
84 ,state_pairs=[]
0a9b8c17 85 ,gen_id=GenID
172421cb
SK
86 }=State) ->
87
0a9b8c17
SK
88 NewGenID = GenID + 1,
89 ok = cast_all(Cells, {next_gen, NewGenID}),
90 {noreply, State#state{replies_pending=NumCells, gen_id=NewGenID}};
d2a0e2f9 91
0a9b8c17 92handle_cast({report_state, {ID, CellState}},
d2a0e2f9 93 #state{x=X
1aa9333c 94 ,y=Y
d2a0e2f9
SK
95 ,state_pairs=StatePairs
96 ,replies_pending=RepliesPending
0a9b8c17 97 ,gen_id=GenID
812a14ea 98 ,num_cells=NumCells
d2a0e2f9
SK
99 }=State) ->
100
101 NewStatePairs = [{ID, CellState} | StatePairs],
102 NewRepliesPending = RepliesPending - 1,
103 NewState = State#state{replies_pending=NewRepliesPending},
104
105 case NewRepliesPending of
106 0 ->
3803d7be
SK
107 SortedStatePairs = lists:sort(
108 fun({A, _}, {B, _}) -> A < B end,
109 NewStatePairs
110 ),
d2a0e2f9 111 StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
eec4cd1f 112
812a14ea 113 ok = io:format(
a88b2916 114 "X: ~b Y: ~b CELLS: ~b GENERATION: ~b~n",
0a9b8c17 115 [X, Y, NumCells, GenID]
812a14ea 116 ),
f7349482 117 ok = do_print_bar(X),
eec4cd1f 118
d2a0e2f9 119 ok = do_print_state_chars(X, StateChars),
eec4cd1f 120
d2a0e2f9 121 ok = timer:sleep(?INTERVAL),
0a9b8c17 122 schedule_next_gen(),
a88b2916 123 {noreply, NewState#state{state_pairs=[]}};
d2a0e2f9
SK
124
125 _N ->
126 {noreply, NewState#state{state_pairs=NewStatePairs}}
127 end;
128
129handle_cast(_Msg, State) ->
130 {noreply, State}.
131
132
133handle_info(_Msg, State) ->
134 {noreply, State}.
135
136
137%% ============================================================================
138%% Internal
139%% ============================================================================
140
0a9b8c17
SK
141schedule_next_gen() ->
142 gen_server:cast(?MODULE, next_gen).
172421cb
SK
143
144
145cast_all([], _) -> ok;
146cast_all([Server | Servers], Msg) ->
147 ok = gen_server:cast(Server, Msg),
148 cast_all(Servers, Msg).
d2a0e2f9
SK
149
150
151state_to_char(0) -> ?CHAR_DEAD;
152state_to_char(1) -> ?CHAR_ALIVE.
153
154
155do_print_state_chars(_, []) -> ok;
156do_print_state_chars(X, Chars) ->
157 {XChars, RestChars} = lists:split(X, Chars),
158 ok = io:format([XChars, $\n]),
159 do_print_state_chars(X, RestChars).
160
161
162do_print_bar(X) ->
163 io:format("~s~n", [[?CHAR_BAR || _ <- lists:seq(1, X - 1)]]).
This page took 0.033687 seconds and 4 git commands to generate.