Get neighbour sides of a Rubics cube I'm writing 2x2x2 rubics cube simulator. In my code I have a concept of Face which is individual side of the cube, and State which is agregate of all six sides. When I create new State, in addition to creating faces I need to set all neighbour faces for a given face. Currently I do this in manual way, as in:

faces[Side.Top][Direction.Up] = faces[Side.Back]; 
faces[Side.Top][Direction.Down] = faces[Side.Front]; 
faces[Side.Top][Direction.Left] = faces[Side.Left]; 
faces[Side.Top][Direction.Right] = faces[Side.Right]; 

faces[Side.Bottom][Direction.Up] = faces[Side.Front]; 
faces[Side.Bottom][Direction.Down] = faces[Side.Back]; 
faces[Side.Bottom][Direction.Left] = faces[Side.Left]; 
faces[Side.Bottom][Direction.Right] = faces[Side.Right];
...


However I feel theres more algorithmical approach which I can't figure out. Does anyone know better way to do this?

Here's full code I've got so far if anyone's interested.
One more algorithmic approach could be to use a different object model more closely relating to the actual cube. That is instead of keeping the state of faces, keep the state of each part of the cube, i.e.The 8 corner pieces of a 2x2x2 cube, and then keep track of the state of each of these, and then have methods to handle the cube and to get the different faces based on the pieces.

If you do this kind of modelling, then your code would also expand easily to larger cube dimensions, as the general movement methods would/should be generic and be applicable to any dimension of the cube.

Edit: Added pseudo code

As always when approaching a problem you need to visualise and simplify into something you are able to comprehend and code. Some givens for this particular problem:


The cube is 3D, but at any given time you are only revolving one plane/face of the cube
When revolving any given face, pieces keep their internal place in that face, albeit the global position has changed, i.e. the corner piece remains the corner piece, but the top-left corner piece could become the top-right corner piece
All pieces in a plane is related and will move as one
For a 2x2x2 cube, use positions from [0, 0, 0] through [1, 1, 1], where [0, 0, 0] is the bottommost, leftmost, frontfacing piece


How does this help us? Well, one can define and code the 2d operations available, and apply them to the cube, which in turn applies them to each piece in turn. Lets define the operations as Rot + Plane  + Direction, where Plane can be X, Y, and Z, and direction is either CW - Clockwise or CCW - CounterClockWise. Operations then become: RotXCW, RotXCCW, RotYCW, and so on. But we need to know which of n=2 planes we're rotating, so lets pass that as an argument.

In order to execute the operation RotXCW(1) on the cube, that is rotate the face to the right clockwise, we need to loop through all pieces having x==1, and call RotXCW on each piece:


Change piece orientation correctly
Let the piece calculate its own position:



        x = prevX 
        y = ( prevZ == 0 ) ?    0  : (n-1) 
        z = ( prevY == 0 ) ? (n-1) :    0 



  To find the new position I wrote down all combinations based on previous position, and looked for the easiest way to represent this acknowledging that the coordinate of a corner piece is always 0 or n-1. Here is the table for rotating clockwise:


  Old Z   Old Y     New Z   New Y
    0      0     >     1      0
    1      0     >     1      1
    1      1     >     0      1
    0      1     >     0      0 

  
  This table leads to implementation given that the new Yis the previous Z, and the new Z is the opposite of the previous Y


In order to implement this you need a Cube class of dimension n, and this concists of Piece's which has a position, and an orientation. Both classes needs to have rotational methods, and in addition you need a method to display the face, and rotate the face of a given piece. 

Hopefully this should give you enough to start coding. It might seem heavy, but isolating and diving the concern into handling single pieces will, in my experience, be well worhth it. Now you can call the rotation operation on the cube, and the single concern of that method is to call rotate on the pieces.