%% API
-export([start_link/3
- ,tock/2
+ ,report_state/3
]).
%% Callbacks
,y :: integer()
,cells :: list(atom())
,num_cells :: integer()
+ ,num_dead :: integer()
+ ,num_alive :: integer()
,state_pairs :: list(tuple(integer(), integer())) | []
,replies_pending :: integer()
- ,generation = 0 :: integer()
+ ,gen_id :: integer()
}).
gen_server:start_link(ServerName, ?MODULE, Args, Opts).
-tock(CellID, CellState) ->
- gen_server:cast(?MODULE, {tock, {CellID, CellState}}).
+report_state(CellID, GenID, CellState) ->
+ gen_server:cast(?MODULE, {report_state, {CellID, GenID, CellState}}).
%% ============================================================================
%% ============================================================================
init([X, Y, Cells]) ->
- State = #state{x=X
- ,y=Y
- ,cells=Cells
- ,num_cells=length(Cells)
- ,state_pairs=[]
- ,replies_pending=0
+ State = #state{x = X
+ ,y = Y
+ ,cells = Cells
+ ,num_cells = length(Cells)
+ ,state_pairs = []
+ ,replies_pending = 0
+ ,gen_id = 0
},
- schedule_next_tick(),
+ ok = schedule_next_gen(),
{ok, State}.
{reply, ok, State}.
-handle_cast(next_tick,
+handle_cast(next_gen,
#state{cells=Cells
,num_cells=NumCells
,state_pairs=[]
- ,generation=Generation
+ ,gen_id=GenID
}=State) ->
- NewGeneration = Generation + 1,
- ok = cast_all(Cells, {tick, NewGeneration}),
- {noreply, State#state{replies_pending=NumCells, generation=NewGeneration}};
+ NewGenID = GenID + 1,
+ ok = cast_all(Cells, {next_gen, NewGenID}),
+ NewState = State#state{replies_pending=NumCells
+ ,gen_id=NewGenID
+ ,num_dead=0
+ ,num_alive=0
+ },
+ {noreply, NewState};
-handle_cast({tock, {ID, CellState}},
+handle_cast({report_state, {CellID, GenID, CellState}},
#state{x=X
,y=Y
+ ,num_dead=NDead
+ ,num_alive=NAlive
,state_pairs=StatePairs
,replies_pending=RepliesPending
- ,generation=Generation
+ ,gen_id=GenID
,num_cells=NumCells
}=State) ->
- NewStatePairs = [{ID, CellState} | StatePairs],
+ NewStatePairs = [{CellID, CellState} | StatePairs],
NewRepliesPending = RepliesPending - 1,
- NewState = State#state{replies_pending=NewRepliesPending},
+ {NewNDead, NewNAlive} = increment_dead_or_alive(CellState, NDead, NAlive),
+ NewState = State#state{replies_pending=NewRepliesPending
+ ,num_dead=NewNDead
+ ,num_alive=NewNAlive
+ },
case NewRepliesPending of
0 ->
StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
ok = io:format(
- "X: ~b Y: ~b CELLS: ~b GENERATION: ~b~n",
- [X, Y, NumCells, Generation]
+ "X: ~b Y: ~b CELLS: ~b DEAD: ~b ALIVE: ~b GENERATION: ~b~n",
+ [X, Y, NumCells, NewNDead, NewNAlive, GenID]
),
ok = do_print_bar(X),
-
ok = do_print_state_chars(X, StateChars),
-
ok = timer:sleep(?INTERVAL),
- schedule_next_tick(),
+ ok = schedule_next_gen(),
{noreply, NewState#state{state_pairs=[]}};
_N ->
%% Internal
%% ============================================================================
-schedule_next_tick() ->
- gen_server:cast(?MODULE, next_tick).
+increment_dead_or_alive(0, NDead, NAlive) -> {NDead + 1, NAlive};
+increment_dead_or_alive(1, NDead, NAlive) -> {NDead, NAlive + 1}.
+
+
+schedule_next_gen() ->
+ ok = gen_server:cast(?MODULE, next_gen).
cast_all([], _) -> ok;