Rubik's Cube

By John McHugh

Originally published in EUG #22

Gus writes...

This excellent program was sent in many moons ago to include in the Games section. Since then I became involved in other things but it has finally made it. I have often thought that the problem with the Elk is that it gives me too many ideas! Sorry for the late delivery!

I asked for some notes on the way the program works and on solving the cube as I thought this would be of interest to members. Back came a wad of notes including six Mode 2 screen dumps showing routines for solution. I think they would have to be serialised if anyone was interested. In the meantime, here are some notes on the program.

It runs in Mode 2 and the screen is loaded as a separate file. This shows an 'exploded' view of the cube so that you can see all sides of it. It also shows the keys to use for the two rotating cursors.

John has used a DATA file rather than arrays for the data on colours and the x, y coordinates and VDU 23 numbers for the two cursors so as to save memory. I will give the details of the data as he has listed it:

DATA FILE &900 -&96B


V            0900    05 05 05 05 05 05 05 05 05                 TOP
.            0909    06 06 06 06 06 06 06 06 06                 REAR
.            0912    02 02 02 02 02 02 02 02 02                 LEFT
.            091B    03 03 03 03 03 03 03 03 03                 RIGHT
.            0924    07 07 07 07 07 07 07 07 07                 FRONT
.            092D    01 01 01 01 01 01 01 01 01                 BOTTOM

.                    CYAN CURSOR. (POINTER W%)

J%           0936    04 05 06 07 07 07 06 05 04 03 03 03        X% TAB
K%           0942    0E 0E 0E 0F 10 11 12 12 12 11 10 0F        Y% TAB
L%           094E    E6 E6 E6 E8 E8 E8 E7 E7 E7 E9 E9 E9        CHAR

.                    GREEN CURSOR.  (POINTER E%)

M%(+1)       095A    0D 0E 0F 0F 0E 0D                          O% TAB
N%(+1)       0960    0B 0A 09 0D 0E 0F                          P% TAB
Q%(+1)       0966    E6 E6 E6 E7 E7 E7                          CHAR

The main program uses multi-statement lines to save memory and to make the program run quicker.

Lines 40 and 50 alter the auto-repeat values and I found with my fumble fingers that this made the cursors too fast! These lines can be deleted if anyone finds it difficult to control.

Line 70 sets up the characters:

  CHR$ 225 to 227 sets the cube colours,
CHR$ 230 to 233 sets the cursors.
  • Line 100 allocates names to frequently used variable values.
  • Lines 110 to 200 are the main loop:
  • Lines 130 to 160 vary the values of W% and E% to select the section to move and PROC X (cyan) and PROC Y (green) move the cursors.
  • Lines 170 and 180 select PROC M (cyan) and PROC N (green).

These 'move' the section of the cube by swapping the colours through PROC C and, where the top, bottom or sides move, through PROC B or PROC D depending on which way the side moves.

The cube is printed using:


and the procedures swap the colours using the indirection operator (?) -

      eg       VDUa,Y,Z,b,V?A,c,b,V?B,c,..
      becomes  VDU31,Y,Z,17,col,char,17,col,char,..

B%, C% and D% are used to hold three of the colours while the twelve colours of the four sides are swapped. In PROC B and PROC D, only B% and C% are needed as only eight colours are swapped as the centre remains the same.

I hope this brief explanation is of interest and gives some idea of how the program works.

Have fun shuffling the cube and trying to solve it. I will try to give some clues on how to do this if there is sufficient interest.

Gus Donnachaidh, EUG #22