Results 1 to 6 of 6
  1. #1
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default Omg! Towers of Hanoi. Seriously. Why isnt this right?

    im doing exactly what the comments are telling me to do, but it doesnt work right.

    Java Code:
    public void recursiveTOfH( int numDisks, int fromTower, int toTower, int useTower )
     {
       
    
       
        // Student code starts here
        // 1. Move ( numDisks - 1 ) disks  from fromTower
        //   to useTower using toTower
    		
    
        // 2. Move one disk from fromTower to toTower
        //   Print a message to the screen, then
        //   call moveDisk in order to animate.
    
    
    
    
        // 3. Move ( numDisks - 1 ) disks from useTower to toTower
        //   using fromTower
    
    if(numDisks > 0)
    		{
    			recursiveTOfH(numDisks -1, fromTower, toTower, useTower);	
    			recursiveTOfH(numDisks - 1, fromTower, useTower, toTower);
    			System.out.println("Move a disc from tower " + fromTower + " to tower " + toTower);
    			moveDisk(numDisks, fromTower, toTower);
    			recursiveTOfH(numDisks - 1, useTower, fromTower, toTower);
    		}
    
    
       }
    
       // Base case:  0 disks to move ==> do nothing
    
       //
       // end of student code
       //
     }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    25

    Default

    What you mean does not work correctly? Any errors, or unexpected thing happening in your code?

  3. #3
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    the code compiles. but it doesnt do what it should. tells me things like trying to move from an empty tower, or trying to move a smaller disk, etc.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    10

    Default

    You're not doing exactly what the comments say. Read the comments again and look at your code again.

    -Gary-

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    25

    Default

    Yeah I think in comments you are directing to some invokes. You didn't do it in correct way I guess.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default

    According to the description you need two recursive calls; I see three in your code ...

    kind regards,

    Jos

Similar Threads

  1. Why isnt this working?
    By GoingThroAPhase in forum New To Java
    Replies: 4
    Last Post: 04-03-2010, 02:36 AM
  2. Replies: 11
    Last Post: 12-25-2009, 10:30 PM
  3. Replies: 8
    Last Post: 10-29-2009, 10:54 AM
  4. Towers of Hanoi using Producer/Consumer Pattern
    By dooey in forum New To Java
    Replies: 0
    Last Post: 09-08-2009, 12:45 PM
  5. why isnt it sending value
    By snitdesne in forum New To Java
    Replies: 5
    Last Post: 10-24-2008, 07:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •