image thumbnail
from Rolling Cubes by Krishna Lalith
Flip the cubes upside down.

[Cubex]=Swap_Element(Cubex,Xnew,Ynew,BXnew,BYnew)
function [Cubex]=Swap_Element(Cubex,Xnew,Ynew,BXnew,BYnew)

%--------------------------------------------------------------------------
%Swap along Row
% Up
    if BYnew>Ynew
        for jj=1:2
             temp1=Cubex(6,3:4);
             for ii=5:-1:1
                Cubex(ii+1,3:4)=Cubex(ii,3:4);
             end
             Cubex(1,3:4)=temp1;  
        end
        
        %Readjusting Bottom Face
        temp2=Cubex(1:2,3:4);
        Cubex(1:2,3:4)=[Cubex(2,2) Cubex(2,5);Cubex(5,2) Cubex(5,5)];
        
        %Rotation
         Cubex(3:4,1:2)=rot90(Cubex(3:4,1:2),1); 
         Cubex(3:4,5:6)=rot90(Cubex(3:4,5:6),-1); 
    end
% Down
    if BYnew<Ynew
        for jj=1:2
             temp1=Cubex(1,3:4);
             for ii=1:5
                Cubex(ii,3:4)=Cubex(ii+1,3:4);
             end
             Cubex(6,3:4)=temp1;  
        end
        
        %Readjusting Bottom Face
        temp2=Cubex(5:6,3:4);
        Cubex(5:6,3:4)=[Cubex(2,2) Cubex(2,5);Cubex(5,2) Cubex(5,5)];
        
        %Rotation
         Cubex(3:4,1:2)=rot90(Cubex(3:4,1:2),-1); 
         Cubex(3:4,5:6)=rot90(Cubex(3:4,5:6),1);
    end
%--------------------------------------------------------------------------
%Swap along Column
% Left
    if BXnew>Xnew
        for jj=1:2
             temp1=Cubex(3:4,1);
             for ii=1:5
                Cubex(3:4,ii)=Cubex(3:4,ii+1);
             end
             Cubex(3:4,6)=temp1;  
        end        
        
        %Readjusting Bottom Face
        temp2=Cubex(3:4,5:6);
        Cubex(3:4,5:6)=[Cubex(2,2) Cubex(2,5);Cubex(5,2) Cubex(5,5)];
        
        %Rotation
         Cubex(1:2,3:4)=rot90(Cubex(1:2,3:4),-1);
         Cubex(5:6,3:4)=rot90(Cubex(5:6,3:4),1);
    end
% Right
    if BXnew<Xnew
        for jj=1:2
             temp1=Cubex(3:4,6);
             for ii=5:-1:1
                Cubex(3:4,ii+1)=Cubex(3:4,ii);
             end
             Cubex(3:4,1)=temp1;  
        end
        
        %Readjusting Bottom Face
        temp2=Cubex(3:4,1:2);
        Cubex(3:4,1:2)=[Cubex(2,2) Cubex(2,5);Cubex(5,2) Cubex(5,5)];
        
        %Rotation
         Cubex(1:2,3:4)=rot90(Cubex(1:2,3:4),1);
         Cubex(5:6,3:4)=rot90(Cubex(5:6,3:4),-1);
    end
%--------------------------------------------------------------------------
%Swapping odd n even

if Xnew==BXnew
    temp3=Cubex([1,3,5],3:4);
    Cubex([1,3,5],3:4)=Cubex([2,4,6],3:4);
    Cubex([2,4,6],3:4)=temp3; 
    
    Cubex(2,2)=temp2(1,1);
    Cubex(2,5)=temp2(1,2);
    Cubex(5,2)=temp2(2,1);
    Cubex(5,5)=temp2(2,2);    
end

if Ynew==BYnew
%     temp3=Cubex(3:4,[1,3,5]);
%     Cubex(3:4,[1,3,5])=Cubex(3:4,[2,4,6]);
%     Cubex(3:4,[2,4,6])=temp3;    

    Cubex(2,2)=temp2(1,2);
    Cubex(2,5)=temp2(1,1);
    Cubex(5,2)=temp2(2,2);
    Cubex(5,5)=temp2(2,1);    
end
%--------------------------------------------------------------------------
Cubex([1,6],[1:2,5:6])=2;
Cubex([1:2,5:6],[1,6])=2;%Defining Boundary
%--------------------------------------------------------------------------
%Critical Checks

if Cubex(3:4,3:4)==[1 1;1 1]
    Cubex(:,:)=[2 2 0 0 2 2
      2 0 1 1 0 2
      0 1 1 1 1 0
      0 1 1 1 1 0
      2 0 1 1 0 2
      2 2 0 0 2 2];%6X6
end

if Cubex(3:4,3:4)==[0 0;0 0]
    Cubex(:,:)=[2 2 1 1 2 2
      2 1 0 0 1 2
      1 0 0 0 0 1
      1 0 0 0 0 1
      2 1 0 0 1 2
      2 2 1 1 2 2];%6X6
end

if (Cubex(3:4,3:4)~=[1 1;1 1] & Cubex(3:4,3:4)~=[0 0;0 0])
    if Xnew==BXnew
        Cubex(1:2,3:4)=Cubex(3:4,3:4);
        Cubex(5:6,3:4)=Cubex(3:4,3:4);
    end

    if Ynew==BYnew
        Cubex(3:4,1:2)=Cubex(3:4,3:4);
        Cubex(3:4,5:6)=Cubex(3:4,3:4);
    end
end
%--------------------------------------------------------------------------

Contact us at files@mathworks.com