Printing a status bar per generation.
authorSiraaj Khandkar <siraaj@khandkar.net>
Thu, 9 Aug 2012 00:58:02 +0000 (20:58 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Thu, 9 Aug 2012 00:58:02 +0000 (20:58 -0400)
003/bin/life
003/src/life.erl

index fb2b7f0..072d422 100755 (executable)
@@ -1,7 +1,7 @@
 #! /bin/sh
 
 
-Y=`expr \`stty size | awk '{print $1}'\` - 3`
+Y=`expr \`stty size | awk '{print $1}'\` - 4`
 X=`stty size | awk '{print $2}'`
 
 
index 806d2cb..ea6b499 100644 (file)
@@ -5,6 +5,8 @@
 
 -define(CHAR_DEAD,   32).  % " "
 -define(CHAR_ALIVE, 111).  % "o"
+-define(CHAR_BAR,    45).  % "-"
+
 -define(INTERVAL, 100).
 
 
 
 bang(Args) ->
     [X, Y] = [atom_to_integer(A) || A <- Args],
-    Board = init_board(X, Y),
-    life_loop(X, Y, Board).
+    {Time, Board} = timer:tc(fun() -> init_board(X, Y) end),
+    Generation = 1,
+    life_loop(X, Y, Generation, Time, Board).
 
 
 %% ============================================================================
 %% Internal
 %% ============================================================================
 
-life_loop(X, Y, Board) ->
+life_loop(X, Y, Generation, Time, Board) ->
+    ok = do_print_status(X, Y, Generation, Time),
     ok = do_print_board(Board),
+
+    {NextTime, NextBoard} = timer:tc(fun() -> next_generation(X, Y, Board) end),
+    NextGeneration = Generation + 1,
     timer:sleep(?INTERVAL),
-    life_loop(X, Y, next_generation(X, Y, Board)).
+    life_loop(X, Y, NextGeneration, NextTime, NextBoard).
+
+
+do_print_status(X, Y, Generation, TimeMic) ->
+    TimeSec = TimeMic / 1000000,
+    Bar = [?CHAR_BAR || _ <- lists:seq(1, X)],
+    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]
+    ),
+    ok = io:format("~s~n", [Bar]).
 
 
 do_print_board(Board) ->
This page took 0.030534 seconds and 4 git commands to generate.