function dummy = master()
A = init2();
image(A);
rand('seed',1234);
p=0.01;
last=625;
for i = 1:last,
	A = generationip(A);
	image(A);
	pause(p);
end;
dummy = 0;

function A = init2()
n=25; A = zeros(n,n);
for i=1:n, for j=1:n, A(i,j) = mod(i+j,2)+2; end; end;
cm = [ 0 0 0; 1 0 0; 0 1 0; 0 0 1; 1 1 0; 1 0 1; 0 1 1 ];
% 1 black
% 2 red
% 3 green
% 4 blue 
% 5 yellow 
% 6 magenta
% 7 cyan 
% 0 gets black (why the plus one)
colormap(cm);

function out = generationip(in)
[m, n ] = size(in);
out = in;
for i = 1:m*n, out = ip(out); end;

function out = ip(in)
[n, m] = size(in);
out = in;
i = floor(rand()*n)+1;
j = floor(rand()*m)+1;
switch floor(rand()*4)
case 0,
    i1 = i-1;
    j1 = j;
case 1,
    i1 = i+1;
    j1 = j;
case 2,
    i1 = i;
    j1 = j-1;
case 3,
    i1 = i;
    j1 = j+1;
end;
switch i1
case 0,
    i1 = n;
case n+1,
    i1 = 1;
end;
switch j1
case 0,
    j1 = m;
case m+1,
    j1 = 1;
end;
out(i1,j1) = in(i,j);
