DN Staff

March 20, 2006

4 Min Read
Rubik's Cube For The Computer

Most people have a love/hate relationship with the Rubik's cube. Who hasn't been entertained and frustrated for hours trying to solve it? Without, of course, removing the colored stickers!

Now, this tangible classic has moved to the computer. I developed the program using MATLAB(R), our flagship product, and posted the game to The MathWorks' popular user community portal "MATLAB Central:" http://rbi.ims.ca/4917-538.

Using two programming features of MATLAB 7.0 (Release 14), nested functions, and the hgtransform graphical object, I developed the necessary code to make the Rubik's Cube come to life.

I used to be a cube enthusiast when I was younger and was able to solve it using the patterns, and I thought it would be interesting to try to replicate the well-known look and feel of the Rubik's Cube using MATLAB graphics. This program won't help you solve your cube, but my objective was not to build a cube solver. I wanted to build a cube you could play with just the same as if you had one sitting on your desk."

By using nested functions, I wrote very compact and readable code for a user interface. I created the GUI components in the main function, which makes their handles visible inside all of the nested functions (without having to pass anything around). I also used the new hgtransform graphical object. These allow users to manipulate multiple graphical objects (like patches) as if they were a single object - eliminating the very tedious programming of calculating all of the cartesian coordinates for every face of every block in the cube.

To play, you simply need MATLAB and a mouse. To rotate a side of the cube, click on the face of the side you want to turn, which will move either clockwise or counter clockwise according to the setting of the radio buttons on the right. You can view the cube from a different perspective by clicking the ROTATE3D toggle button on the left, then click and drag the cube to rotate it to the desired perspective. Click on the toggle button again to resume basic play.

I even programmed the popular method for solving the puzzle: re-arranging the stickers. Users can click the "Rearrange Stickers" button to reset the cube to a solved state. Re-arranging stickers also clears the player's move memory. -Alexander Mueller, quality engineer, The MathWorks

You need this trick if: You are building graphical user interfaces (GUIs) in MATLAB; you are interested in some of the recent advances in MATLAB graphics capabilities; or you need a cool way to kill some time at work!



For example, the "Rearrange Stickers" uicontrol is defined in the main function as follows:

hstickers = uicontrol('style','pushbutton',...

'Callback',@stickers,...

'String','Rearrange Stickers',...

'Units','normalized',...

'Position',[.42 .08 .2 .05],...

'Enable','off',...

'Interruptible','off',...

'BusyAction','cancel');

The callback function for this uicontrol shwon below is defined as a nested function inside the main function:

function varargout = stickers(varargin)

set(h,'Visible','off');

set(h,'CData',[1 2 3 4 5 6]);

moves=[];

playerMoves=[];

set([hundo,hsolve,hstickers],'Enable','off');

set(h,'Visible','on');

end

All of the data structures and object handles operated on in this callback function were defined in the main function. Thus, we need not worry about inputs or outputs. We can focus on performing the necessary task and nothing more. This simplicity is a direct benefit of nested functions.

The rotations that occur when a user clicks on the cube are implemented in the "rotate" nested function. The following code segments from this function illustrate the use of the hgtransform object in performing these spatial transformations.

% hh are handles to the objects that comprise the side to be

% rotated

hh=h(ind);

t=hgtransform('Parent',gca);

set(hh,'Parent',t);

% Rotate face

for r=speed:15

R=makehgtform(ax_rotation,TformFactor*r*pi/30);

set(t,'Matrix',R);

drawnow

end

set(hh,'Parent',gca);

The hgtransform object, "t", is inserted hierarchically between the axes and the objects to be rotated. That is, the patch objects being rotated are no longer direct children of the axes. They are now children of the hgtransform object, which is a child of the axes. The transformation matrix, "R", is created using the MAKEHGTFORM function according to the parameters of the desired transformation, and is then applied to the hgtransform object, "t". Once the rotation is complete, the patch objects are re-parented to the axes and restored to their original positions.

In truth, the individual patch objects that comprise the cube never move from their original positions relative to one another. The illusion of rotation is achieved through the hgtransform object and manipulation of the patch objectts' "CData".

Sign up for the Design News Daily newsletter.

You May Also Like