Matrix approach in Erlang, using array module.
[cellular-automata.git] / 003 / src / life.erl
1 -module(life).
2
3 -export([bang/1]).
4
5
6 -define(CHAR_DEAD, 32). % " "
7 -define(CHAR_ALIVE, 111). % "o"
8 -define(INTERVAL, 100).
9
10
11 %% ============================================================================
12 %% API
13 %% ============================================================================
14
15 bang(Args) ->
16 [X, Y] = [atom_to_integer(A) || A <- Args],
17 Board = init_board(X, Y),
18 life_loop(Board).
19
20
21 %% ============================================================================
22 %% Internal
23 %% ============================================================================
24
25 life_loop(Board) ->
26 ok = do_print_board(Board),
27 timer:sleep(?INTERVAL),
28 life_loop(next_generation(Board)).
29
30
31 do_print_board(Board) ->
32 CharLists = array:to_list(
33 array:map(
34 fun(_, Row) ->
35 array:to_list(
36 array:map(
37 fun(_, State) ->
38 state_to_char(State)
39 end,
40 Row
41 )
42 )
43 end,
44 Board
45 )
46 ),
47
48 ok = lists:foreach(
49 fun(CharList) ->
50 ok = io:format("~s~n", [CharList])
51 end,
52 CharLists
53 ).
54
55
56 state_to_char(0) -> ?CHAR_DEAD;
57 state_to_char(1) -> ?CHAR_ALIVE.
58
59
60 next_generation(Board) ->
61 H = array:size(Board),
62 W = array:size(array:get(0, Board)),
63
64 array:map(
65 fun(Y, Row) ->
66 array:map(
67 fun(X, State) ->
68 Neighbors = filter_offsides(H, W, neighbors(X, Y)),
69 States = neighbor_states(Board, Neighbors),
70 LiveNeighbors = lists:sum(States),
71 new_state(State, LiveNeighbors)
72 end,
73 Row
74 )
75 end,
76 Board
77 ).
78
79
80 new_state(1, LiveNeighbors) when LiveNeighbors < 2 -> 0;
81 new_state(1, LiveNeighbors) when LiveNeighbors < 4 -> 1;
82 new_state(1, LiveNeighbors) when LiveNeighbors > 3 -> 0;
83 new_state(0, LiveNeighbors) when LiveNeighbors =:= 3 -> 1;
84 new_state(State, _LiveNeighbors) -> State.
85
86
87 neighbor_states(Board, Neighbors) ->
88 [array:get(X, array:get(Y, Board)) || {X, Y} <- Neighbors].
89
90
91 filter_offsides(H, W, Coordinates) ->
92 [{X, Y} || {X, Y} <- Coordinates, is_onside(X, Y, H, W)].
93
94
95 is_onside(X, Y, H, W) when (X >= 0) and (Y >= 0) and (X < W) and (Y < H) -> true;
96 is_onside(_, _, _, _) -> false.
97
98
99 neighbors(X, Y) ->
100 [{X + OffX, Y + OffY} || {OffX, OffY} <- offsets()].
101
102
103 offsets() ->
104 [offset(D) || D <- directions()].
105
106
107 offset('N') -> { 0, -1};
108 offset('NE') -> { 1, -1};
109 offset('E') -> { 1, 0};
110 offset('SE') -> { 1, 1};
111 offset('S') -> { 0, 1};
112 offset('SW') -> {-1, 1};
113 offset('W') -> {-1, 0};
114 offset('NW') -> {-1, -1}.
115
116
117 directions() ->
118 ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'].
119
120
121 init_board(X, Y) ->
122 array:map(fun(_, _) -> init_row(X) end, array:new(Y)).
123
124
125 init_row(X) ->
126 array:map(fun(_, _) -> init_cell_state() end, array:new(X)).
127
128
129 init_cell_state() ->
130 crypto:rand_uniform(0, 2).
131
132
133 atom_to_integer(Atom) ->
134 list_to_integer(atom_to_list(Atom)).
This page took 0.076993 seconds and 5 git commands to generate.