Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4017372
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:51:11+00:00 2026-05-20T09:51:11+00:00

I am writing my own ‘Rubik’s cube’ application. The main class Cube has 18

  • 0

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? 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:51:11+00:00Added an answer on May 20, 2026 at 9:51 am

    I would not use delegate references as the way to model the rotations, if one of the main purposes is to be able to perform redo/undo. I would consider creating a data-model for each rotation, and store a list of these rotation steps. Each step could then have it’s own associated Redo/Undo delegate, which allows someone traversing the list (from either end) to understand what operations took place, and either repeat or reverse them.

    One additional benefit of a data-oriented approach to model such transformations, is that it could potentially reduce the number of similar (but slightly different) versions of your RotateXXX( ) methods.

    EDIT: Addressing the your question about what shape such a solution might take.

    The simplest thing to do may be to store a Tuple<Action,Action> representing each pair of rotate/unrotate operations as paired delegates. However, I would consider using an explicit data structure that describes the rotation operation, perhaps eventually including things like a descriptive name, direction/face attributes, and so on. I would also change your RotateXXX methods so that they are static methods of Cube, and accept an instance of cube as a parameter. This would allow modeling the rotation operations externally to the instance of Cube.

    public sealed class Rotation
    {
        private readonly Action<Cube> _RotateAction;
        private readonly Action<Cube> _UnrotateAction;  // used for undo or backtracking
    
        private Rotation( Action<Cube> rotateAction, Action<Cube> unrotateAction )
        {
            _RotateAction = rotateAction;
            _UnrotateAction = unrotateAction;
        }
    
        public void Rotate( Cube cube )   { _RotateAction( cube ); }
    
        public void Unrotate( Cube cube ) { _Unrotate( cube ); }
    
        public static readonly RotateFrontFaceClockswise = 
            new Rotation( Cube.RotateFrontFaceClockwise
                          Cube.RotateFrontFaceCounterClockwise );
    
        public static readonly RotateFrontFaceCounterClockwise = 
            new Rotation( Cube.RotateFrontFaceCounterClockwise,
                          Cube.RotateFrontFaceClockwise );
    
        public static readonly RotateLeftFaceClockwise = 
            new Rotation( Cube.RotateLeftFaceClockwise,
                          Cube.RotateLeftFaceCounterClockwise );
    
        public static readonly RotateLeftFaceCounterClockwise = 
            new Rotation( Cube.RotateLeftFaceCounterClockwise,
                          Cube.RotateLeftFaceClockwise );
        // etc..
    }
    
    // now we can keep track of the state changes of a cube using:
    List<Rotation> cubeRotations = new List<Rotation>();
    cubeRotations.Add( Rotation.RotateFrontFaceCounterClockwise );
    cubeRotations.Add( Rotation.RotateBackFaceClockwise );
    cubeRotations.Add( Rotation.RotateLeftFaceCounterClockwise );
    
    // to apply the rotations to a cube, you simple walk through the data structure
    // calling the Rotate( ) method on each:
    Cube someCube = new Cube( ... )
    foreach( Rotation r in cubeRotations )
    {
        r.Rotate( someCube );
    }
    
    // to undo these rotations you can walk the like in reverse:
    foreach( Rotation r in cubeRotations.Reverse() )
    {
        r.Unrotate( someCube );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing my own comparator class called PercentComparator and called the sort as
I am writing my own custom Identity class which implements IIdentity. I don't need
I have started writing my own WebDAV server class in .NET, and the first
I'm writing an own container class and have run into a problem I can't
I am writing my own Vector class by inheriting from my own Point class.
I'm writing my own bignum class for operating on large numbers. So far I've
I'm writing my own wrapper class for parsing XML data. Usually I use the
I just finished writing my own collecion class, and i'd really like to make
I'm writing my own PriorityQueue class and I have: private Queue<E>[] queues; public PriorityQueue(int
I am writing my own poor-man's testing framework. In my console application, I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.