%%%%%%% 
% powerflipper.m
% Author: Lance Harden, Davidson College
% June 2006
% 
% Description: Adapted from randompancakeflipper, this version of the
% pancake simulator is designed for more experienced users. It accepts five
% inputs: number of spatulas (1 or 2), type of permutation ('s' or 'u', for
% signed or unsigned), initial permutation, number of trials, and the
% number of random flips per trial, respectively. Thus, in contrast to
% randompancakeflipper, powerflipper does not stop flipping a stack of
% pancakes if it is in the home, [1 2 3 4 5], permutation. It stores the
% flip history for each trial in the cell array, allflips. Meanwhile, for
% each trial, the matrix bigbox indicates whether the stack of pancakes is
% in the home permutation after each flip. Each trial is a row, while the
% column number represents the flip number in that trial. Ones mean
% that the stack is currently in the home permutation.

function [allflips,bigbox] = powerflipper(s,t,I,n,br)
allflips = cell(n,1);
bigbox = [];

if (s ~= 1 & s ~= 2)
    disp('How many arms do you have!')
    return
end

if (t ~= 's' & t ~= 'u')
    disp('Error: You must enter "s" or "u".  Try again by re-running the program.')
    return
end

k = length(I);

if k < 2
    disp('Error: You must enter your initial permutation as a row vector with at least two entries.')
    return
end

B = sort(abs(I));

if (B ~= 1:k & t == 's')
    disp('Error: Your initial permutation must consist of signed consecutive integers starting at 1, e.g. [3 -2 -4 1] is OK, but [8 3 5 1] and [0 -2 1 -3] are not.')
    return
end
if (B ~= 1:k & t == 'u')
    disp('Error: Your initial permutation must consist of consecutive integers starting at 1, e.g. [3 1 2 4] is OK, but [2 8 0 5] is not.')
    return
end

slots = k+1;
choices = nchoosek(1:slots,2);
choices(:,2) = choices(:,2) - 1;
numchoices = length(choices);

tic
if s == 1
    if t == 's'
        for i = 1:n
            G = I;
            count = 0;            
            flips = [];
            
            for u = 1:br
                count = count + 1;
                flips(:,count) = G';             
                P = G;
                a = ceil(k*rand);
                G = [fliplr(P(1:a)) P(a+1:k)];
                for j = 1:a
                  G(j) = -G(j);
                end
                
                if ( all(G == 1:k) || all(G == -k:-1) )
                    bigbox(i,u) = 1;
                else
                    bigbox(i,u) = 0;
                end
            end
            
            flips(:,count + 1) = G';
            allflips{i,1} = flips;
        end
    else
        for i = 1:n
            G = I;
            count = 0;
            flips = [];
            
            for u = 1:br
                count = count + 1;
                flips(:,count) = G';
                P = G;
                a = ceil(k*rand);
                G = [fliplr(P(1:a)) P(a+1:k)];
                
                if all(G == 1:k)
                    bigbox(i,u) = 1;
                else
                    bigbox(i,u) = 0;
                end                
            end
            
            flips(:,count + 1) = G';
            allflips{i,1} = flips;            
        end
    end
else
    if t == 's'
        for i = 1:n
            G = I;
            count = 0;
            flips = [];
            
            for u = 1:br
                count = count + 1;
                flips(:,count) = G';
                P = G;
                a = ceil(numchoices*rand);
                G = [P(1:(choices(a,1)-1)) fliplr(P(choices(a,1):choices(a,2))) P((choices(a,2)+1):k)];                  
                for j = choices(a,1):choices(a,2)
                  G(j) = -G(j);
                end
                
                if ( all(G == 1:k) || all(G == -k:-1) )
                    bigbox(i,u) = 1;
                else
                    bigbox(i,u) = 0;
                end                
            end
            
            flips(:,count + 1) = G';
            allflips{i,1} = flips;            
        end
    else
        for i = 1:n
            G = I;
            count = 0;
            flips = [];
            
            for u = 1:br
                count = count + 1;
                flips(:,count) = G';
                P = G;
                a = ceil(numchoices*rand);
                G = [P(1:(choices(a,1)-1)) fliplr(P(choices(a,1):choices(a,2))) P((choices(a,2)+1):k)];
                
                if all(G == 1:k)
                    bigbox(i,u) = 1;
                else
                    bigbox(i,u) = 0;
                end
            end

            flips(:,count + 1) = G';
            allflips{i,1} = flips;            
        end
    end
end

time = toc
beep
