# Cube Computer Programming Help



## dbax0999 (Jul 13, 2009)

So two of my hobbies are programming and cubing so I wanted to combine them.

The obvious thing to do of course is to make a cube program. I'm just looking for some advice on where to start. (What language to use, any tips on what kind of format to have it in, etc.) 

I was thinking of having 4 arrays (Edge Permutation, Edge Orientation, Corner Permutation, Corner Orientation) and using those to store a series of numbers. Then reading those arrays to generate a cube. This method might be able to work but I'm sure there are much better ways out there so if your an experienced programmer or have made a program like this before I would greatly appreciate your shared wisdom.

The language I know best is ActionScript which is probably not the best choice for a program like this. I also know some C++ and Perl, so any help geared towards any of these 3 languages would be the best. 

Thanks,
David

P.S. I'm making this program as an exercise for myself. Please don't just give me source code. I'm just looking for some starting tips.


----------



## qqwref (Jul 13, 2009)

dbax0999 said:


> I was thinking of having 4 arrays (Edge Permutation, Edge Orientation, Corner Permutation, Corner Orientation) and using those to store a series of numbers. Then reading those arrays to generate a cube.



While that works, I personally prefer just storing the stickers, although whether that's a good idea or not depends on the type of program you're making. For instance, if you're working on some kind of solver program, you'd save time by dealing with the puzzle as a group of pieces which can be moved around into 24 different states, but if you're making some kind of simulator it would be much easier to work with a 6x3x3 (or 6xnxn for an nxn cube) array of stickers.


----------



## Johannes91 (Jul 13, 2009)

dbax0999 said:


> I was thinking of having 4 arrays (Edge Permutation, Edge Orientation, Corner Permutation, Corner Orientation) and using those to store a series of numbers. This method might be able to work but I'm sure there are much better ways out there so if your an experienced programmer or have made a program like this before I would greatly appreciate your shared wisdom.


That's simple and very good for some things. Verifying the validity (exactly 1 copy of every piece, parity etc.) is easy, for example. Defining moves isn't as nice as for sticker representation, but it's not too bad.

What's the best choice depends on what's "a program like this". For an efficient solver, you can create transition tables and then the state is just a few integers and making moves is O(1). See "The Mathematics behind Cube Explorer" here and Jaap's page here for lots of really useful tips.



dbax0999 said:


> The language I know best is ActionScript which is probably not the best choice for a program like this. I also know some C++ and Perl, so any help geared towards any of these 3 languages would be the best.


I don't know much about ActionScript but it certainly doesn't seem like the best language for something like this.

Perl isn't fast or memory-efficient but how much that matters depends again on what exactly you want to do. For a simulator and simple solvers it's fine. For a good solver C++ is clearly the best of those 3.


----------



## dbax0999 (Jul 13, 2009)

qqwref said:


> dbax0999 said:
> 
> 
> > I was thinking of having 4 arrays (Edge Permutation, Edge Orientation, Corner Permutation, Corner Orientation) and using those to store a series of numbers. Then reading those arrays to generate a cube.
> ...



Alright. Do you have any advice for a faster way to handle the turns. I've decided to do a simulator and currently my method for turning the cube is to manually move the stickers in the arrays in small 4-cycles. This is getting very tedious. Here's an excerpt from the code:


```
hold = colors[9];
	colors[10] = colors[19];
	colors[19] = colors[28];
	colors[28] = colors[39];
	colors[39] = hold;
	hold = colors[12];
	colors[12] = colors[21];
	colors[21] = colors[30];
	colors[30] = colors[37];
	colors[37] = hold;
	hold = colors[11];
	colors[11] = colors[20];
	colors[20] = colors[29];
	colors[29] = colors[38];
	colors[38] = hold;
	hold = colors[8];
```

As you can see its very long and leaves lots of room for error. 

After programming U and F moves only I've realized that it doesn't work either. After doing some random U and Fs the cube ends up having multiple yellow stickers on one piece.  

Any advice would be nice (hey that rhymed)


----------



## Johannes91 (Jul 13, 2009)

Should the "colors[10]" on the second line be "colors[9]"? Anyway, yes, coding like that isn't good. Never repeate yourself, use loops and functions.


----------



## qqwref (Jul 14, 2009)

dbax0999 said:


> Alright. Do you have any advice for a faster way to handle the turns. I've decided to do a simulator and currently my method for turning the cube is to manually move the stickers in the arrays in small 4-cycles. This is getting very tedious. Here's an excerpt from the code:
> 
> As you can see its very long and leaves lots of room for error.


1) Don't use a one-dimensional array... as I said, it's far easier for you if you define six 3x3 arrays for the stickers, rather than one array of size 54. That way - and this works for any size cube - you can do the 4-cycles in two for loops: use one to rotate the stickers on a face (if necessary) and another to rotate the stickers on a slice. So on a U turn the first loop would handle two 4-cycles and the second loop would handle three. (For an M slice or any inner-layer turn on a larger cube, you only need to use the slice one, so it's good to separate the two functions.)
2) Make a function to do a 4-cycle - that is, you should have a function that you can pass four array locations to, and it will cycle them. It saves a lot of time over coding each 4-cycle by hand.


----------



## jms_gears1 (Jul 14, 2009)

i found it easy to draw a 3D cube on a paper then supply them with x and y coords (i did a 2D drawing instead of the 3D cube) in a 2D array.

so i drew a 2D cube then assigned coords to them then drew a 3D cube and coppied them down then wrote out what piece moved to where then used that in programming. 

i hope that wasnt to vague... but thats what i did and it worked
i also used c# its an easy language with lots of support and if you get xna game studio for it you can create really good 3D Games and appps etc.


----------



## StachuK1992 (Jul 14, 2009)

If I were you, I'd have it so you have an 'edge' class, and a 'corner' class, and create a fictional "core" that these classes revolve around, but then again, I'm not sure how this would work out. :/


----------

