1 ## Copyright (C) 2010 VZLU Prague, a.s.
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn{Function File} {B =} gameoflife (A, ngen, delay)
18 ## Runs the Conways' game of life from a given initial state for a given
19 ## number of generations and visualizes the process.
20 ## If ngen is infinity, the process is run as long as A changes.
21 ## Delay sets the pause between two frames. If zero, visualization is not done.
24 function B = gameoflife (A, ngen, delay = 0.2)
27 CSI = char ([27, 91]);
29 oldpso = page_screen_output (delay != 0);
33 puts (["\n", CSI, "s"]);
34 printf ("generation 0\n");
40 C = conv2 (B, ones (3), "same") - B;
41 B1 = C == 3 | (B & C == 2);
43 if (isinf (ngen) && all ((B1 == B)(:)))
48 puts (["\n", CSI, "u"]);
49 printf ("generation %d\n", igen);
55 unwind_protect_cleanup
56 page_screen_output (oldpso);