X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=001%2Fsrc%2Flife_time.erl;h=1140c79d5e531e633243e0adc6a373cd7b7601c3;hb=7319e378a77a6be8fc887433c1bb2de08e108fcf;hp=c3b7a45e1d4cb5c00842a592183a26558a41da5f;hpb=79a2bc14dee4d9c393bb8046375f28b0ab177b3c;p=cellular-automata.git diff --git a/001/src/life_time.erl b/001/src/life_time.erl index c3b7a45..1140c79 100644 --- a/001/src/life_time.erl +++ b/001/src/life_time.erl @@ -19,9 +19,9 @@ -define(INTERVAL, 100). % In milliseconds --define(CHAR_DEAD, 32). % Space --define(CHAR_ALIVE, 111). % o --define(CHAR_BAR, 45). % - +-define(CHAR_DEAD, 32). % " " +-define(CHAR_ALIVE, 111). % "o" +-define(CHAR_BAR, 45). % "-" -record(state, {x :: integer() @@ -56,7 +56,6 @@ report_state(CellID, GenID, CellState) -> %% ============================================================================ handle_call(_Msg, _From, State) -> {reply, ok, State}. -handle_info(_Msg, State) -> {noreply, State}. code_change(_Old, State, _Other) -> {ok, State}. terminate(_Reason, State) -> {ok, State}. @@ -78,7 +77,7 @@ init([X, Y, Cells]) -> {ok, State}. -handle_cast(next_gen, +handle_info(next_gen, #state{cells=Cells ,num_cells=NumCells ,state_pairs=[] @@ -94,6 +93,10 @@ handle_cast(next_gen, }, {noreply, NewState}; +handle_info(_Msg, State) -> + {noreply, State}. + + handle_cast({report_state, {CellID, GenID, CellState}}, #state{x=X ,y=Y @@ -121,13 +124,15 @@ handle_cast({report_state, {CellID, GenID, CellState}}, ), StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs], + ok = life_observer:log_generation(GenID, NewNDead, NewNAlive), + ok = io:format( "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), + ok = schedule_next_gen(), {noreply, NewState#state{state_pairs=[]}}; @@ -148,7 +153,8 @@ increment_dead_or_alive(1, NDead, NAlive) -> {NDead, NAlive + 1}. schedule_next_gen() -> - ok = gen_server:cast(?MODULE, next_gen). + erlang:send_after(?INTERVAL, self(), next_gen), + ok. state_to_char(0) -> ?CHAR_DEAD;