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.
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]