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.