8 -define(DIRECTIONS, ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW']).
11 -define(INTERVAL, 0). % In milliseconds
12 -define(CHAR_DEAD, <<" ">>).
13 -define(CHAR_ALIVE, <<"o">>).
16 main( ) -> bang(?X, ?Y).
17 main([ ]) -> bang(?X, ?Y);
18 main([X ]) -> bang(list_to_integer(X), list_to_integer(X));
19 main([X, Y]) -> bang(list_to_integer(X), list_to_integer(Y)).
22 %% ============================================================================
24 %% ============================================================================
26 %% ----------------------------------------------------------------------------
28 %% ----------------------------------------------------------------------------
32 CellIDs = lists:seq(1, N),
38 integer_to_atom(neighbor_id(D, X, ID))
41 [{integer_to_atom(ID), Neighbors} | Pairs]
50 fun({ID, Neighbors}) ->
53 spawn(fun() -> cell(ID, Parent, Neighbors) end)
56 [{ID, filter_offsides(N, Neighbors)} || {ID, Neighbors} <- Graph]
59 CellNames = [integer_to_atom(ID) || ID <- CellIDs],
64 %% ----------------------------------------------------------------------------
66 %% ----------------------------------------------------------------------------
69 ok = send_all(Cells, {tick, self()}),
73 tock(X, All, Pending, StatePairs).
76 tock(X, All, [], StatePairs) ->
79 fun({_ID, State}, States) -> [State | States] end,
81 lists:sort(StatePairs)
83 ok = do_print_states(X, States),
84 ok = timer:sleep(?INTERVAL),
87 tock(X, All, Pending, StatePairs) ->
89 {tock, {ID, State}} ->
90 NewPending = lists:delete(ID, Pending),
91 NewStatePairs = [{ID, State} | StatePairs],
92 tock(X, All, NewPending, NewStatePairs)
96 %% ----------------------------------------------------------------------------
98 %% ----------------------------------------------------------------------------
101 cell(MyID, MyParent, MyNeighbors) ->
102 MyState = crypto:rand_uniform(0, 2),
103 cell(MyID, MyParent, MyNeighbors, MyState).
106 cell(MyID, MyParent, MyNeighbors, MyState) ->
109 ok = send_all(MyNeighbors, {request_state, MyID}),
110 cell(MyID, MyParent, MyNeighbors, MyState, {MyNeighbors, []})
114 % All neighbors replied
115 cell(MyID, MyParent, MyNeighbors, MyState, {[], States}) ->
116 LiveNeighbors = lists:sum(States),
117 MyNewState = new_state(MyState, LiveNeighbors),
118 MyParent ! {tock, {MyID, MyNewState}},
119 cell(MyID, MyParent, MyNeighbors, MyNewState);
121 % Awaiting requests and replies
122 cell(MyID, MyParent, MyNeighbors, MyState, {Pending, States}) ->
124 {request_state, ID} ->
125 ID ! {response_state, MyID, MyState},
126 cell(MyID, MyParent, MyNeighbors, MyState, {Pending, States});
128 {response_state, ID, State} ->
129 NewPending = lists:delete(ID, Pending),
130 NewStates = [State | States],
131 cell(MyID, MyParent, MyNeighbors, MyState, {NewPending, NewStates})
135 %% ============================================================================
137 %% ============================================================================
139 new_state(1, LiveNeighbors) when LiveNeighbors < 2 -> 0;
140 new_state(1, LiveNeighbors) when LiveNeighbors < 4 -> 1;
141 new_state(1, LiveNeighbors) when LiveNeighbors > 3 -> 0;
142 new_state(0, LiveNeighbors) when LiveNeighbors =:= 3 -> 1;
143 new_state(State, _LiveNeighbors) -> State.
146 neighbor_id(Direction, X, I) ->
147 I + offset(Direction, X).
150 offset('N' , X) -> -X;
151 offset('NE', X) -> -(X - 1);
152 offset('E' , _) -> 1;
153 offset('SE', X) -> X + 1;
154 offset('S' , X) -> X;
155 offset('SW', X) -> X - 1;
156 offset('W' , _) -> -1;
157 offset('NW', X) -> -(X + 1).
160 filter_offsides(N, IDs) ->
161 [ID || ID <- IDs, is_onside(N, atom_to_integer(ID))].
164 is_onside(_, ID) when ID < 1 -> false;
165 is_onside(N, ID) when ID > N -> false;
166 is_onside(_, _) -> true.
169 %% ============================================================================
171 %% ============================================================================
173 atom_to_integer(Atom) ->
174 list_to_integer(atom_to_list(Atom)).
177 integer_to_atom(Integer) ->
178 list_to_atom(integer_to_list(Integer)).
181 send_all([], _) -> ok;
182 send_all([PID | PIDs], Msg) ->
187 do_print_states(_, []) -> ok;
188 do_print_states(X, States) ->
189 {XStates, RestStates} = lists:split(X, States),
190 ok = io:format([state_to_char(S) || S <- XStates] ++ "\n"),
191 do_print_states(X, RestStates).
194 state_to_char(0) -> ?CHAR_DEAD;
195 state_to_char(1) -> ?CHAR_ALIVE.