Wed 30 Apr 2008
BASIC and the Rubik’s Cube
Posted by David O under Programming
[2] Comments
Remember when the Rubik’s Cube was still new and BASIC was cutting edge? Well, for that early generation of geeks, of which I was surely a member, it was unavoidable. You just had to write your own BASIC program to solve the Rubik’s Cube. I know there are other old programmers out there who have also done his because I’ve met at least two. I’ love to hear about anyone’s attempts to solve the Cube on simple (pre-Windows) computers, no matter what the language or machine.
Here’s my story.
It all started a “few” years ago to when I was in high school. The Rubik’s Cube popularity had just peaked and our school was still just getting computers. I forget the exact specifications of the machine, but I think it was a TRS-80 Model III with about 16K of RAM and no color. I spent a lot of time on that machine because it was old even then and usually free. Most others wanting to use the newer Apples that had color. Wow.
So how do you represent a colored cube on a monochrome display with at best crude graphics? I used letters for the different colors (B = blue, R = red, G = green, O = orange, Y = yellow, W = white) and unwrapped it on the screen. (Fortunately none of the colors started with the same letter.) You would have something like this for a completed cube:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <pre> O O O O O O O O O Y Y Y B B B G G G Y Y Y B B B G G G Y Y Y B B B G G G R R R R R R R R R W W W W W W W W W</pre> |
After typing in your sequence of letters representing the colors (You only had one chance. One mistake, and you had to start over.), the program dumped instructions to the printer (“Red Right” “Green Left” “White Around” “Blue Left” etc.) while the arrangement of letters on the screen was updated at each step. Needless to say, it was virtually impossible to take a mixed-up cube, type it’s arrangement into the TRS-80, then follow the instructions from the printout and actually solve the puzzle, but I know a few classmates certainly did try. I really should have made it pause after each turn, so you could check for mistakes as you went.
Here are a few things I remember about the program.
- I knew the limits of the machine’s memory so made use of the space compression codes. The TRS-80′s BASIC interpreter used the character values from 0xc0 and up to represent sequences of blanks. One byte could be used to display up to 64 spaces. My data structure was one string holding just the letters and space compression codes. I updated the display by printing just this one string.
- Each of the 18 different moves would change the positions of 20 letters on the display or 20 characters in my string data structure. These character positions were stored in strings of 20 characters. This was much more compact than reading DATA statements. But you couldn’t type these strings with the keyboard because some of their values needed to be higher than the ASCII value of ‘Z’. So I first wrote some code that found the memory location of the array of strings and then used POKE to write the proper values directly into the memory of the program’s instructions. This trick actually made the program manipulated itself.
I don’t really remember why I did it this way. It did make the program shorter and the code simpler in some places, but I doubt that was the real reason. I think I did it just because I could. Plus is looked cool when you listed the program on the screen. It would beep, clear screen, and show graphical characters inside your program listing. This was a great way to show off to the older junior and seniors when they came in and kicked you off the computer so they could write their boring programs to convert Celsius to Fahrenheit for their computer class. I’m sure they all hated me. - Finally, it was my own algorithm. There were a slew books out then to teach you to solve the Cube, but none of that cheating for me. Of course, that also meant that the list of “Yellow Left” “Orange Around” “Green Right” “White Left” etc. probably went on much longer than needed. But that didn’t matter, because you were never going to solve an actual Cube with the program anyway. It was all just a young kid’s way to use up free period.
Best program I ever wrote!
May 7th, 2008 at 2:28 pm
So, in the end, your algorithm could actually solve a Rubik’s Cube or not ?
May 7th, 2008 at 3:05 pm
It would solve it accurately every time on the screen and print out a series of steps that would solve the cube. It was just hard to type in the starting position without a mistake, and then even harder to follow the list of instructions on the printout without a mistake.