-1

Okay so I have an assignment on the tower of Hanoi problem. I got the code and got it working but I just don't know what's going on behind it. Can someone please explain? I don't understand the lines where it says solveTower(num-1, from, other, to) and solveTower(1, from, to, other) and solveTower(num-1, other, to, from). I understand what it is doing, it's just moving the number of disks from one peg to another using the other one. I just don't understand what's going on behind it. Like why do I have to move the num-1 disks from the left peg to the middle, the move the biggest disk from the left peg to right peg, and move the num-1 disks from the middle to the right? Why can't I just say something like solveTower(num, from, to, other) to move all the disks from the left peg to the right peg? Here's my driver:

public class Hanoi_Driver
{
  public static void main(String[] args) 
  {
    Tower tower = new Tower();
    tower.solveTower();
  }
}

And here's my object:

import java.util.Scanner;

public class Tower 
{

  private int size;
  private String fromPeg = "A";
  private String toPeg = "C";
  private String otherPeg = "B";

  public Tower() 
  {
    this.size = size;
    this.fromPeg = fromPeg;
    this.toPeg = toPeg;
    this.otherPeg = otherPeg;
  }

  public void solveTower()
  {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("What do you want the size to be?");
    size = keyboard.nextInt();
    System.out.println("Steps for solving " + size + " disk tower of hanoi puzzle, ");

    solveTower(size, fromPeg, toPeg, otherPeg);
  }

  private void solveTower(int num, String from, String to, String other) {

    if (num == 1) {
      System.out.println("Move disk from " + from +
                         " to " + to + ".");
    }
    else {
      solveTower(num-1, from, other, to);
      solveTower(1, from, to, other);
      solveTower(num-1, other, to, from);
    }
  }
}
Ccyan
  • 1,057
  • 12
  • 32

1 Answers1

0

These lines

  solveTower(num-1, from, other, to);
  solveTower(1, from, to, other);
  solveTower(num-1, other, to, from);

indicate that if the solution is not already found (the else of num == 1, in other words where num > 1 or actually when num < 1), then #1: move all but one (num-1) from from to other (not onto to), #2: move the remaining one onto the correct destination to, and #3: move back the original ones from other back to to.
Each time, the third argument is the extra peg to be used.
When you start here:

solveTower(size, fromPeg, toPeg, otherPeg);

you are saying "solve the entire problem".
The other recursive calls to solveTower` break the problem into smaller pieces.
This works just fine for me:

What do you want the size to be?
3
Steps for solving 3 disk tower of hanoi puzzle, 
Move disk from A to C.
Move disk from A to B.
Move disk from C to B.
Move disk from A to C.
Move disk from B to A.
Move disk from B to C.
Move disk from A to C.

With size larger than 3, then the steps to solve the problem become more complex. Sizes 1 and 2 work fine, but size 0 is not handled correctly. (There is no check in the solver for sizes of 0 or less.)
You can delete some unnecessary code in the Tower constructor; it only needs to be:

public Tower() { }

and you might close the Scanner keyboard:

  public void solveTower()
  {
  ... (code removed to reduce length of post)
    solveTower(size, fromPeg, toPeg, otherPeg);
    keyboard.close();
  }
jedison
  • 908
  • 6
  • 15
  • Well I get that, but I just don't get what is happening behind those codes. When I tried using `solveTower(size, fromPeg, toPeg, otherPeg)` it caused an error in Java. That's why I want to know what is going on behind the codes. – Ccyan Apr 27 '14 at 08:50
  • Are you saying that this line of code: `solveTower(size, fromPeg, toPeg, otherPeg)` "causes an error"? What is the error (exception)? That is likely your problem. – jedison Apr 27 '14 at 08:52
  • It says `java.lang.StackOverflowError at Tower.nextMove(Tower.java:35)`. It did compile but it just didn't run. – Ccyan Apr 27 '14 at 08:57
  • When you input size of 0 or less, right? – jedison Apr 27 '14 at 08:58
  • This code works just fine; I pasted the output above. – jedison Apr 27 '14 at 09:01
  • I have suggested two cleanups above. – jedison Apr 27 '14 at 09:02
  • What? I put size is 3 and I ran into that error!! And what should I do if size is 0 or less? – Ccyan Apr 27 '14 at 09:07
  • It works just fine for me. I suspect that you might have accidentally edited the code locally, and posted something else above. Triple check that your local code is the same as you posted in your question. – jedison Apr 27 '14 at 09:09
  • I copied and paste the code here and tried putting solveTower(num, fromPeg, toPeg, otherPeg) but it still causes an error. Oh and I just put that code alone, without anything else. Maybe that's why I get an error? And does the program I'm using make a difference? I'm using Dr.Java. – Ccyan Apr 27 '14 at 09:23
  • Where did you put `solveTower(num, fromPeg, toPeg, otherPeg)`? That is already in the code. You should run "Hanoi_Driver" in Dr. Java, NOT Tower. – jedison Apr 27 '14 at 09:24
  • Wait a second! The error you pasted says `Tower.nextMove(Tower.java:35)` but there is no method called `nextMove()` in the code which you pasted. You've got a mixup between the code you pasted and the code you are testing. – jedison Apr 27 '14 at 09:39
  • Well I changed the name of the method to `nextMove()` in my local code but I did copied and pasted this code from here to my program again and instead of `solveTower(num-1, from, other, to)` and `solveTower(1, from, to, other)` and `solveTower(num-1, other, to, from)`, I just put `solveTower(num,from,to,other)` and run it in the driver but it still gives an `java.lang.StackOverflowError` at the line. – Ccyan Apr 27 '14 at 17:31
  • It works just fine for me. Maybe you need to change the Stack size in Dr Java. – jedison Apr 27 '14 at 19:02