Tower of Hanoi - Java

893 views Asked by At

I have this Tower of Hanoi program in java and I'm able to get it to work, but I can't for the life of me figure out how to get it to show how it's going from, for example, "Disk 1 From TowerA to TowerC", "Disk 2 From TowerA to TowerB", etc..

Here's my code:

Is there a way to add the "Disk # From (TowerA, TowerB, or TowerC) to (TowerA, TowerB, or TowerC)"? Any help would be much appreciated.

2

There are 2 answers

0
ManojP On BEST ANSWER

try this

public class TowerApp
{
    static int nDisks = 3;
    static public LinkStack A = new LinkStack("A");
    static public LinkStack B = new LinkStack("B");
    static public LinkStack C = new LinkStack("C");


    static public void doTowers(int nDisks, LinkStack source, LinkStack temp, LinkStack dest)
    {
        if(nDisks <= 4)
            if ((nDisks % 2) == 0)
            {   
                displayStacks(source, temp, dest);
                nDisks = nDisks - 1;
                long dn = source.pop();
                temp.push(dn);
                System.out.println("Disk # "+dn+" moved from Tower "+source.getName() +" to Tower "+temp.getName());
                displayStacks(dest, source, temp);

                dn = source.pop();
                dest.push(dn);
                System.out.println("Disk # "+dn+" moved from Tower "+source.getName() +" to Tower "+dest.getName());

                doTowers(nDisks, temp, source, dest);
            } 
            else
            {
                displayStacks(source, dest, temp);
                nDisks = nDisks - 1;
                long dn =  source.pop();
                dest.push(dn);
                System.out.println("Disk # "+dn+" moved from Tower "+source.getName() +" to Tower "+dest.getName());
                displayStacks(temp, source, dest);
            }

        /*          else if (nDisks >= 5)
        {
            doTowers(nDisks - 2, source, temp, dest);
            temp.push(source.pop());
            doTowers(nDisks - 2, dest, source, temp);
            dest.push(source.pop());
            doTowers(nDisks - 1, temp, source, dest);
        }*/
    }
    static public void displayStacks(LinkStack source, LinkStack temp, LinkStack dest)
    {           
        long n = source.pop();
        temp.push(n);
        System.out.println("Disk #"+n+" moved from Tower "+source.getName() +" to Tower "+temp.getName());
        PrintStacks();
        n = source.pop();
        dest.push(n);
        System.out.println("Disk #"+n+" moved from Tower "+source.getName() +" to Tower "+dest.getName());
        PrintStacks();
        n= temp.pop();
        dest.push(n);
        System.out.println("Disk #"+n+" moved from Tower "+temp.getName() +" to Tower "+dest.getName());
        PrintStacks();
    }

    static public void PrintStacks()
    {
        A.displayStack("TowerA");
        B.displayStack("TowerB");
        C.displayStack("TowerC");
        System.out.println("");
    }

    public static void main(String[] args)
    {
        for (int i = nDisks; i >= 1; i--)
        {
            A.push(i);
        }
        PrintStacks();

        doTowers(nDisks, A, B, C);
    }
}

and LinkStack class look like

class LinkStack
{

    private LinkedList theList;
    private String name;

    public LinkStack(String name)             // constructor
    {
        theList = new LinkedList();
        this.name =  name;
    }
    public void push(long j)     // put item on top of stack
    {
        theList.addFirst(j);
    }
    public long pop()            // take item from top of stack
    {
        return (Long)theList.removeFirst();
    }
    public boolean isEmpty()       // true if stack is empty
    {
        return ( theList.isEmpty() );
    }
    public void displayStack(String name)
    {
        System.out.print(name + ": (top-->bottom): ");
        System.out.println(theList);
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

output :

TowerA: (top-->bottom): [1, 2, 3]
TowerB: (top-->bottom): []
TowerC: (top-->bottom): []

Disk #1 moved from Tower A to Tower C
TowerA: (top-->bottom): [2, 3]
TowerB: (top-->bottom): []
TowerC: (top-->bottom): [1]
0
user1438038 On

To get the stack's name, add a getter method to your LinkStack class:

public String getName() {
  return this.name;
}

To create your desired output, call System.out.println(...) in your method called doTowers(). To get the name of your current stack, you can call source.getName(), temp.getName() and dest.getName() then.

Furthermore, it can be useful to add a peek() method to your LinkStack class, which returns the internal list's first element, without removing it. Alternatively, you can store the return value of source.pop() in a local variable, so you can output it and pass it to dest.push() afterwards.