X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=001%2Fsrc%2Flife_time.erl;h=6f9d859625ee4cdb043497941be28a4fad642519;hb=1aa9333c0dab226e2e54ea60863c9e55523d77d7;hp=851aa0510ef3d6a618918e729fb27b8883571190;hpb=eec4cd1f37ccb353d325f7f8e46e6f9efaa02077;p=cellular-automata.git diff --git a/001/src/life_time.erl b/001/src/life_time.erl index 851aa05..6f9d859 100644 --- a/001/src/life_time.erl +++ b/001/src/life_time.erl @@ -3,7 +3,7 @@ %% API --export([start_link/2 +-export([start_link/3 ,tock/2 ]). @@ -17,7 +17,7 @@ ]). --define(INTERVAL, 0). % In milliseconds +-define(INTERVAL, 100). % In milliseconds -define(CHAR_DEAD, 32). % Space -define(CHAR_ALIVE, 111). % o @@ -25,6 +25,7 @@ -record(state, {x :: integer() + ,y :: integer() ,cells :: list(atom()) ,num_cells :: integer() ,state_pairs :: list(tuple(integer(), integer())) | [] @@ -37,9 +38,9 @@ %% API %% ============================================================================ -start_link(X, Cells) -> +start_link(X, Y, Cells) -> ServerName = {local, ?MODULE}, - Args = [X, Cells], + Args = [X, Y, Cells], Opts = [], gen_server:start_link(ServerName, ?MODULE, Args, Opts). @@ -52,8 +53,9 @@ tock(CellID, CellState) -> %% Callbacks %% ============================================================================ -init([X, Cells]) -> +init([X, Y, Cells]) -> State = #state{x=X + ,y=Y ,cells=Cells ,num_cells=length(Cells) ,state_pairs=[] @@ -86,6 +88,7 @@ handle_cast(next_tick, handle_cast({tock, {ID, CellState}}, #state{x=X + ,y=Y ,state_pairs=StatePairs ,replies_pending=RepliesPending ,gen_count=GenCount @@ -99,12 +102,15 @@ handle_cast({tock, {ID, CellState}}, case NewRepliesPending of 0 -> NewGenCount = GenCount + 1, - SortedStatePairs = lists:sort(NewStatePairs), + SortedStatePairs = lists:sort( + fun({A, _}, {B, _}) -> A < B end, + NewStatePairs + ), StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs], ok = io:format( - "CELLS: ~b GENERATIONS: ~b~n", - [NumCells, NewGenCount] + "X: ~b Y: ~b CELLS: ~b GENERATIONS: ~b~n", + [X, Y, NumCells, NewGenCount] ), ok = do_print_bar(X),