gusucode.com > 三维模仿源码程序 > 三维模仿源码程序/MathRubik2/Rubik.m

    function varargout=Rubik(varargin)
% Rubik - Starting function of the rubik cube
%    Rubik[(options)]
%    Rubik(Cube)
%    Rubik solve
% for example:
%    rubik('doff',0.3,'bfull',true)

Cube=[];
bNewCube=true;
bPlot=true;
options=varargin;
if nargin&isstruct(options{1})
	Cube=options{1};
	bNewCube=false;
	options(1)=[];
end
loop=true;
while loop&~isempty(options)&ischar(options{1})
	loop=false;
	switch lower(options{1})
	case 'solve'
		if nargout==0|isempty(Cube)
			hAxes=FindRubikAxes;
		end
		if isempty(Cube)
			Cube=get(hAxes,'UserData');
		end
		if isempty(Cube.texture)
			Anime=1;
		else
			Anime=0;
		end
		S=SSSsolver('Anime',Anime,options{2:end});
		if nargout
			varargout={S};
		else
			Cube.sol=S;
			Cube.iSol=0;
			set(hAxes,'UserData',Cube)
			set(get(hAxes,'Title'),'String',sprintf('Solution : %d steps',size(S,1)))
			set(get(hAxes,'Parent'),'KeyPressFcn','showsol key')
			axes(hAxes)
			showsol
		end
		return
	case 'mathworks'
		try
			hAxes=FindRubikAxes;
		catch
			Rubik;
			hAxes=FindRubikAxes;
		end
		Cube=get(hAxes,'UserData');
		Cube.texture=load('MathworksCubeTexture');
		Cube=MakeFullCube(Cube);
		set(hAxes,'UserData',Cube)
		PlotCube(hAxes,Cube);
		return
	case 'colors'
		try
			hAxes=FindRubikAxes;
		catch
			Rubik;
			return
		end
		Cube=get(hAxes,'UserData');
		Cube.texture=[];
		Cube.bFullCube=false;
		set(hAxes,'UserData',Cube)
		PlotCube(hAxes,Cube);
		return
	case 'bplot'
		bPlot=options{2};
		options(1:2)=[];
		loop=true;
	otherwise
		%setoptions({'dCubes'},options{:})
	end
end % loop for options

scrsz = get(0,'ScreenSize');
pl = (scrsz(3)-460)/2; % left bound
pu = (scrsz(4)-460)/2; % upper bound
if bNewCube
	Cube = InitCube(Cube,options); % initialize the cube
end
axLim=[-3.7 3.7];
hFig = figure('Position',[pl pu 460 460],...
	'Color',get(0,'DefaultUicontrolBackgroundColor'),...
	'Resize','off','MenuBar','none',...
	'NumberTitle','off','Name','Rubik Cube Game',...
	'WindowButtonMotionFcn','ChangePointer;',	...
	'Tag','RubikFigure'	...
	);
hAxes = axes('Units','pixels','Position',[20 20 420 420],...
	'CameraPosition',[4.5 4 3.5],...
	'XLim',axLim,'YLim',axLim,'ZLim',axLim,...
	'XTickLabel','','YTickLabel','','ZTickLabel','',...
	'XTick',[],'YTick',[],'ZTick',[],	...
	'Tag','RubikAxes'	...
	);
set(hAxes,'UserData',Cube);
hMenuGame = uimenu(hFig,'Label','RubikGame','Tag','RubikMenu');
uimenu(hMenuGame,'Label','Clean Cube',...
	'Callback','CreateNewGame(0);');
uimenu(hMenuGame,'Label','New Game',...
	'Callback','CreateNewGame;');
uimenu(hMenuGame,'Label','Singmasters solver',...
	'Callback','Rubik(''solve'');','Separator','on');
uimenu(hMenuGame,'Label','Set Colors',...
	'Callback','SetColors(gca);','Separator','on');
uimenu(hMenuGame,'Label','Change Colors',...
	'Callback','SetColors(gca,FindRubikCube);');
uimenu(hMenuGame,'Label','Mathworks cube',...
	'Callback','Rubik(''mathworks'');');
uimenu(hMenuGame,'Label','Coloured cube',...
	'Callback','Rubik(''colors'');');
uimenu(hMenuGame,'Label','Exit',...
	'Callback','delete(gcf);','Separator','on');

if bNewCube
	CreateNewGame([],hAxes); % create new game and draw
elseif bPlot
	PlotCube(hAxes,Cube);
end
if nargout
	varargout={hFig};
	if nargout>1
		varargout{2}=hAxes;
		if nargout>2
			varargout{3}=Cube;
		end
	end
end