Set interval back to 0.1 sec.
[cellular-automata.git] / 001 / src / life_time.erl
CommitLineData
982ec720 1-module(life_time).
d2a0e2f9
SK
2-behaviour(gen_server).
3
4
5%% API
6-export([start_link/2
172421cb 7 ,tock/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()
28 ,cells :: list(atom())
29 ,num_cells :: integer()
30 ,state_pairs :: list(tuple(integer(), integer())) | []
31 ,replies_pending :: integer()
f7349482 32 ,gen_count = 0 :: integer()
d2a0e2f9
SK
33 }).
34
35
36%% ============================================================================
37%% API
38%% ============================================================================
39
40start_link(X, Cells) ->
41 ServerName = {local, ?MODULE},
42 Args = [X, Cells],
43 Opts = [],
44 gen_server:start_link(ServerName, ?MODULE, Args, Opts).
45
46
172421cb
SK
47tock(CellID, CellState) ->
48 gen_server:cast(?MODULE, {tock, {CellID, CellState}}).
d2a0e2f9
SK
49
50
51%% ============================================================================
52%% Callbacks
53%% ============================================================================
54
55init([X, Cells]) ->
56 State = #state{x=X
57 ,cells=Cells
58 ,num_cells=length(Cells)
59 ,state_pairs=[]
60 ,replies_pending=0
61 },
172421cb 62 schedule_next_tick(),
d2a0e2f9
SK
63 {ok, State}.
64
65
66terminate(_Reason, State) ->
67 {ok, State}.
68
69
70code_change(_Old, State, _Other) ->
71 {ok, State}.
72
73
74handle_call(_Msg, _From, State) ->
75 {reply, ok, State}.
76
77
172421cb
SK
78handle_cast(next_tick,
79 #state{cells=Cells
80 ,num_cells=NumCells
81 ,state_pairs=[]
82 }=State) ->
83
84 ok = cast_all(Cells, tick),
d2a0e2f9
SK
85 {noreply, State#state{replies_pending=NumCells}};
86
87handle_cast({tock, {ID, CellState}},
88 #state{x=X
89 ,state_pairs=StatePairs
90 ,replies_pending=RepliesPending
f7349482 91 ,gen_count=GenCount
812a14ea 92 ,num_cells=NumCells
d2a0e2f9
SK
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 ->
f7349482 101 NewGenCount = GenCount + 1,
3803d7be
SK
102 SortedStatePairs = lists:sort(
103 fun({A, _}, {B, _}) -> A < B end,
104 NewStatePairs
105 ),
d2a0e2f9 106 StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
eec4cd1f 107
812a14ea
SK
108 ok = io:format(
109 "CELLS: ~b GENERATIONS: ~b~n",
110 [NumCells, NewGenCount]
111 ),
f7349482 112 ok = do_print_bar(X),
eec4cd1f 113
d2a0e2f9 114 ok = do_print_state_chars(X, StateChars),
eec4cd1f 115
d2a0e2f9 116 ok = timer:sleep(?INTERVAL),
172421cb 117 schedule_next_tick(),
f7349482 118 {noreply, NewState#state{state_pairs=[], gen_count=NewGenCount}};
d2a0e2f9
SK
119
120 _N ->
121 {noreply, NewState#state{state_pairs=NewStatePairs}}
122 end;
123
124handle_cast(_Msg, State) ->
125 {noreply, State}.
126
127
128handle_info(_Msg, State) ->
129 {noreply, State}.
130
131
132%% ============================================================================
133%% Internal
134%% ============================================================================
135
172421cb
SK
136schedule_next_tick() ->
137 gen_server:cast(?MODULE, next_tick).
138
139
140cast_all([], _) -> ok;
141cast_all([Server | Servers], Msg) ->
142 ok = gen_server:cast(Server, Msg),
143 cast_all(Servers, Msg).
d2a0e2f9
SK
144
145
146state_to_char(0) -> ?CHAR_DEAD;
147state_to_char(1) -> ?CHAR_ALIVE.
148
149
150do_print_state_chars(_, []) -> ok;
151do_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
157do_print_bar(X) ->
158 io:format("~s~n", [[?CHAR_BAR || _ <- lists:seq(1, X - 1)]]).
This page took 0.031277 seconds and 4 git commands to generate.