Introduction

This program will solve a Rubik’s cube using algorithms for beginners. It is a WPF open source application written in C# using 3D graphics and animation. The .NET framework namespaces used are: System.Windows.Media and System.Windows.Media.Media3D and System.Windows.Media.Animation.

The article and the application can be viewed in three different ways.

A demo program to help a Rubik’s cube beginner understand the solution process.

An example of C# source code for solving Rubik’s cube.

An example of WPF C# source code for 3D graphics and animation.

You can play with the cube on the screen or you can scramble your own real cube and paint the screen’s cube to match yours. Then press the Solve Step button and see the rotation steps needed to move one block at a time to achieve progress. The instructions are given in both relative rotation codes U, F, R, B, L, D (Up, Front, Right, Back, Left and Down). And, in color codes Y, R, G, O, B, W (Yellow, Red, Green, Orange, Blue and White).

The cube is solved layer by layer from the white face to the yellow face. Each layer is divided between corners and edges. The major groups are:

White Edges

White Corners

Mid Layer Edges

Yellow Cross

Yellow Corners Position

Yellow Corners Orientation

Yellow Edges

Each group is divided into steps. Each step typically will move one block to its position.

When you debug the program in Visual Studio environment, the program will produce a trace file RubiksCubeTrace.txt. The file is stored in the current directory. If you keep the directory structure stored in the “Downlod source” zip file (RubiksCube_src.zip), the current directory will be “..\RubiksCube\Work” . If you want the visual studio default behaviour, delete the Work folder. The file will be saved in “..\RubiksCube\bin\Debug”. If you want a different working directory go to debug tab in project properties and change it to your directory. The change from revision 1.0.0 is that the working directory in the project properites is now blank.

Terminology

It is a fair assumption that anyone reading this article is familiar with Rubik’s Cube. There are many articles on the Internet about how to solve the Rubik’s cube. The terminology given here is for those who want to understand the code.

The cube is made of 27 blocks. Twenty of these blocks are movable. Six are fixed and their face color represents the cube face color. One block is invisible. The 20 movable blocks are divided into 8 corner blocks and 12 edge blocks. Corner block has 3 visible faces and edge block has 2 visible faces. There are six sided to the cube and each side has 8 movable faces. In total there are 48 movable faces. The 3D graphics part of the code displays and animates these blocks and block faces. The solution part of the code, manipulates the block faces numbers.

The source code classes can be divided into classes that deals with the display of the cube on the screen and classes that solve the puzzle. The display classes have 3D in their name. All the others deal with the logic of rotating faces and solving the cube. The main class for display is Cube3D and the main class for solution is Cube.

Demo program

To test the demo program, create a new folder, copy the attached RubiksCube.exe into this folder and run the program.

Control Buttons

Side Rotations: There are 18 buttons to rotate each of the six sides of the Rubik’s cube in three ways 90 deg CW, 180 deg or 90 deg CCW.

Up Face: Six color buttons to rotate the full cube such that the Up face matches the selected color.

Front Face: Six color buttons to rotate the full cube such that the front face matches the selected color.

User Cube Selected Color: Six color buttons to select a color for painting the Rubik’s cube on the screen to match your real Rubik’s cube. These buttons are active after the User Cube button is pressed. See User Cube notes below.

Solve Step: Pressing the Solve Step button will make the program run one algorithm to advance the solution by one solution step.

Auto Solve: Pressing the Auto Solve button will make the program solve the cube by repeating solve step action until the cube is in solved state.

Save Solution: Pressing Save Solution button will save the cube’s definition and solution steps in a text file. Example of such file is given at the end of this section. The program creates SolutionSteps.txt file in the default directory. The file will override existing file if there is one. After the file is created, the default program in your computer for .txt file will be called. Normally it is Notepad. If you want to save the file permanently, go to Save As in Notepad and save it under another name. The cube definition is made of six lines, one for each face. Each line has eight colors for the movable faces. The first one is the top left corner. The others are in clock wise sequence. The solution is one line per solution step. The solution is given in two ways: relative codes to up and front faces and color codes.

Load Solution: Pressing the load solution will load a cube definition text file. It can be a file saved with Save Solution button. Or, it can be any text file with six lines delimited by [ and ]. Each line consists of eight letters or words. The program reads the first letter of each word. The letter must be W B R G O Y upper or lower case.

Reset: Reset cube to solved state.

Scramble: Scramble the cube by performing 10 random rotations.

Undo: Undo last rotation.

User Cube: Pressing the User Cube button allows you to paint the cube on the screen to match your real cube. Make sure both cubes are oriented the same way. Select color from the User Cube Selected Color six buttons. Rotate the screen cube with the up face and front face buttons. Once you are done press End Coloring button. The program will check that you have entered a valid cube. If the cube is not valid, an error message will be displayed. Correct the error and press again on End Coloring. If you do not want to correct the error, your only choice is Reset.

Mouse Actions

Full cube rotation: Left click the screen outside the cube picture. Hold the left button and move it. The whole cube image will rotate.

One side rotation: Click on any face of the side you want to rotate. If you clicked on a corner face, click again on any of the other three corner faces on the same side. The same thing for edge faces. The face will rotate to bring the first face to the position of the second face.

User cube painting. If you are in user cube painting mode the mouse can only be used to paint faces. To rotate the cube you must use the Up Face and Front Face buttons.

Solving the Rubik’s Cube

The Cube class is the main class controlling the solution process. The cube has six sides. Each side has eight movable faces. In total there are 48 movable faces. The Cube class has an integer array (FaceArray) of 48 integers representing the movable faces. The Cube class and other classes associated with the Cube class manipulate this face array. There are methods to reset the array, to load it, to save it, to rotate it, to test it and to solve the cube.

Face numbers are 0 to 47. The diagram below shows all the face numbers. They have the following properties.

Face position on the side of the cube is FacePos = FaceNo % 8. Face position on each side starts at top left corner and goes clock wise around the face.

When all face numbers are equal to their position in FaceArray the cube is solved. FaceArray[FaceNo] == FaceNo.

Reset. Reset FaceArray to solved state.

public void Reset()

Get color array. Return integer array of face colors.

public int[] ColorArray {get}

Set color array. The software tests the color array for a valid Rubik’s cube. If test is successful, the color array is converted to face array. If the test fails, ApplicationException will be raised.

public int[] ColorArray {set}

Cube status test properties. There are 8 properties to test the solution level of the cube.

public bool AllBlocksInPlace
public bool AllWhiteEdgesInPlace
public bool AllWhiteCornersInPlace
public bool AllMidLayerEdgesInPlace
public bool YellowEdgesInCrossShape
public bool AllYellowEdgesInPlace
public bool AllYellowCornersInPosition
public bool AllYellowCornersInPlace

Rotate array. Rotate FaceArray by one rotation or by an array of rotation.

Cube’s 3D View and Camera Initialization

The initialization code below creates a 3D view port, a 3D model of our Rubik’s cube and a perspective camera. The 3D model of our cube is represented by Cube3D class. The Cube3D class creates 27 blocks represented by Block3D class. Each block creates 6 block faces represented by BlockFace3D. Each block face is divided into triangles represented by MashGeometry3D. Invisible block faces are black. Visible block faces have a gray edge and one of the six colors of the Rubik’s cube. The source code for the 3D objects is part of the attached source code of this article. To view the cube on the screen we create a camera looking at the cube. The camera is positioned above and to the right of the cube. The result is we can see the front face, up face and right face.

Rotating the Full Rubik’s Cube

Rotating the full cube is very simple. During initialization we created 3 AxisAngleRotation3D objects. One object for each axis. To rotate the cube set the Angle property of these three objects. The angles are in degrees. The program keeps the range to -180 to +180.

Rotating One Side of the Cube with Animation

Rotating one side of the cube is done by grouping all 9 blocks of this side and rotating the group. The rotation is done with DoubleAnimation object. The rotation is from zero degrees to rotate angle degrees. The animation duration is RotateDuration. It is 0.25sec for 90 deg and 0.5 sec for 180 deg

Once the animation has been completed, we clear all the objects that were created above.

// clear all block children of the side that was rotated
RotateTransformGroup.Children.Clear();
// rotate the current face array of the cube object
RubiksCube3D.FullCube.RotateArray(RotateCode);
// set the color of all block faces of the cube
RubiksCube3D.SetColorOfAllFaces();
// reset the transform fields of each face that was part of the group
for(int Index = 0; Index <Cube.BlocksPerFace;Index++)RubiksCube3D.CubeFaceBlockArray[RotateCode/3][Index].Transform= null;

Block3D class represent one Rubik's Cube block. This block has 6 sides represented by BlockFace3D. Each visible face is made of 10 triangles. Each invisible face is made of 2 triangles. To do what you want you need to make two significant changes. You need to change Block3D class to represent a sphere. you do it by dividing the sphere surface into many triangles. The six blocks with one visible side will have the normal 6 colors. How you color the other spheres I have no idea. The second big change is the rotation and solution logic. Now it is based on the fact the each block has 6 sided. Six blocks have one visible side. 12 blocks have 2 visible sided and 8 have 3 visible sides and one is invisible. Again how this relates to the "new and improved" cube I have no idea.

When you first see a scrambled Rubik's cube being solved to bring it the perfect order you get excited. The goal of this toy to have one side with no repeating colors. I just cannot see the excitement on a child's face when this happens.

It looks great. Thank you.
When I open the solution with Visual Studio 2010, I get an error "this project is incompatible with the current version of visual studio".
Is there a way to change the project or solution to work with VS 2010?

Sorry but I do not have VS 2010. I tried to change the .net target all the way back to revision 3.0 and I have no problem compiling or running it. My suggestion to you is create a new project RubiksCube with your VS 2010. Copy all the sources from my project to your new and empty project. I think it will work.