6 -define(CHAR_DEAD, 32). % " "
7 -define(CHAR_ALIVE, 111). % "o"
8 -define(CHAR_BAR, 45). % "-"
10 -define(INTERVAL, 100).
13 -record(state, {x :: non_neg_integer()
14 ,y :: non_neg_integer()
16 ,bar :: nonempty_string()
18 ,gen_count :: pos_integer()
19 ,gen_duration :: non_neg_integer()
20 ,print_time :: non_neg_integer()
24 %% ============================================================================
26 %% ============================================================================
29 [X, Y] = [atom_to_integer(A) || A <- Args],
30 {Time, Board} = timer:tc(fun() -> init_board(X, Y) end),
34 ,bar = [?CHAR_BAR || _ <- lists:seq(1, X)]
36 ,gen_count = 1 % Consider inital state to be generation 1
38 ,print_time = 0 % There was no print time yet
43 %% ============================================================================
45 %% ============================================================================
55 ,print_time = LastPrintTime
58 {PrintTime, ok} = timer:tc(
60 do_print_screen(Board, Bar, X, Y, N, GenCount, Time, LastPrintTime)
64 {NewTime, NewBoard} = timer:tc(
66 next_generation(X, Y, Board)
70 NewState = State#state{board = NewBoard
71 ,gen_count = GenCount + 1
72 ,gen_duration = NewTime
73 ,print_time = PrintTime
76 timer:sleep(?INTERVAL),
80 do_print_screen(Board, Bar, X, Y, N, GenCount, Time, PrintTime) ->
81 ok = do_print_status(Bar, X, Y, N, GenCount, Time, PrintTime),
82 ok = do_print_board(Board).
85 do_print_status(Bar, X, Y, N, GenCount, TimeMic, PrintTimeMic) ->
86 TimeSec = TimeMic / 1000000,
87 PrintTimeSec = PrintTimeMic / 1000000,
88 ok = io:format("~s~n", [Bar]),
90 "X: ~b Y: ~b CELLS: ~b GENERATION: ~b DURATION: ~f PRINT TIME: ~f~n",
91 [X, Y, N, GenCount, TimeSec, PrintTimeSec]
93 ok = io:format("~s~n", [Bar]).
96 do_print_board(Board) ->
97 % It seems that just doing a fold should be faster than map + to_list
98 % combo, but, after measuring several times, map + to_list has been
99 % consistently (nearly twice) faster than either foldl or foldr.
100 RowStrings = array:to_list(
118 ok = io:format("~s~n", [RowString])
124 state_to_char(0) -> ?CHAR_DEAD;
125 state_to_char(1) -> ?CHAR_ALIVE.
128 next_generation(W, H, Board) ->
133 Neighbors = filter_offsides(H, W, neighbors(X, Y)),
134 States = neighbor_states(Board, Neighbors),
135 LiveNeighbors = lists:sum(States),
136 new_state(State, LiveNeighbors)
145 new_state(1, LiveNeighbors) when LiveNeighbors < 2 -> 0;
146 new_state(1, LiveNeighbors) when LiveNeighbors < 4 -> 1;
147 new_state(1, LiveNeighbors) when LiveNeighbors > 3 -> 0;
148 new_state(0, LiveNeighbors) when LiveNeighbors =:= 3 -> 1;
149 new_state(State, _LiveNeighbors) -> State.
152 neighbor_states(Board, Neighbors) ->
153 [array:get(X, array:get(Y, Board)) || {X, Y} <- Neighbors].
156 filter_offsides(H, W, Coordinates) ->
157 [{X, Y} || {X, Y} <- Coordinates, is_onside(X, Y, H, W)].
160 is_onside(X, Y, H, W) when (X >= 0) and (Y >= 0) and (X < W) and (Y < H) -> true;
161 is_onside(_, _, _, _) -> false.
165 [{X + OffX, Y + OffY} || {OffX, OffY} <- offsets()].
169 [offset(D) || D <- directions()].
172 offset('N') -> { 0, -1};
173 offset('NE') -> { 1, -1};
174 offset('E') -> { 1, 0};
175 offset('SE') -> { 1, 1};
176 offset('S') -> { 0, 1};
177 offset('SW') -> {-1, 1};
178 offset('W') -> {-1, 0};
179 offset('NW') -> {-1, -1}.
183 ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'].
187 array:map(fun(_, _) -> init_row(X) end, array:new(Y)).
191 array:map(fun(_, _) -> init_cell_state() end, array:new(X)).
195 crypto:rand_uniform(0, 2).
198 atom_to_integer(Atom) ->
199 list_to_integer(atom_to_list(Atom)).