function runner(numReps, bFactor, filename)

%
%L-system
%2D

%clear all
%clf

%Rules-- Cell array {1,x} is the xth string to be replaced
%      -- {2,x}



drawing = true;
branching_factor = bFactor;
%branching_factor = 2;

%number of repititions -- This is 'N' in the project specification
%nReps = 4;
nReps = numReps;


rule(1).before = 'F';
%rule(1).after = 'FF';
rule(1).after = 'F';


iterator = log2(branching_factor);


for i = 1: (branching_factor / 2)
    
   % All right, for this I need to have two string, one containing
   % the [--...--F] and another containting the [+++...++F].
   % I would like to add an addition + and an additional - for each 
   % iteration of the loop. 
   %
   %
   % The first string will contain F[-F][--F]... ] stuff
   % The second string contains [+F][++F]... 
   
   minus_string = '[';
   plus_string = '[';

   for j = 1 : i 
      minus_string = strcat(minus_string, '-');
      plus_string = strcat(plus_string, '+');
   end
   
   minus_string = strcat(minus_string, 'F]');
   plus_string = strcat(plus_string, 'F]');
   
   current = strcat(minus_string, plus_string);
   
   rule(1).after =  strcat(rule(1).after, current);
    
end % end for loop

sprintf('\n\nAFTER STRING CONSTRUCTION\n\n')
sprintf('rule(1).after = %s', rule(1).after)


%rule(2).before = 'G';
%rule(2).after = 'F[+G][-G]F[+G][-G]FG';
%rule(2).after = 'F[+G]F[-G]G';

nRules = length(rule);

%angle: +operator means turn left; -operator means turn right
%delta = 27.5; %degrees
delta = 25.0;  % This is 'theta' in the project specifications

%length of the line segments corresponding to the symbols F and G
lenF = 1;
lenG = 1;

%starting seed
%axiom = 'G';
axiom = 'F';




sprintf('branching_factor = %d', branching_factor)
sprintf('nReps = %d', nReps)
number_leaves = power(branching_factor, nReps);
sprintf('number_leaves = %d', number_leaves)


for i=1:nReps
    
    %one character/cell, with indexes the same as orilsys.mginal axiom string
    axiomINcells = cellstr(axiom'); 
    %axiom

    for j=1:nRules
        %the indexes of each 'before' string
        hit = strfind(axiom, rule(j).before);
        if (length(hit)>=1)
            for k=hit
                axiomINcells{k} = rule(j).after;
            end
        end
    end
    %now convert individual cells back to a string
    axiom=[];0
    sprintf('length(axionINcells = %d', length(axiomINcells))
    for j=1:length(axiomINcells)
        axiom = [axiom, axiomINcells{j}];
        if mod(j, 500) == 0 
            sprintf('REP NUMBER %d  --- %d percent done', i, j / length(axiomINcells) * 100)
        end
    end
    sprintf('FINISHED REP NUMBER %d', i)
end


leaf_count = (axiom == 'G');
leaf_count = leaf_count(axiom == 'G');
sprintf('leaf_count = %d',length(leaf_count))

branch_count = (axiom == 'F');
branch_count = branch_count(axiom == 'F');
sprintf('branch_count = %d', length(branch_count))

sprintf('length(axiom) = %d', length(axiom))

axiom

% Now draw the string as turtle graphics
%Upper case (e.g. F or G) causes a line to be drawn in the current direction of the turtle
%Lower case causes a move with no draw
%angle +operator means turn left; -operator means turn right

%Init the turtle
xT = 0;
yT = 0;
aT = 0;


%da = deg2rad(delta) ; %convert to radians
% This was crashing bad...
da = delta * 0.0174532925;

%init the turtle stack
stkPtr = 1;

%set(gca,'xlim', [-5 5], 'ylim', [-5 5]);
hold on

% Leaf counter 
leafs = 0;

width = 30; % Used to scale the width up and down
origional_width = width;

%width = power(branching_factor, nReps) * width_constant;

net_area = 0;

length_multipler = lenF; % Make ourselfs a length constant to scale up and down
origional_length = lenF;


sprintf('STARTING WIDTH = %d', width)

for i=1:length(axiom)
    cmdT = axiom(i);
    switch cmdT
    case 'F' % Branches
        newxT = xT + cos(aT)*length_multipler;
        newyT = yT + sin(aT)*length_multipler;
        %line([xT newxT], [yT newyT]);
        
        if drawing == true
           line([yT newyT], [xT newxT],'color',[.03 .3 .09], 'linewidth',width);
        end
        
        xT = newxT;
        yT = newyT;

        if mod(j, 500) == 0 
            sprintf('STRING PARSING --- %d', (i /length(axiom) * 100))
        end
        

	net_area = net_area +length_multipler * width;	

    case 'G' % Leaves
        leafs = leafs + 1;
        newxT = xT + lenG*cos(aT);
        newyT = yT + lenG*sin(aT);
        %line([xT newxT], [yT newyT]);
        if drawing == true
            line([yT newyT], [xT newxT],'color','g', 'linewidth',width);
        end
        xT = newxT;
        yT = newyT;
        
        if mod(j, 500) == 0 
            sprintf('STRING PARSING --- %d', (i /length(axiom) * 100))
        end
        
        
    case '+'
        aT = aT + da;
        
        if mod(j, 500) == 0 
            sprintf('STRING PARSING --- %d', (i /length(axiom) * 100))
        end
        
    case '-'
        aT = aT - da;
        
        
        if mod(j, 500) == 0 
            sprintf('STRING PARSING --- %d', (i /length(axiom) * 100))
        end
        
    case '[' %push the stack
        stack(stkPtr).xT = xT ;
        stack(stkPtr).yT = yT ;
        stack(stkPtr).aT = aT ;
        stkPtr = stkPtr +1 ;
        
        width = width / 2;
	length_multipler  = length_multipler / 2;

	%sprintf('     NEW WIDTH = %d', width)

          if mod(j, 500) == 0 
            sprintf('STRING PARSING --- %d', (i /length(axiom) * 100))
        end
    
        
    case ']' %pop the stack
        stkPtr = stkPtr -1 ;
        xT = stack(stkPtr).xT ;
        yT = stack(stkPtr).yT ;
        aT = stack(stkPtr).aT ;
        
        width = width * 2; 
	length_multipler = length_multipler * 2;
    
      if mod(j, 500) == 0 
            sprintf('STRING PARSING --- %d', (i /length(axiom) * 100))
        end
        
    otherwise
        disp('error')
        return
    end
    %drawnow
    
    
end


leaf_width = origional_width * power(.5, nReps);
leaf_length = origional_length * power(.5, nReps);

leaf_area = leaf_width * leaf_length; 

leaf_area = leaf_area * power(branching_factor, nReps);





daspect([1,1,1])

fid = fopen(filename, 'w');
fprintf(fid, 'net_area = %d\n', net_area)
fprintf(fid, 'total_leaf_area = %d\n', leaf_area)
fprintf(fid, 'nReps = %d\n', nReps)
fprintf(fid, 'branching_factor = %d\n', branching_factor)


myValue = 1;

