Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Towers of Hanoi Algorithm Error once i get to 3 disks??

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    2
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Towers of Hanoi Algorithm Error once i get to 3 disks??

    Hi,

    I'm working on a towers of hanoi java programming assignment where
    there are 3 pegs(stacks) and <n> number of disks.


    When my gui calls moveDisks() it moves the disks between the pegs.
    When i say there is 1 disk, the game completes as normal, the same with 2 disks.

    But when i say there are 3 disks my algorithm doesnt work, im not sure what im doing wrong.

    If anyone can help me with my algorithm that would be greatly appreciated.


    See below for my moveDisks and legal move methods:


    public void moveDisks() throws Exception {
            int even =0;
            if (Peg1.size%2 ==0) { //checks if the number of disks is even
                even = 1;
                if(Peg1.top() == smallest.top()){
                        smallest = smallest.right();
                        move(Peg1, Peg2);
                }
            }
            else if(Peg1.size%2>0) { //checks if the number of disks is odd
                even = 0;
                if(Peg1.top() == smallest.top()){
                    smallest = smallest.left();
                    move(Peg1, Peg3);
                }
            }
            if (!done()) {
                BoardString();
            }
            while(!done()) {
                legalMove();
                BoardString();
                if (even == 1) {
                    if(Peg1.top() == smallest.top()){
                            smallest = smallest.right();
                            move(Peg1, Peg2);
                    }
                    else if(Peg2.top() == smallest.top()) {
                            smallest = smallest.right();
                            move(Peg2, Peg3);
                    }
                    else if(Peg3.top() == smallest.top()) {
                            smallest = smallest.right();
                            move(Peg3, Peg1);
                    }
                }
                else if(even == 0) {
                    if(Peg1.top() == smallest.top())
                    {
                            smallest = smallest.left();
                            move(Peg1, Peg3);
                    }
                    else if(Peg2.top() == smallest.top()) {
                            smallest = smallest.left();
                            move(Peg2, Peg1);
                    }
                    else if(Peg3.top() == smallest.top()) {
                            smallest = smallest.left();
                            move(Peg3, Peg2);
                   }
                }
            }
            System.out.println("****** Game Complete ******");
        }
     
        public void legalMove() throws Exception { // make the only other legal move
            try{
                if (Peg1.top() == smallest.top()) {
                    if (Peg2.top() > Peg3.top()) {
                                move(Peg3, Peg2);
                    }
                    else if(Peg2.top() <Peg3.top()){
                                move(Peg2, Peg3);
                    }
                }
                else if(Peg2.top() == smallest.top()) {
                    if (Peg3.top() > Peg1.top()) {
                                move(Peg1, Peg3);
                    }
                    else if(Peg3.top() < Peg1.top()) {
                                move(Peg3, Peg1);
                    }
                }
                else if(Peg3.top() == smallest.top()) {
                    if (Peg1.top() > Peg2.top()) {
                                move(Peg2, Peg1);
                    }
                    else if(Peg1.top()<Peg2.top()){
                                move(Peg1, Peg2);
                    }
                }
            }catch(Exception e){}
        }
    Last edited by KevinWorkman; May 10th, 2011 at 07:43 AM. Reason: added highlight tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,370
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Towers of Hanoi Algorithm Error once i get to 3 disks??

    What do you mean when you say "it doesn't work"? You have to be more specific than that.

    Have you stepped through this with a debugger, or at least added some print statements, to figure out what's going on?

    PS- I added highlight tags for you this time. Don't forget them in the future.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    courtenay (May 10th, 2011)

  4. #3
    Junior Member
    Join Date
    May 2011
    Posts
    2
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Towers of Hanoi Algorithm Error once i get to 3 disks??

    Thank you for your comments, I have managed to solve the issue in the meantime, it turns out I had messed up elsewhere in my Stack class. Instead of returning 0 when a stack was empty i had it returning a error which caused my if statements to fall over somehow.

    Thanks for your tip about the tags, i did not know I could do that.

Similar Threads

  1. all i need is algorithm
    By coder.freak in forum Paid Java Projects
    Replies: 3
    Last Post: April 6th, 2011, 11:11 AM
  2. SDES algorithm
    By low1988 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 2nd, 2010, 09:57 AM
  3. [SOLVED] Algorithm Help
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2010, 04:12 PM
  4. recursive search of all local disks
    By ttsdinesh in forum Java Theory & Questions
    Replies: 4
    Last Post: September 27th, 2009, 08:23 AM
  5. algorithm
    By AmmrO in forum Algorithms & Recursion
    Replies: 13
    Last Post: September 24th, 2009, 09:18 PM