image thumbnail

Mathworks Rubik version 2

by

 

04 Apr 2013 (Updated )

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

showsol(Cube,varargin)
function showsol(Cube,varargin)
%showsol  - Shows the solution
hAxes=FindRubikAxes;
if nargin<1|isempty(Cube)
	Cube=FindRubikCube;
elseif ischar(Cube)
	switch Cube
	case 'key'
		switch get(gcf,'CurrentCharacter')
		case {'n',' '}
			ShowTurn(hAxes,1)
		case 'N'
			ShowTurn(hAxes,1,-1)
		case 'p'
			ShowTurn(hAxes,-1)
		case 'P'
			ShowTurn(hAxes,-1,-1)
		case 's'
			ShowTurn(hAxes,-Inf)
		case 'S'
			ShowTurn(hAxes,-Inf,-1)
		case 'T'
			ShowTurn(hAxes,-Inf,0)
		case 'e'
			ShowTurn(hAxes,Inf)
		case 'E'
			ShowTurn(hAxes,Inf,-1)
		case 'F'
			ShowTurn(hAxes,Inf,0)
		case 'X'
			StopShow(hAxes,Cube)
	end
	case 'next'
		ShowTurn(hAxes,1)
	case 'back'
		ShowTurn(hAxes,-1)
	case 'backall'
		ShowTurn(hAxes,-Inf)
	case 'stop'
		StopShow(hAxes,Cube)
	end
	return
end
% Startup settings
Cube.iSol=0;
ShowTitle(hAxes,Cube)
set(hAxes,'UserData',Cube)
set(gcf,'KeyPressFcn','showsol key')

function  StopShow(hAxes,Cube)
Cube.sol=[];
set(hAxes,'UserData',Cube)
set(get(hAxes,'Parent'),'KeyPressFcn','')
ShowTitle(hAxes,Cube);

function ShowTurn(hAxes,dx,Anim)
if nargin<3
	Anim=1;
end
Cube=get(hAxes,'UserData');
i=Cube.iSol;
n=size(Cube.sol,1);
while dx&i<=n
	if dx>0
		i=i+1;
		if i>n
			i=n;
			break;
		end
		sol=Cube.sol(i,:);
		dx=dx-1;
	else	% dx<0
		if i<1
			break;
		end
		sol=Cube.sol(i,:);
		if sol(2)<2
			sol(2)=-sol(2);
		end
		i=i-1;
		dx=dx+1;
	end
	Cube=RotateLayer(sol,Anim,hAxes,Cube);
	if Anim
		axes(hAxes)
		ShowTitle(hAxes,Cube,i)
		drawnow
	end
end
Cube.iSol=i;
set(hAxes,'UserData',Cube)
if Anim==0
	PlotCube(hAxes,Cube);
end
ShowTitle(hAxes,Cube)

Contact us