X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=003%2Fsrc%2Flife.erl;h=f8b6bcc6086e5380fe87c9d16f660222744b412a;hb=39738e2236f416f7bfaada7f41c8838af807e697;hp=ea6b499f59e49f160258fb6f19ad2871b03ded65;hpb=7a9e70eb6700efa601ab5bd975cf4046b6bd3532;p=cellular-automata.git diff --git a/003/src/life.erl b/003/src/life.erl index ea6b499..f8b6bcc 100644 --- a/003/src/life.erl +++ b/003/src/life.erl @@ -10,6 +10,17 @@ -define(INTERVAL, 100). +-record(state, {x :: non_neg_integer() + ,y :: non_neg_integer() + ,n :: non_neg_integer() + ,bar :: string() + ,board :: array() + ,gen_count :: non_neg_integer() + ,gen_duration :: non_neg_integer() + ,print_time :: non_neg_integer() + }). + + %% ============================================================================ %% API %% ============================================================================ @@ -17,31 +28,62 @@ bang(Args) -> [X, Y] = [atom_to_integer(A) || A <- Args], {Time, Board} = timer:tc(fun() -> init_board(X, Y) end), - Generation = 1, - life_loop(X, Y, Generation, Time, Board). + State = #state{x = X + ,y = Y + ,n = X * Y + ,bar = [?CHAR_BAR || _ <- lists:seq(1, X)] + ,board = Board + ,gen_count = 1 % Consider inital state to be generation 1 + ,gen_duration = Time + ,print_time = 0 % There was no print time yet + }, + life_loop(State). %% ============================================================================ %% Internal %% ============================================================================ -life_loop(X, Y, Generation, Time, Board) -> - ok = do_print_status(X, Y, Generation, Time), - ok = do_print_board(Board), +life_loop( + #state{x = X + ,y = Y + ,n = N + ,bar = Bar + ,board = Board + ,gen_count = GenCount + ,gen_duration = Time + ,print_time = LastPrintTime + }=State) -> + + {PrintTime, ok} = timer:tc( + fun() -> + do_print_screen(Board, Bar, X, Y, N, GenCount, Time, LastPrintTime) + end + ), + + {NewTime, NewBoard} = timer:tc(fun() -> next_generation(X, Y, Board) end), + NewState = State#state{board = NewBoard + ,gen_count = GenCount + 1 + ,gen_duration = NewTime + ,print_time = PrintTime + }, - {NextTime, NextBoard} = timer:tc(fun() -> next_generation(X, Y, Board) end), - NextGeneration = Generation + 1, timer:sleep(?INTERVAL), - life_loop(X, Y, NextGeneration, NextTime, NextBoard). + life_loop(NewState). + + +do_print_screen(Board, Bar, X, Y, N, GenCount, Time, PrintTime) -> + ok = do_print_status(Bar, X, Y, N, GenCount, Time, PrintTime), + ok = do_print_board(Board). -do_print_status(X, Y, Generation, TimeMic) -> +do_print_status(Bar, X, Y, N, GenCount, TimeMic, PrintTimeMic) -> TimeSec = TimeMic / 1000000, - Bar = [?CHAR_BAR || _ <- lists:seq(1, X)], + PrintTimeSec = PrintTimeMic / 1000000, ok = io:format("~s~n", [Bar]), ok = io:format( - "X: ~b Y: ~b CELLS: ~b GENERATION: ~b DURATION: ~f~n", - [X, Y, X * Y, Generation, TimeSec] + "X: ~b Y: ~b CELLS: ~b GENERATION: ~b DURATION: ~f PRINT TIME: ~f~n", + [X, Y, N, GenCount, TimeSec, PrintTimeSec] ), ok = io:format("~s~n", [Bar]).