function dummy = master2()
A = init2();
rand('seed',12345)
last=250;
p=0.01;
for i=1:last,
	A = generationip(A);
	image(A);
	pause(p);
end
pause(3)
for i=1:last,
	A = generationip(A);
	image(A);
	pause(p);
end
dummy = 0;

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

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

function out = ip(in)
%invasion process
[n, m] = size(in);
out = in;
plants = find( (in > 0) & (in < 8) );
pop = size(plants,1);
loc = floor(rand()*pop)+1;
I = plants(loc)-1;
i = mod(I,n)+1;
j = floor(I/n)+1;
%i = floor(rand()*n)+1;
%j = floor(rand()*m)+1;
%while in(i,j) <= 0,
%	i = floor(rand()*n)+1;
%	j = floor(rand()*m)+1;
%end;
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;
while in(i1,j1) <= 0,
	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;
end;
out(i1,j1) = in(i,j);
