1 | within ;
|
---|
2 | model GameofLife
|
---|
3 | parameter Integer n=10; //dimension of the cellular space
|
---|
4 | parameter Integer initState[:,2]= [1, 2; 2, 3; 3, 1; 3, 2; 3, 3]; //glider
|
---|
5 | Integer state[n,n]; // current state of cells
|
---|
6 | Integer newstate[n,n]( start = zeros(n,n)); // new state of cells
|
---|
7 | Integer neighbors( start = 0); // number of neighbors alive
|
---|
8 |
|
---|
9 | initial algorithm
|
---|
10 | //when initial() then // set initial configuration for the cells
|
---|
11 | for i in 1:n loop
|
---|
12 | for j in 1:n loop
|
---|
13 | state[i,j] := 0;
|
---|
14 | end for;
|
---|
15 | end for;
|
---|
16 |
|
---|
17 | for i in 1:size(initState,1) loop
|
---|
18 | state[initState[i,1],initState[i,2]] := 1;
|
---|
19 | end for;
|
---|
20 | //end when;
|
---|
21 |
|
---|
22 | algorithm
|
---|
23 | when sample(1,1) then // periodic iterations or steps
|
---|
24 | for i in 0:n-1 loop // two for loops to evaluate all cells in the space
|
---|
25 | for j in 0:n-1 loop
|
---|
26 | neighbors := 1;
|
---|
27 | /*neighbors := state[mod(i-1,n)+1,mod(j-1,n)+1]+
|
---|
28 | state[i+1,mod(j-1,n)+1]+
|
---|
29 | state[mod(i+1,n)+1,mod(j-1,n)+1]+
|
---|
30 | state[mod(i-1,n)+1,j+1]+
|
---|
31 | state[mod(i+1,n)+1,j+1]+
|
---|
32 | state[mod(i-1,n)+1,mod(j+1,n)+1]+
|
---|
33 | state[i+1,mod(j+1,n)+1]+
|
---|
34 | state[mod(i+1,n)+1,mod(j+1,n)+1];*/
|
---|
35 | if state[i+1,j+1] == 1 then // if alive
|
---|
36 | if neighbors < 2 or neighbors > 3 then
|
---|
37 | newstate[i+1,j+1] := 0; // dies because of isolation or overpopulation (less than 2 or more than 3 neighbors alive)
|
---|
38 | else
|
---|
39 | newstate[i+1,j+1] := 1; // otherwise still alive
|
---|
40 | end if;
|
---|
41 | elseif neighbors == 3 then // if not alive
|
---|
42 | newstate[i+1,j+1] := 1; // becomes alive because of reproduction (3 neighbors alive)
|
---|
43 | end if;
|
---|
44 | end for;
|
---|
45 | end for;
|
---|
46 | state := newstate; // update state
|
---|
47 | end when;
|
---|
48 |
|
---|
49 | end GameofLife;
|
---|