I've been working on a brilliant problem that requires programming a cube. Although, it seems that I've not reached the correct solution, I'll share my approach and discoveries.
Beware of bugs in the above code. I have only proved it correct but not tried it.
Here is my code
Here are the things it define: a solved cube (Line 2 of the code), the various rotation operations of the cube(Line 47-58 of the code) and a function that counts the number of time(>0) an Algorithm must be applied on a solved cube to get back the solved cube itself(Line-60).
returns 6. This means that upon applying R U R' U' repeatedly 6 times on a solved cube, you get the solved cube back.
So, How does the code work?
Take a cube and hold it such that the White side is Up, and the Red side is Front. Now start numbering the top face with numbers from 0 to 8 columnwise. Now, hold the cube with your right hand and rotate the cube (and not the face) clockwise 180 Degrees such with your right hand such that the green is stll on your right. Now, number the down face (which is now held up) columnwise from 0 to 8 and also the back face(which is now held front) in the same way. Go back to the way as you held the cube initially. Look towards the right face from the right side of the cube. Number the sqares from 0 to 9 columnwise. Look towards the left face of the cube from the left and do the same thing.
We define the solved cube like this:
,i.e, a dictionary consisting of the squares as a list. Note that the entries in the list are ordered as we numbered the squares for each cube.
Also, that is the basic data structure which is used to describe the cube everywhere throughout is this.
What about the rotations?
Every rotation has been split into two parts: Transposing the matrix of colors on top of the face and rotating the colors of the edges connected to it.
For example, consider doing the move U. First thing, we transpose the matrix on the top clockwise. Next, the squares on the top part on the right face come to the top part of the front, the squares of the front top go to the left and so on. In short, there is a cycle.
We make a copy of the old cube and build the new cube by replacing the corresponding parts from the new cube.
Just for an example, here is Line 51:
U = lambda c: fbmove('U',c,zip([0,3,6],[0,3,6],[8,5,2],[0,3,6]),['F','R','B','L'])
This means that replace the squares 0,3 and 6 of the front face with 0,3 and 6 of the right face.
and replace the squares 0,3 and 6 of the right face with 8,5 and 2 of the back face.
and replace the squares 8,5 and 2 of the back face with 0,3 and 6 of the Left face.
and replace the squares 0,3 and 6 of the Left face with 0,3 and 6 of the front face.
and ofcourse rotate the top squares by 90 Degree Clockwise.
The best way to understand what I am talking of is by actually having a cube with you.
I hope this guide has now shed some light on how to represent a Rubik's Cube through a program. Well, this is just a beginners way. I cannot guarantee that it is the optimal.
P. S.: Thanks to Thaddeus Abiy for correcting my code.