Code covered by the BSD License

# Mathworks Rubik version 2

### Thomas Beneder (view profile)

04 Apr 2013 (Updated )

Display, tester and solver of the Rubik's game, including "full cubes".

[out,out2]=optimhist(in,Cube)
```function [out,out2]=optimhist(in,Cube)
%optimhist - Optimize history
%     out=optimhist(hAxes);	% if hAxes not given, an axes is searched
%          takes the data from the axes' userdata
%     out=optimhist(Cube);
%          takes the data from the Cubes history
%     out=optimhist(history);
%          uses directly the array of the same type as the history

if ~exist('Cube','var')
Cube=[];
end
typeIn=0;
if nargin<1
h=FindRubikAxes;
Cube=get(h,'UserData');
H=Cube.history;
typeIn=1;
elseif isstruct(in)
H=in.history;
typeIn=2;
elseif ischar(in)
H=string2hist(in);
typeIn=4;
else
H=in;
typeIn=3;
end
nOpt=0;
bColorSpace=size(H,2)==2;
if bColorSpace
if isempty(Cube)
error('Cube has to be given if colorspace data')
end
H=Color2FRU(H,Cube);
end
n=size(H,1);
i=1;
while i<n
j=i+1;
while j<=n
if H(j)~=H(i)
break;
end
j=j+1;
end
if j-i>1
% if two different sides, sort them
if max(H(i:j-1,2))>min(H(i:j-1,2))
i1=i-1+find(H(i:j-1,2)>0);
i2=i-1+find(H(i:j-1,2)<0);
H(i:j-1,:)=[H(i1,:);H(i2,:)];
j=i+length(i1);
end
if j-i>1	% can be changed by part above
% combine
n1=mod(sum(H(i:j-1,3)),4);
if n1==0
nOpt=nOpt+j-i;
H(i:j-1,:)=[];
j=i;
else
if n1>2
n1=n1-4;
end
nOpt=nOpt+j-i-1;
H=[H(1:i-1,:);H(i,1:2) n1;H(j:end,:)];
j=i+1;
end
n0=n;
n=size(H,1);
if n>n0&j>1
j=j-1;
while j>1&H(j-1)==H(j)
j=j-1;
end
end
end
end
i=j;
end
if bColorSpace
H=FRU2Color(H,Cube);
end
switch typeIn
case 1
Cube.history=H;
set(h,'UserData',Cube);
O=Cube;
case 2
O=in;
O.history=H;
case 3
O=H;
case 4
O=hist2string(H);
end
if nargout|typeIn>1
out=O;
end
if nargout>1
out2=nOpt;
end
```