Fun Stuff: Solving a Rubik's Cube


What is more satisfying than solving a Rubik's cube? Making a computer solve one for you.

Background


In graduate school, one of my lab mates and I had a competition see who could first develop a working code to solve a Rubik's cube. I think I won, but seeing as the other party is not here to defend himself, yes, I definitely won.

In this tutorial, we will develop an interactive Rubik's cube in Python using VPython (Visual Python), and develop enough infrastructure that you can implement code to solve the cube.

The Python code for my interactive Rubik's cube and solver can be found here.

Now, the basic design process:

Step 1. Develop a framework that allows you to represent and visualize the cube. In rubiks_cube.py, you will find the rubiks_cube class with member varibles and functions that represent the elements of the cube and to manipulate the cube. Aside from an internal representation of the data, you will want to have some way of actually visualizing the cube. For this tutorial, we will use VPython.

Step 2. Develop functions to perform basic operations to manipulate the cube. These include all of the standard operations you might find in any online Rubik's cube tutorials (right face: clockwise rotation, etc.). Each of these operations are implemented in the rubiks_cube class in rubiks_cube.py.

Step 3. Develop algorithms that can recognize the current state of the cube and apply appropriate operations to work toward the solution. In principle, you can apply any algorithm you want. Just google "how to solve a Rubik's cube" for some examples. You can find the components of my algorithm in the rubiks_cube_solver class in rubiks_cube_solver.py.

Interactive Rubik's Cube

The first thing we need to do is establish an internal representation for the cube. Here is the indexing scheme I use for the faces and the elements of the faces:

      b           3
    l u r       2 0 4
      f   d       1    5


    the origin (0, 0, 0) should be the back
    left corner, indicated by the arrows

    x increases toward the front of the cube
    y increases to the right
    z increases toward the top of the cube

             -> 0|1|2                            
                3|4|5                            
                6|7|8                            
                                                 
      -> 0|1|2  0|1|2  0|1|2                     
         3|4|5  3|4|5  3|4|5                     
         6|7|8  6|7|8  6|7|8                     
                                                 
                0|1|2         -> 0|1|2        
                3|4|5            3|4|5        
                6|7|8            6|7|8  
                                                
Here, u, l, r, f, b, and d refer to the top (up), left, right, front, back, and bottom (down) faces. We will need to visualize this representation in three-dimensional space. The origin is defined as the back left corner, and we use a right-handed coordinate system where the x increases toward the front of the cube, y increases to the right, and z increases toward the top of the cube.

More coming soon!