Imcamshie
Imcamshie

Reputation: 31

Counting towers of hanoi -Java

I am testing the Towers of Hanoi program. I already got help measuring the time of the program, but that is not what my professor wanted he wanted to count the iterations. I can't get it down, and I need the "total of moves" my program will run but it will not print out correctly. Are you guys able to help me? Thank you kindly.

This is the code I was using:

package stackoverflow;

import java.util.*;

public class towers {
    public static int N;
    public static Stack<Integer>[] integer = new Stack[4];

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        integer[1] = new Stack<>();
        integer[2] = new Stack<>();
        integer[3] = new Stack<>();
        System.out.print("Enter 5 integers: ");
        int num = input.nextInt();
        N = num;
        StackMove(num);
    }

    public static void StackMove(int N) {
        for (int d = N; d > 0; d--)
            integer[1].push(d);
        PrintStack();
        move(N, 1, 2, 3);
    }

    public static void move(int N, int a, int b, int c) {
        if (N > 0) {
            move(N - 1, a, c, b);
            int d = integer[a].pop();
            integer[c].push(d);
            PrintStack();
            move(N - 1, b, a, c);
        }
    }

    public static void PrintStack() {
        System.out.println("  A  |  B  |  C");
        System.out.println("---------------");
        for (int i = N - 1; i >= 0; i--) {
            String d1 = " ", d2 = " ", d3 = " ";
            try {
                d1 = String.valueOf(integer[1].get(i));
            } catch (Exception e) {
            }
            try {
                d2 = String.valueOf(integer[2].get(i));
            } catch (Exception e) {
            }
            try {
                d3 = String.valueOf(integer[3].get(i));
            } catch (Exception e) {
            }
            System.out.println("  " + d1 + "  |  " + d2 + "  |  " + d3);
        }
        System.out.print("\n");
    }
}

The output should be like this:

Outpu1 Output2

Upvotes: 1

Views: 593

Answers (3)

Timothy Truckle
Timothy Truckle

Reputation: 15622

Off Topic comments

Here are some comments on your code which do not address your problem but you have to be told. (on topic comments below)

naming of identifiers

Adopt Java Naming Conventions!

eg.:

  • class names start with Uppercase letter whereas method names start with lowercase letter (you did vice versa)
  • variables also start with lowercase letter, only constants are ALL_UPPER_CASE

Then do not use single letter names for variables.

When chosing names for your variables take them from the problem domain.
What is the meaning of the variable integer?
The more proper name would be stacks here (mind the plural 's') but not because it holds an object of the Stack class, but because you are modeling a stack.

Your (class) member variable N interferes with the parameter N of some methods. And here is the one and only point where violating Java Naming Conventions is acceptable: You could prefix member variables with an underscore (_). This will avoid this kind of interference effectively.

avoid odd ball solutions

This doubled identifier N leads to two different approaches within your program:

  • some of your method access the member variable N.
  • some other methods have a parameter of same Type and same name.

For any program choose either one of that access methods.

Imagine what would happen if you need to change N. Your program would start to behave strange and you'll have a hard time to find the bug.

do not use Exceptions as flow control

String d1 = " ", d2 = " ", d3 = " ";
try {
    d1 = String.valueOf(integer[1].get(i));
} catch (Exception e) {
}

With this code you keep the space in d1 in case that the first stack has not enough elements. You should better use a if statement to do that:

String d1 = " ", d2 = " ", d3 = " ";
if( i < integer[1].size() ){
    d1 = String.valueOf(integer[1].get(i));
}

This does not only save a line in your code it also conveys your intention better that your original code.

get used zero based indexing.

You declared your array integer of size 4 to be able to access its elements by natural counting

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        integer[1] = new Stack<>();
        integer[2] = new Stack<>();
        integer[3] = new Stack<>();

The problem here is that you have an additional element in that array which is not used. And even worse, it is the first element in that array.

Java has some clever routines working on arrays (and Collections) which will have problems or at least need special handling if your first array element should be ignored like this.

You can easily come around this by using another technique: replace magic numbers by constants

You could define constants in your class like this:

public class towers {
    private static final int STACK_ONE = 0;
    private static final int STACK_TWO = 1;
    private static final int STACK_TREE = 2;

this would change your code like this:

        integer[STACK_ONE] = new Stack<>();
        integer[STACK_TWO] = new Stack<>();
        integer[STACK_TREE] = new Stack<>();
 //...
                d1 = String.valueOf(integer[STACK_ONE].get(i));

Someone might suggest the Java enum type, but this is kind of tricky when used with arrays.


On Topic comment

Since you choose my answer as the solution, I feel that I must contribute to it... ;o)

where to add the actual counting

the question is: where in the code do you do a countable move?
Obviously it is the move method. But not every call to the move method is a countable move. You can only count the move if you enter the if block inside. So this is the place where you have to add the "counting code".

how to implement the counting?

There are four approaches to this. In order of complexity:

  1. arithmetical calculation
  2. additional member variable,
  3. extra parameter and return value
  4. passing around a counter object.
arithmetical calculation

Since the underlaying problem is deterministic the number of moves can simply be calculated. This solution is provided by Lew Bloch.

additional member variable

You could add another member variable like N:

 public class towers
 {
    public static int N;
    public static int moveCounter = 0;

At the position identified in the previous section you simply add one to the current value of this new member variable.

extra parameter and return value

This solution was provided by Dani

passing around a counter object.

This is somehow similar to the previous solution with the exception that we don't need to return something. But we need an additional class to do this. Therefore this solution is way to complex for this simple task, but I add it for the records.

public class towers {
  private static class Counter{
     private int counter = 0;
     void increment(){ counter++;}
     int counted() { return counter; }
  }
  // ...
        Counter numberOfMoves= new Counter(num,);
        StackMove(num,numberOfMoves);
        System.out.println("Number of moves: "+ numberOfMoves.counted());
  // ...
    public static void StackMove(int N, Counter numberOfMoves) {
        for (int d = N; d > 0; d--)
            integer[1].push(d);
        PrintStack();
        move(N, 1, 2, 3, numberOfMoves);
    }
    public static void move(int N, int a, int b, int c, Counter numberOfMoves) {
        if (N > 0) {
            move(N - 1, a, c, b, numberOfMoves);
            int d = integer[a].pop();
            integer[c].push(d);
            PrintStack();
            move(N - 1, b, a, c, numberOfMoves);
            numberOfMoves.count();
        }
     }
  // ...

Upvotes: 0

Lew Bloch
Lew Bloch

Reputation: 3433

For n items, raise 2 to the n and subtract 1.

Upvotes: 1

Dani
Dani

Reputation: 2036

CODE

import java.util.Stack;

import java.util.*;

public class TowersOfHanoiPrint {
    public static int N;
    public static Stack<Integer>[] integer = new Stack[4];

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        integer[1] = new Stack<>();
        integer[2] = new Stack<>();
        integer[3] = new Stack<>();
        System.out.print("Enter 5 integers: ");
        int num = input.nextInt();
        N = num;
        System.out.println("Number of moves: "+StackMove(num));
    }

    public static int StackMove(int N) {
        for (int d = N; d > 0; d--)
            integer[1].push(d);
        PrintStack();
        return move(N, 1, 2, 3);
    }

    public static int move(int N, int a, int b, int c) {
        if (N > 0) {
            int numberMovesA = move(N - 1, a, c, b);
            int d = integer[a].pop();
            integer[c].push(d);
            PrintStack();
            int numberMovesB = move(N - 1, b, a, c);
            return (numberMovesA + numberMovesB + 1);
        }
        return 0;
    }

    public static void PrintStack() {
        System.out.println("  A  |  B  |  C");
        System.out.println("---------------");
        for (int i = N - 1; i >= 0; i--) {
            String d1 = " ", d2 = " ", d3 = " ";
            try {
                d1 = String.valueOf(integer[1].get(i));
            } catch (Exception e) {
            }
            try {
                d2 = String.valueOf(integer[2].get(i));
            } catch (Exception e) {
            }
            try {
                d3 = String.valueOf(integer[3].get(i));
            } catch (Exception e) {
            }
            System.out.println("  " + d1 + "  |  " + d2 + "  |  " + d3);
        }
        System.out.print("\n");
    }
}

OUTPUT

Enter 5 integers: 6
  A  |  B  |  C
---------------
  1  |     |   
  2  |     |   
  3  |     |   
  4  |     |   
  5  |     |   
  6  |     |   

  A  |  B  |  C
---------------
     |     |   
  2  |     |   
  3  |     |   
  4  |     |   
  5  |     |   
  6  |  1  |   

  A  |  B  |  C
---------------
     |     |   
     |     |   
  3  |     |   
  4  |     |   
  5  |     |   
  6  |  1  |  2

  A  |  B  |  C
---------------
     |     |   
     |     |   
  3  |     |   
  4  |     |   
  5  |     |  1
  6  |     |  2

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
  4  |     |   
  5  |     |  1
  6  |  3  |  2

  A  |  B  |  C
---------------
     |     |   
     |     |   
  1  |     |   
  4  |     |   
  5  |     |   
  6  |  3  |  2

  A  |  B  |  C
---------------
     |     |   
     |     |   
  1  |     |   
  4  |     |   
  5  |  2  |   
  6  |  3  |   

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
  4  |  1  |   
  5  |  2  |   
  6  |  3  |   

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |  1  |   
  5  |  2  |   
  6  |  3  |  4

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |     |   
  5  |  2  |  1
  6  |  3  |  4

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
  2  |     |   
  5  |     |  1
  6  |  3  |  4

  A  |  B  |  C
---------------
     |     |   
     |     |   
  1  |     |   
  2  |     |   
  5  |     |   
  6  |  3  |  4

  A  |  B  |  C
---------------
     |     |   
     |     |   
  1  |     |   
  2  |     |   
  5  |     |  3
  6  |     |  4

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
  2  |     |   
  5  |     |  3
  6  |  1  |  4

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |     |  2
  5  |     |  3
  6  |  1  |  4

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |  1
     |     |  2
  5  |     |  3
  6  |     |  4

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |  1
     |     |  2
     |     |  3
  6  |  5  |  4

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |     |  2
  1  |     |  3
  6  |  5  |  4

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |     |   
  1  |  2  |  3
  6  |  5  |  4

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |  1  |   
     |  2  |  3
  6  |  5  |  4

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |  1  |   
  3  |  2  |   
  6  |  5  |  4

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |     |   
  3  |  2  |  1
  6  |  5  |  4

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
  2  |     |   
  3  |     |  1
  6  |  5  |  4

  A  |  B  |  C
---------------
     |     |   
     |     |   
  1  |     |   
  2  |     |   
  3  |     |   
  6  |  5  |  4

  A  |  B  |  C
---------------
     |     |   
     |     |   
  1  |     |   
  2  |     |   
  3  |  4  |   
  6  |  5  |   

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
  2  |  1  |   
  3  |  4  |   
  6  |  5  |   

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |  1  |   
  3  |  4  |   
  6  |  5  |  2

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |     |   
  3  |  4  |  1
  6  |  5  |  2

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |  3  |   
     |  4  |  1
  6  |  5  |  2

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |  3  |   
  1  |  4  |   
  6  |  5  |  2

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |  2  |   
     |  3  |   
  1  |  4  |   
  6  |  5  |   

  A  |  B  |  C
---------------
     |     |   
     |  1  |   
     |  2  |   
     |  3  |   
     |  4  |   
  6  |  5  |   

  A  |  B  |  C
---------------
     |     |   
     |  1  |   
     |  2  |   
     |  3  |   
     |  4  |   
     |  5  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |  2  |   
     |  3  |   
     |  4  |  1
     |  5  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |  3  |   
     |  4  |  1
  2  |  5  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |  3  |   
  1  |  4  |   
  2  |  5  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |     |   
  1  |  4  |  3
  2  |  5  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |  1  |   
     |  4  |  3
  2  |  5  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |  1  |  2
     |  4  |  3
     |  5  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |  1
     |     |  2
     |  4  |  3
     |  5  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |  1
     |     |  2
     |     |  3
  4  |  5  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |     |  2
  1  |     |  3
  4  |  5  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |     |   
  1  |  2  |  3
  4  |  5  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |  1  |   
     |  2  |  3
  4  |  5  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |  1  |   
  3  |  2  |   
  4  |  5  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |     |   
  3  |  2  |  1
  4  |  5  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
  2  |     |   
  3  |     |  1
  4  |  5  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
  1  |     |   
  2  |     |   
  3  |     |   
  4  |  5  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
  1  |     |   
  2  |     |   
  3  |     |  5
  4  |     |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
  2  |     |   
  3  |     |  5
  4  |  1  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |     |  2
  3  |     |  5
  4  |  1  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |  1
     |     |  2
  3  |     |  5
  4  |     |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |  1
     |     |  2
     |     |  5
  4  |  3  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |     |  2
  1  |     |  5
  4  |  3  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |     |   
  1  |  2  |  5
  4  |  3  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |  1  |   
     |  2  |  5
  4  |  3  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |  1  |  4
     |  2  |  5
     |  3  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |  1
     |     |  4
     |  2  |  5
     |  3  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |  1
     |     |  4
     |     |  5
  2  |  3  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |   
     |     |  4
  1  |     |  5
  2  |  3  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |  3
     |     |  4
  1  |     |  5
  2  |     |  6

  A  |  B  |  C
---------------
     |     |   
     |     |   
     |     |  3
     |     |  4
     |     |  5
  2  |  1  |  6

  A  |  B  |  C
---------------
     |     |   
     |     |  2
     |     |  3
     |     |  4
     |     |  5
     |  1  |  6

  A  |  B  |  C
---------------
     |     |  1
     |     |  2
     |     |  3
     |     |  4
     |     |  5
     |     |  6

Number of moves: 63

Upvotes: 1

Related Questions