home
/
code
/
cellular-automata.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Added generation count to time and cell states.
[cellular-automata.git]
/
001
/
src
/
life_time.erl
diff --git
a/001/src/life_time.erl
b/001/src/life_time.erl
index
b832043
..
dfcec8a
100644
(file)
--- a/
001/src/life_time.erl
+++ b/
001/src/life_time.erl
@@
-3,7
+3,7
@@
%% API
%% API
--export([start_link/
2
+-export([start_link/
3
,tock/2
]).
,tock/2
]).
@@
-17,19
+17,20
@@
]).
]).
--define(INTERVAL, 0). % In milliseconds
+-define(INTERVAL,
10
0). % In milliseconds
-define(CHAR_DEAD, 32). % Space
-define(CHAR_ALIVE, 111). % o
-define(CHAR_DEAD, 32). % Space
-define(CHAR_ALIVE, 111). % o
--define(CHAR_BAR,
61). % =
+-define(CHAR_BAR,
45). % -
-record(state, {x :: integer()
-record(state, {x :: integer()
+ ,y :: integer()
,cells :: list(atom())
,num_cells :: integer()
,state_pairs :: list(tuple(integer(), integer())) | []
,replies_pending :: integer()
,cells :: list(atom())
,num_cells :: integer()
,state_pairs :: list(tuple(integer(), integer())) | []
,replies_pending :: integer()
- ,gen
_count = 0
:: integer()
+ ,gen
eration = 0
:: integer()
}).
}).
@@
-37,9
+38,9
@@
%% API
%% ============================================================================
%% API
%% ============================================================================
-start_link(X, Cells) ->
+start_link(X,
Y,
Cells) ->
ServerName = {local, ?MODULE},
ServerName = {local, ?MODULE},
- Args = [X, Cells],
+ Args = [X,
Y,
Cells],
Opts = [],
gen_server:start_link(ServerName, ?MODULE, Args, Opts).
Opts = [],
gen_server:start_link(ServerName, ?MODULE, Args, Opts).
@@
-52,8
+53,9
@@
tock(CellID, CellState) ->
%% Callbacks
%% ============================================================================
%% Callbacks
%% ============================================================================
-init([X, Cells]) ->
+init([X,
Y,
Cells]) ->
State = #state{x=X
State = #state{x=X
+ ,y=Y
,cells=Cells
,num_cells=length(Cells)
,state_pairs=[]
,cells=Cells
,num_cells=length(Cells)
,state_pairs=[]
@@
-79,16
+81,19
@@
handle_cast(next_tick,
#state{cells=Cells
,num_cells=NumCells
,state_pairs=[]
#state{cells=Cells
,num_cells=NumCells
,state_pairs=[]
+ ,generation=Generation
}=State) ->
}=State) ->
- ok = cast_all(Cells, tick),
- {noreply, State#state{replies_pending=NumCells}};
+ NewGeneration = Generation + 1,
+ ok = cast_all(Cells, {tick, NewGeneration}),
+ {noreply, State#state{replies_pending=NumCells, generation=NewGeneration}};
handle_cast({tock, {ID, CellState}},
#state{x=X
handle_cast({tock, {ID, CellState}},
#state{x=X
+ ,y=Y
,state_pairs=StatePairs
,replies_pending=RepliesPending
,state_pairs=StatePairs
,replies_pending=RepliesPending
- ,gen
_count=GenCount
+ ,gen
eration=Generation
,num_cells=NumCells
}=State) ->
,num_cells=NumCells
}=State) ->
@@
-98,20
+103,23
@@
handle_cast({tock, {ID, CellState}},
case NewRepliesPending of
0 ->
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],
StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
- ok = do_print_bar(X),
+
ok = io:format(
ok = io:format(
- "
CELLS: ~b GENERATIONS
: ~b~n",
- [
NumCells, NewGenCount
]
+ "
X: ~b Y: ~b CELLS: ~b GENERATION
: ~b~n",
+ [
X, Y, NumCells, Generation
]
),
ok = do_print_bar(X),
),
ok = do_print_bar(X),
+
ok = do_print_state_chars(X, StateChars),
ok = do_print_state_chars(X, StateChars),
- ok = do_print_bar(X),
+
ok = timer:sleep(?INTERVAL),
schedule_next_tick(),
ok = timer:sleep(?INTERVAL),
schedule_next_tick(),
- {noreply, NewState#state{state_pairs=[]
, gen_count=NewGenCount
}};
+ {noreply, NewState#state{state_pairs=[]}};
_N ->
{noreply, NewState#state{state_pairs=NewStatePairs}}
_N ->
{noreply, NewState#state{state_pairs=NewStatePairs}}
This page took
0.044324 seconds
and
4
git commands to generate.