I am writing my own 'Rubik's cube' application. The main class Cube
has 18 rotation methods:
- RotateAxisXClockWise, RotateAxisXAntiClockWise
- RotateAxisYClockWise, RotateAxisYAntiClockWise
RotateAxisZClockWise, RotateAxisZAntiClockWise
RotateUpperFaceClockWise, RotateUpperFaceAntiClockWise
- RotateFrontFaceClockWise, RotateFrontFaceAntiClockWise
- RotateRightFaceClockWise, RotateRightFaceAntiClockWise
- RotateBackFaceClockWise, RotateBackFAceAntiClockWise
- RotateLeftFaceClockWise, RotateLeftFaceAntiClockWise
- RotateDownFaceClockWise, RotateDownFaceAntiClockWise
Yes, they could be joined in pairs with a parameter Direction (for example RotateFrontFace(Direction direction)
) but for now this seems appropriately.
I would like to implement undo/redo functionality and because all methods have the same signature (no input parameters, void return type) they could be saved in a LinkedList data structure. So every time one of the rotation methods is called, it is added to the linked list.
This would work pretty well if we start on the beginning of the LinkedList (haven't tried it out yet though) and advance toward the end, so each rotation would be performed exactly as it was in the first place.
But what about undo? If I traverse the list from the end to the beginnig, then the opposite method should be called (for example instead of RotateFrontFaceClockWise
, RotateFrontFaceAntiClockWise
should be called). Any ideas how to implement this? Elegantly? :)