Development and School29 Aug 2007 03:02 pm

In CS 490T I am working on a team with two others creating a Rubix Cube project, one being Luke. We have have been struggling to find a nice, clean, object oriented way to represent the cube, but I believe that we have found a good solution. We are going to have a Cube class with methods that rotate sides of the cube. This class will have one property, a collection of Blocks. A single block has six properties: front, left, right, bottom, top, back. The way we will rotate the block will be by specifying a face (top, left, etc…) and a direction, clockwise or counter clockwise. Class outline is below.

Class Cube {
 Block blocks[20];
 rotate(Face, Direction);
}

Class Block {
 Color top;
 Color bottom;
 Color left;
 Color right;
 Color front;
 Color back;
}

The way that you can represent the position of the block is which faces are defined. If a color is not present in the block, then the color will not be defined. Such as the top middle piece will only have the top property not set to null. This way, you can know which position a block is in by knowing which sides have colors defined. So the position of the block with the top, left and front defined is the block in the upper left corner closest to you.

 ______
|\\_\\_\\_\\
|\\|X| | |
|\\| | | |
 \\|_|_|_|

I belive that this Rubix cube has many valid uses for LINQ. One such way defined below.

We were going to do a lot of nasty if statements like

if (top!=null) { //some block on top
 if (front !=null) { //some front block
  if (left !=null) { //top front left
  } else { //top front right
  }
  etc...
}

But instead, with LINQ, we can do the following:

SELECT * FROM blocks WHERE top!=NULL AND front!=NULL AND left!=NULL;

And that will do all the work for us.

I was thinking about other ways to use LINQ, such as the actual updates when a move is done.

UPDATE blocks SET top=(SELECT color FROM blocks WHERE...) WHERE ...

This will update all the block colors for a given move. This may or may not be simpler than a lot of if/else if’s with manual updating.

3 Responses to “Rubix Cube – LINQ Application”

  1. on 29 Aug 2007 at 9:10 pm Daniel Tang

    I haven’t really thought this through, but I wonder how efficient your representation would be. Also, directions seem to be relative (after you twist a cube, is “top” still reaaally “top”? How do you rotate a side face counter-clockwise?), which may be kind of confusing. But then again, I suck at Rubik’s cubes.

  2. on 30 Aug 2007 at 8:50 am Logan

    You can think of the different directions as slices of the cube. So a left or right side contains the 9 blocks on the left or right. The way you move a side counter-clockwise would be to orient the cube with the side face to the front of you, then either move the front blocks clockwise or counterclockwise, then move the orientation of the cube back to where it was.

    As far as directions go, they are pretty much just what direction each color’s axis is in. I arbitrarily said that the cube’s white axis is the top and the green axis is the front, which is it’s “master” orientation. Therefore no matter what you do, a block’s side that is in the same direction as the white axis is considered “top”.

  3. on 02 Sep 2007 at 5:41 pm Luke Hoersten

    @Dan: Gustavo had some of the same hesitations as you. But the orientation, like Logan said, is really elegantly handled in an implicit way.

    Speed is also something Gustavo asked about and he didn’t really understand the magnitude of LINQ. Just because it looks like DB table query syntax doesn’t mean that is how the data is represented. I don’t know if anyone knows how it’s implemented (being proprietary) but I’m sure MS wouldn’t release something like this if it was simply just a table.

    The design should be scalable enough that even if LINQ doesn’t work so well, we can move to something more custom fit to our problem.

    @Logan: Phil helped me get cygwin working on windows so mercurial should work now. Just simply installing cygwin should do it.

Trackback this Post | Feed on comments to this Post

Leave a Reply