%http://en.wikipedia.org/wiki/Wolfram_code
%http://en.wikipedia.org/wiki/Rule_30 RANDOM GENERATOR
%http://en.wikipedia.org/wiki/Rule_90 XOR
%http://en.wikipedia.org/wiki/Rule_110 TURING MACHINE
%http://en.wikipedia.org/wiki/Rule_184
%http://en.wikipedia.org/wiki/Cellular_automaton
% 150 paritas szabaly
% 
%
% 184: Dugo araszolasba megy at ha A es B aranya jo.
% A=275
% B=300
% rand_vec = [ones(1, A) zeros(1, B)];
% rand_vec = rand_vec(randperm(A+B));
% ca1d_3(rand_vec ,184, 260);

%Probalkozni lehet ezekkel:
%ca1d_3([ rand_vec zeros(1,20) rand_vec(1,1:50) ones(1,20) rand_vec ] ,184, 200);
% 
% ca1d_3(zeros(1,10) ,90, 100);
% ca1d_3([zeros(1,10) 1 zeros(1,10)] ,90, 100);
% ca1d_3([zeros(1,50) 1 zeros(1,50)] ,150, 65);
% ca1d_3([zeros(1,50) 1 zeros(1,50)] ,150, 64);
% ca1d_3([zeros(1,50) 1 zeros(1,50)] ,150, 63);
% ca1d_3([zeros(1,100) 1 zeros(1,100)] ,150, 100);
% ca1d_3([zeros(1,100) 1 zeros(1,100)] ,150, 260);

function  cellasorok  = ca1d_3(cells, rule, iter)
% gets 'cells' binary input (row) vector of size n,
% makes iteration for 'iter' with periodic boundary conditions
% w_rule defines Wolfram 1D cellular automata rule (decimal value)
% outputs 'iter+1 x n+2' size of binary matrix, each row is the next iterated state 
% white cells: 0, black cells: 1

% A bin es  a fi fuggvenyek csak valami toolboxban vannak benne, a dec2bin core Matlab fuggveny
%brule = bin(fi(rule,0,8,0));
brule = dec2bin(rule,8);

lcells = [ cells(end) cells cells(1) ];
cellasorok = [ lcells;
               zeros(iter,length(lcells)) ];
for i=2:iter+1
    for j=2:length(cells)+1
        index = 8 - cellasorok(i-1,j-1)*4 - cellasorok(i-1,j)*2 - cellasorok(i-1,j+1)*1;
        cellasorok(i,j)=str2double(brule(index));
    end
    cellasorok(i,1) = cellasorok(i,end-1);
    cellasorok(i,end) = cellasorok(i,2);
end
image(~cellasorok(:,2:end-1)*8);
colormap(gray(2));
%  set(gca,'xtick',0.5:1:length(cells)+0.5,'ytick',0.5:1:iter+0.5)
%  grid on
