The magazine of the Melbourne PC User Group

Rubik's Cube - Part 5 of 5
Let's Wrap It Up

Ken Holmes

Top Edges

Continued from PC Update Online for May 2000

The four top edge cubes should now be in the top layer but probably not all in correct position and orientation. Each has one face the same colour as the centre of the U face, with the other face the same as the centre of one of the adjacent side faces. Again following the procedure in Don Taylor's book, we need to determine the pattern of correct or incorrect placings and then apply an appropriate sequence of moves to get them into correct positions. At this stage, we again ignore their orientation as this will be corrected later in SUB Flips().

To identify the pattern we will use a 4 digit variable called "correct" in which the thousands digit will be 1 if the u/f cube is correct, the hundreds digit 1 if u/r is correct, the tens digit 1 if u/b is correct and the units digit 1 if u/1 is correct. Note the code in Listing 10, Edges3() for determining "correct". If we get "correct" equal to 1111, all cubes are in position and we don't need to do anything. Assuming we do have the proper four cubes, it is obvious that we can't have only one out of place; oddly, it happens that we never get just two out of place (don't know why), so we just need to deal with all wrong (correct=0) or three wrong (correct= 1,10,100 or 1000).

If "correct" = 0, we can get all correct by either swapping two pairs of opposite cubes or by swapping two pairs of adjacent cubes. In the latter case there are two possible patterns and we can change one into the other by simply rotating the whole cube by 90 deg.; eg. if a swap of u/r and u/f were called for, rotating the whole cube moves the pair to u/f and u/r. We then only need to remember (or program) one rather involved routine to do the double swap. Note in the code that we first check for this and correct it. After that we have two alternative routines, whether for an adjacents double swap or an opposites double swap. Note the routines for this in Listing 10.

If we have three wrong, we rotate the whole cube so that the correct one is at a/r. Then the other three need to circulate in either a clockwise or an anticlockwise direction using the alternative routines in the code.

Flipping The Top Edge Cubes

Again we are grateful to Don Taylor for the routine to do this as I really can't envisage working it out from scratch. We will put the whole thing in a DO.. LOOP which won't give in until we have all of the top faces of edge cubes matching the U face centre. It seems that we can only expect to need to flip 0, 2 or 4 top edge cubes and we have routines to do these in pairs where one of the pair is at u/f. So, we rotate the whole cube until a wrong'un is at u/f. If we find one we then determine where the second one is and apply the double whammy. The first whammy of seven face rotations completely confuses the cube and we then rotate the U face by 90, 180 or 270 deg. depending on the original relation between the pair, which puts the second one at u/r. The second whammy is a reversal of the first and, after a reversal of the U face rotation, the pair are amazingly flipped to correct orientation. If a second pair needs the same treatment, it will receive it before the LOOP lets go.

Twirling The Top Corner Cubes

Again we will use an outer loop which will keep trying until all corners are correct. We use four variables, i/j/k/l, to record the state of u/f/r, u/r/b, u/b/l and u/l/f cubes. They are initially 0 but become + 1 or -1 if the corner needs a clockwise or anticlockwise twirl. Summing their absolute values will tell us how many (sum) need correction. We have a routine which will give u/f/r a clockwise twirl and any other an anticlockwise twirl. Seemingly, we will never find only one corner wrong. If our sum is 0, we don't have a problem and the loop won't repeat. If it is or 4, we rotate the whole cube until u/f/r Leeds a clock twirl and then find the lumber (num) of edges, anticlockwise, to corner needing an anticlockwise twirl.

Should there be three wrong corners, we set num to 1 and so give corner cubes u/f/r and u/r/b the treatment. We don't try to rotate ie cube to find the clock/anticlock combination since it is possible that it does not exist. The willy-nilly treatment will change the arrangement and more trips around the loop will take sum from 4 or 2 to 0. The "treatment" is another double whammy with intermediate U face rotations and the whole cube will appear in its pristine glory with each face one colour only. Note at top left of Figure 5 that this particular solution required 154 rotations.

End of story.

If you have persisted to get a working program, you have obviously found it a rewarding experience.

The files RUBIK.BAS and RUBIK.EXE (64 KB) are available for download, and cover the total program described in the five Part series of articles from February to June, 2000. 

Rubik.bas is the QBasic version and will run in the QBasic environment but you won't be able to make an exe file. If placed in the QuickBasic environment it will be necessary to swap the comment signs in front of some lines of code. These all refer to mouse operation; to ensure you find all comments to change, search on "QBasic" or "outregs" and carefully swap comment "'"s at start of lines. If you leave any QBasic lines uncommented, the file will not run. In all other aspects the two languages are compatible. QuickBasic users can, of course, make an exe file.
Rubik.exe is such a file for those who simply wish to see the program operate, and below is a very brief description of how to use it.

Operation
Note that the left view is of the outside of the cube and the right view is of the inside of the far faces. I consider this the best way to visualise the 3D cube on a two dimensional screen. All operation is by mouse alone; it is confined to the top panel. The articles describe the conventions for naming faces and for a shorthand way to define series of moves. For each face you may click in the appropriate rectangle for a rotation of the face through 90, 180 or 270 degrees, or you may rotate the whole cube about the axis of that face. If you mess it up and can't restore it, just click on "Reset".

Alternatively, click on "Solve" and the program will perform the solution in about 150 moves. This, of course, was the whole object of the articles, to provide a programming exercise for those uncertain of their programming in the hope that they may be encouraged to enjoy more of this activity. The solution may be studied move by move if you click on "Pause" to toggle it on or off. The right mouse button finishes the program. You really need to study the articles to appreciate the whole process.

I hope the program provides you with some interest and enjoyment.

Reprinted from the June 2000 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[About Melbourne PC User Group]