Explicitly matching casts with 'ok'.
[cellular-automata.git] / 001 / src / life_time.erl
... / ...
CommitLineData
1-module(life_time).
2-behaviour(gen_server).
3
4
5%% API
6-export([start_link/3
7 ,report_state/3
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 ,y :: integer()
29 ,cells :: list(atom())
30 ,num_cells :: integer()
31 ,state_pairs :: list(tuple(integer(), integer())) | []
32 ,replies_pending :: integer()
33 ,gen_id :: integer()
34 }).
35
36
37%% ============================================================================
38%% API
39%% ============================================================================
40
41start_link(X, Y, Cells) ->
42 ServerName = {local, ?MODULE},
43 Args = [X, Y, Cells],
44 Opts = [],
45 gen_server:start_link(ServerName, ?MODULE, Args, Opts).
46
47
48report_state(CellID, GenID, CellState) ->
49 gen_server:cast(?MODULE, {report_state, {CellID, GenID, CellState}}).
50
51
52%% ============================================================================
53%% Callbacks
54%% ============================================================================
55
56init([X, Y, Cells]) ->
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
64 },
65 ok = schedule_next_gen(),
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
81handle_cast(next_gen,
82 #state{cells=Cells
83 ,num_cells=NumCells
84 ,state_pairs=[]
85 ,gen_id=GenID
86 }=State) ->
87
88 NewGenID = GenID + 1,
89 ok = cast_all(Cells, {next_gen, NewGenID}),
90 {noreply, State#state{replies_pending=NumCells, gen_id=NewGenID}};
91
92handle_cast({report_state, {CellID, GenID, CellState}},
93 #state{x=X
94 ,y=Y
95 ,state_pairs=StatePairs
96 ,replies_pending=RepliesPending
97 ,gen_id=GenID
98 ,num_cells=NumCells
99 }=State) ->
100
101 NewStatePairs = [{CellID, CellState} | StatePairs],
102 NewRepliesPending = RepliesPending - 1,
103 NewState = State#state{replies_pending=NewRepliesPending},
104
105 case NewRepliesPending of
106 0 ->
107 SortedStatePairs = lists:sort(
108 fun({A, _}, {B, _}) -> A < B end,
109 NewStatePairs
110 ),
111 StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
112
113 ok = io:format(
114 "X: ~b Y: ~b CELLS: ~b GENERATION: ~b~n",
115 [X, Y, NumCells, GenID]
116 ),
117 ok = do_print_bar(X),
118 ok = do_print_state_chars(X, StateChars),
119 ok = timer:sleep(?INTERVAL),
120 ok = schedule_next_gen(),
121 {noreply, NewState#state{state_pairs=[]}};
122
123 _N ->
124 {noreply, NewState#state{state_pairs=NewStatePairs}}
125 end;
126
127handle_cast(_Msg, State) ->
128 {noreply, State}.
129
130
131handle_info(_Msg, State) ->
132 {noreply, State}.
133
134
135%% ============================================================================
136%% Internal
137%% ============================================================================
138
139schedule_next_gen() ->
140 ok = gen_server:cast(?MODULE, next_gen).
141
142
143cast_all([], _) -> ok;
144cast_all([Server | Servers], Msg) ->
145 ok = gen_server:cast(Server, Msg),
146 cast_all(Servers, Msg).
147
148
149state_to_char(0) -> ?CHAR_DEAD;
150state_to_char(1) -> ?CHAR_ALIVE.
151
152
153do_print_state_chars(_, []) -> ok;
154do_print_state_chars(X, Chars) ->
155 {XChars, RestChars} = lists:split(X, Chars),
156 ok = io:format([XChars, $\n]),
157 do_print_state_chars(X, RestChars).
158
159
160do_print_bar(X) ->
161 io:format("~s~n", [[?CHAR_BAR || _ <- lists:seq(1, X - 1)]]).
This page took 0.024356 seconds and 4 git commands to generate.