Rubik’s Cube Programming: Part 1

16 10 2008

Oct 08 21:08:02 *    Sabriel doesn’t know what to do
Oct 08 21:08:09 <Sabriel>    other than sleep/sit around all day
Oct 08 21:08:18 <Sabriel>    trying to think of what to do
Oct 08 21:08:19 <PherricOxide>    I see…
Oct 08 21:15:28 <Sabriel>    maybe I’ll go get a rubik’s cube
Oct 08 21:15:55 <PherricOxide>    hm
Oct 08 21:16:04 <PherricOxide>    you could program a rubik’s cube solver
Oct 08 21:16:13 <Sabriel>    hmm
Oct 08 21:16:17 <PherricOxide>    3d array, functions to twist

And while trying to come up with something for her to do, I came up with a programming project that sounded like something I could use to practice my newly learned language of Java. As ideas spun around my head I wandered the disaster area known as my room in search of the frustrating little cube that held my interest until I solved it after a few days (giving up and going to the Internet to learn how to solve the last layer). From the moment I found my long lost little cube buried under piles of science fiction books and old computer parts, a near obsession to model and solve it in a program began. Little cubes took over my thoughts, notebooks, and desktop wallpaper. I even went to Toys-R-Us and got a key chain Rubik’s cube to carry around.

My first idea to use 3-dimensional arrays fell apart in my head before I even loaded up Eclipse, because some cubes had to store the values of 3 colors (corners) while others only had to store the value of one (middle). You could use a 3 dimensional array and then store yet another array containing the colors of the cube, but that’s just overly complicated. Instead, the answer seemed to be using 2-dimensional arrays to store each face of the cube. The next morning I woke up, loaded Eclipse, and began my new found project. I threw together a set of six 3×3 arrays, filled them with strings containing the colors, and began planning the methods to rotate the cube’s sides in certain directions. The way I originally decided to orient my arrays in context to the side made it horribly confusing to do most of the twists, having to move rows of one array to columns of another. Even a function to rotate an array 90 degrees in a particular direction proved frustrating at first. A few minutes into programming I ran into several problems, and began working them out on paper during my classes. Soon the number of pages in my biology notebook filled with representations of cubes outnumbered my notes on biology. Since when do I pay attention in class anyway?

Once I got back to my computer I worked late into the night trying to make the cube object work correctly. I came up with a new orientation for the arrays with respect to the physical cube making twisting sides usually as easy as moving rows into rows, columns into columns, and rotating arrays by 90 degrees. The new array layout became something like this. U: Underside. L: left. B: back. T: top. F: front. R: right.

                              B00 B01 B02
                              B10 B11 B12
                              B20 B21 B22

U01 U02 U03   L00 L01 L02    T00 T01 T02   R00 R01 R02
U10 U11 U12   L10 L11 L12    T10 T11 T12   R10 R11 R12
U20 U21 U22   L20 L21 L22    T20 T21 T22   R20 R21 R22

                              F00 F01 F02
                              F10 F11 F12
                              F20 F21 F22

The next morning I awoke with a new function to rotate an array by 90 degrees clockwise and counterclockwise with just a few lines of code, once again scribbled out on paper during a bus ride to school when I didn’t have a computer with me. I underestimate the importance of planning, and when I’m in front of the computer I tend to code without direction and then run into major speed bumps along the way. Anyway, my functions to rotate the arrays,

	public String[][] rotate_face_clockwise(String startface[][]) {
		String returnface[][] = new String[3][3];
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				returnface[i][j] = startface[2-j][i];
		return returnface;
	public String[][] rotate_face_counterclockwise(String startface[][]) {
		String returnface[][] = new String[3][3];
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				returnface[i][j] = startface[j][2-i];
		return returnface;

With the problem of rotating arrays out of the way, and fairly easy methods to twist the outside of the cube values from side to side, the class to keep track of the cube data and twists came along quickly. Testing the code at this point was becoming rather tedious though. After all, was F02 supposed to twist into R00 or R20 during a clockwise twist of the top face? Aerg! I spent a few minutes solving my Rubik’s cube again, and then labeled all the blocks with a sharpy showing what location in my array the block’s value existed. Sharpies for some reason come off Rubik’s cubes with just touching them… so that didn’t work so well.

Rubik's Cubes

Rubik's Cubes

This made testing a bit easier, but I decided before getting to the programming of the cube solver I would start work on the GUI. Java 3D is prooving to be a pain, but I think I’ll keep working on it. Afterall, it’s just a collection of rotating cubes… how long can it take to make? Stay tuned for part 2 of my programming adventure, probably after midterms, so don’t hold your breath.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: