gusucode.com > 三维模仿源码程序 > 三维模仿源码程序/MathRubik2/CheckCube.m
function D=CheckCube(Cube) %CheckCube - Checks the state of the cube % D=CheckCube(Cube) if nargin==0|isempty(Cube) Cube=FindRubikCube; end targetColors=Cube.Color(Cube.iMidInd); % dit kan bij initialisatie (en zelfs nog beter want dan is de kubus juist) VCube=zeros(1,27); % "Value" for right position of small cube % the goal is to have a unique number for each cube % this is done in "front-right-up-space", not in color-space % different calculations were possible: % sum (6 ^ (surface#)) % prod ( prime(surface#)) % the last was choosen Plist=[2 3 5 7 11 13]; for i=1:27 VCube(i)=prod(Plist(find(Cube.Color(:,i)>0))); end P=zeros(1,27); for i=1:27 j=find(Cube.Color(:,i)>0); n=length(j); if n>1&n<4 cc=Cube.Color(j,i); % colours of coloured faces dd=cc; % conversion to positions for k=1:n dd(k)=find(targetColors==cc(k)); end if all(j==dd) P(i)=-10; elseif all(j==sort(dd)) P(i)=-5; else % find target location v=prod(Plist(dd)); P(i)=find(VCube==v); end end end iNok0=find(P>0); iNok=iNok0; % search for loops of small cubes Cloops=cell(1,0); B=false(1,27); while length(iNok) i=iNok(1); B(:)=false; B(i)=true; j=i; while P(j)~=i j=P(j); B(j)=true; end Cloops{1,end+1}=find(B); iNok=setdiff(iNok,Cloops{end}); end NOK=zeros(6,3); % look to different layers for i=1:6 j=Cube.RotLayerCube(i,:); ok=P(j)==-10; NOK(i)=sum(ok); NOK(i,2)=length(intersect(j(ok),Cube.iEdge)); NOK(i,3)=length(intersect(j(ok),Cube.iCorner)); end D=struct('OK',all(P==-10|P==0) ... ,'P',P ... ,'iNok',iNok0 ... ,'NOK',NOK ... ,'Cloops',{Cloops} ... );