Added generation count to time and cell states.
[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
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()
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()
a88b2916 33 ,generation = 0 :: 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
172421cb
SK
48tock(CellID, CellState) ->
49 gen_server:cast(?MODULE, {tock, {CellID, CellState}}).
d2a0e2f9
SK
50
51
52%% ============================================================================
53%% Callbacks
54%% ============================================================================
55
1aa9333c 56init([X, Y, Cells]) ->
d2a0e2f9 57 State = #state{x=X
1aa9333c 58 ,y=Y
d2a0e2f9
SK
59 ,cells=Cells
60 ,num_cells=length(Cells)
61 ,state_pairs=[]
62 ,replies_pending=0
63 },
172421cb 64 schedule_next_tick(),
d2a0e2f9
SK
65 {ok, State}.
66
67
68terminate(_Reason, State) ->
69 {ok, State}.
70
71
72code_change(_Old, State, _Other) ->
73 {ok, State}.
74
75
76handle_call(_Msg, _From, State) ->
77 {reply, ok, State}.
78
79
172421cb
SK
80handle_cast(next_tick,
81 #state{cells=Cells
82 ,num_cells=NumCells
83 ,state_pairs=[]
a88b2916 84 ,generation=Generation
172421cb
SK
85 }=State) ->
86
a88b2916
SK
87 NewGeneration = Generation + 1,
88 ok = cast_all(Cells, {tick, NewGeneration}),
89 {noreply, State#state{replies_pending=NumCells, generation=NewGeneration}};
d2a0e2f9
SK
90
91handle_cast({tock, {ID, CellState}},
92 #state{x=X
1aa9333c 93 ,y=Y
d2a0e2f9
SK
94 ,state_pairs=StatePairs
95 ,replies_pending=RepliesPending
a88b2916 96 ,generation=Generation
812a14ea 97 ,num_cells=NumCells
d2a0e2f9
SK
98 }=State) ->
99
100 NewStatePairs = [{ID, CellState} | StatePairs],
101 NewRepliesPending = RepliesPending - 1,
102 NewState = State#state{replies_pending=NewRepliesPending},
103
104 case NewRepliesPending of
105 0 ->
3803d7be
SK
106 SortedStatePairs = lists:sort(
107 fun({A, _}, {B, _}) -> A < B end,
108 NewStatePairs
109 ),
d2a0e2f9 110 StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
eec4cd1f 111
812a14ea 112 ok = io:format(
a88b2916
SK
113 "X: ~b Y: ~b CELLS: ~b GENERATION: ~b~n",
114 [X, Y, NumCells, Generation]
812a14ea 115 ),
f7349482 116 ok = do_print_bar(X),
eec4cd1f 117
d2a0e2f9 118 ok = do_print_state_chars(X, StateChars),
eec4cd1f 119
d2a0e2f9 120 ok = timer:sleep(?INTERVAL),
172421cb 121 schedule_next_tick(),
a88b2916 122 {noreply, NewState#state{state_pairs=[]}};
d2a0e2f9
SK
123
124 _N ->
125 {noreply, NewState#state{state_pairs=NewStatePairs}}
126 end;
127
128handle_cast(_Msg, State) ->
129 {noreply, State}.
130
131
132handle_info(_Msg, State) ->
133 {noreply, State}.
134
135
136%% ============================================================================
137%% Internal
138%% ============================================================================
139
172421cb
SK
140schedule_next_tick() ->
141 gen_server:cast(?MODULE, next_tick).
142
143
144cast_all([], _) -> ok;
145cast_all([Server | Servers], Msg) ->
146 ok = gen_server:cast(Server, Msg),
147 cast_all(Servers, Msg).
d2a0e2f9
SK
148
149
150state_to_char(0) -> ?CHAR_DEAD;
151state_to_char(1) -> ?CHAR_ALIVE.
152
153
154do_print_state_chars(_, []) -> ok;
155do_print_state_chars(X, Chars) ->
156 {XChars, RestChars} = lists:split(X, Chars),
157 ok = io:format([XChars, $\n]),
158 do_print_state_chars(X, RestChars).
159
160
161do_print_bar(X) ->
162 io:format("~s~n", [[?CHAR_BAR || _ <- lists:seq(1, X - 1)]]).
This page took 0.030542 seconds and 4 git commands to generate.