,live_neighbors :: integer()
,num_neighbors :: integer()
,replies_pending :: integer()
+ ,generation :: integer()
}).
{reply, ok, State}.
-handle_cast(tick,
+handle_cast({tick, Generation},
#state{name=Name
,neighbors=Neighbors
,num_neighbors=NumNeighbors
}=State) ->
ok = cast_all(Neighbors, {request_state, Name}),
- {noreply, State#state{replies_pending=NumNeighbors}};
+ {noreply, State#state{replies_pending=NumNeighbors, generation=Generation}};
handle_cast({request_state, Requester}, State) ->
,num_cells :: integer()
,state_pairs :: list(tuple(integer(), integer())) | []
,replies_pending :: integer()
- ,gen_count = 0 :: integer()
+ ,generation = 0 :: integer()
}).
#state{cells=Cells
,num_cells=NumCells
,state_pairs=[]
+ ,generation=Generation
}=State) ->
- ok = cast_all(Cells, tick),
- {noreply, State#state{replies_pending=NumCells}};
+ NewGeneration = Generation + 1,
+ ok = cast_all(Cells, {tick, NewGeneration}),
+ {noreply, State#state{replies_pending=NumCells, generation=NewGeneration}};
handle_cast({tock, {ID, CellState}},
#state{x=X
,y=Y
,state_pairs=StatePairs
,replies_pending=RepliesPending
- ,gen_count=GenCount
+ ,generation=Generation
,num_cells=NumCells
}=State) ->
case NewRepliesPending of
0 ->
- NewGenCount = GenCount + 1,
SortedStatePairs = lists:sort(
fun({A, _}, {B, _}) -> A < B end,
NewStatePairs
StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
ok = io:format(
- "X: ~b Y: ~b CELLS: ~b GENERATIONS: ~b~n",
- [X, Y, NumCells, NewGenCount]
+ "X: ~b Y: ~b CELLS: ~b GENERATION: ~b~n",
+ [X, Y, NumCells, Generation]
),
ok = do_print_bar(X),
ok = timer:sleep(?INTERVAL),
schedule_next_tick(),
- {noreply, NewState#state{state_pairs=[], gen_count=NewGenCount}};
+ {noreply, NewState#state{state_pairs=[]}};
_N ->
{noreply, NewState#state{state_pairs=NewStatePairs}}