Pedro
Pedro

Reputation: 75

If statement not working properly in Hanoi Tower solution

In this code to find the solution to the Hanoi Tower problem I set N (number of disks) to 4, as an example and when I run it prints out this:

import java.util.Scanner;
public class HanoiTower {

  public static void main(String[] args) {
    int N;
    Scanner in=new Scanner(System.in);
    System.out.println("Choose number of disks? ");
    N= in.nextInt();
    in.close();
    towers(N,0,1,2);        
  }

  static void towers(int disks, int a,int b, int spare){ 

    if (disks==1){
      System.out.println("Move disk 1 from 1 to 2 ");
    }
    else{
      towers(disks-1, a,spare,b);
      System.out.printf("Move disk %d from stack %d to stack %d ", disks, a, b);
      towers(disks-1,spare,b,a);
    }
  }
}

My doubt is why does it print in first place the if statement?

 Move disk 1 from 1 to 2 
 Move disk 2 from stack 0 to stack 2 Move disk 1 from 1 to 2 
 Move disk 3 from stack 0 to stack 1 Move disk 1 from 1 to 2 
 Move disk 2 from stack 2 to stack 1 Move disk 1 from 1 to 2 
 Move disk 4 from stack 0 to stack 2 Move disk 1 from 1 to 2 
 Move disk 2 from stack 1 to stack 0 Move disk 1 from 1 to 2 
 Move disk 3 from stack 1 to stack 2 Move disk 1 from 1 to 2 
 Move disk 2 from stack 0 to stack 2 Move disk 1 from 1 to 2 
 Move disk 5 from stack 0 to stack 1 Move disk 1 from 1 to 2 
 Move disk 2 from stack 2 to stack 1 Move disk 1 from 1 to 2 
 Move disk 3 from stack 2 to stack 0 Move disk 1 from 1 to 2 
 Move disk 2 from stack 1 to stack 0 Move disk 1 from 1 to 2 
 Move disk 4 from stack 2 to stack 1 Move disk 1 from 1 to 2 
 Move disk 2 from stack 0 to stack 2 Move disk 1 from 1 to 2 
 Move disk 3 from stack 0 to stack 1 Move disk 1 from 1 to 2 
 Move disk 2 from stack 2 to stack 1 Move disk 1 from 1 to 2 

Upvotes: 0

Views: 171

Answers (3)

pkalinow
pkalinow

Reputation: 1741

if works correctly, you can be sure about it. If you call the method with any number greater than 1, it will call itself recursively in line towers(disks-1, a,spare,b); until the number is 1. Then it will print the first message.

Upvotes: 0

Aninda Bhattacharyya
Aninda Bhattacharyya

Reputation: 1251

It is always coming to the else block first and doing the recursion as you designed. So the output with some print statements will clarify it...

public static void main(String[] args){

    int N;
    Scanner in=new Scanner(System.in);
    System.out.println("Choose number of disks? ");
    N= in.nextInt();
    in.close();
    System.out.println("Printing the N..."+N);
    towers(N,0,1,2);

}

static void towers(int disks, int a,int b, int spare){
    System.out.println("Printing the disks..."+disks);

    if (disks==1){
        System.out.println("Move disk 1 from 1 to 2 ");}
    else{
        towers(disks-1, a,spare,b);
        System.out.printf("Move disk %d from stack %d to stack %d ", disks, a, b);
        towers(disks-1,spare,b,a);
    }
    }

Output:

Printing the N...4
Printing the disks...4
Printing the disks...3
Printing the disks...2
Printing the disks...1
Move disk 1 from 1 to 2 
Move disk 2 from stack 0 to stack 1 Printing the disks...1
Move disk 1 from 1 to 2 
Move disk 3 from stack 0 to stack 2 Printing the disks...2
Printing the disks...1
Move disk 1 from 1 to 2 
Move disk 2 from stack 1 to stack 2 Printing the disks...1
Move disk 1 from 1 to 2 
Move disk 4 from stack 0 to stack 1 Printing the disks...3
Printing the disks...2
Printing the disks...1
Move disk 1 from 1 to 2 
Move disk 2 from stack 2 to stack 0 Printing the disks...1
Move disk 1 from 1 to 2 
Move disk 3 from stack 2 to stack 1 Printing the disks...2
Printing the disks...1
Move disk 1 from 1 to 2 
Move disk 2 from stack 0 to stack 1 Printing the disks...1
Move disk 1 from 1 to 2 

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201447

Your method towers recurses until disks is equal to 1. You could log the value of disks at the beginning like

static void towers(int disks, int a, int b, int spare) {
    System.out.printf("Disks = %d%n", disks);
    if (disks == 1) {
        System.out.println("Move disk 1 from 1 to 2 ");
    } else {
        towers(disks - 1, a, spare, b);
        System.out.printf("Move disk %d from stack %d to stack %d ", disks,
                a, b);
        towers(disks - 1, spare, b, a);
    }
}

And you can see why you get the output you do. Also, it's a bad idea to close a Scanner around System.in (once you close System.in, you can't open it again).

Upvotes: 1

Related Questions