Question
I need help debugging this program. It's an iterative towers of Hanoi problem. import java.io.PrintWriter; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class TowersOfHanoiIteration {
I need help debugging this program. It's an iterative towers of Hanoi problem.
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class TowersOfHanoiIteration {
class Stack {
//define variables needed to create a stack
int maxSize;
int topOfArray;
int array[];
PrintWriter out;
}
//create stack and define max size held by stack
Stack newStack(int maxSize) {
Stack stack = new Stack();
stack.maxSize = maxSize;
stack.topOfArray = -1;
stack.array = new int[maxSize];
return stack;
}
//define parameters if stack is full and if stack is empty. Return to user in either case.
boolean stackIsFull (Stack stack) {
return (stack.topOfArray == stack.maxSize -1);
}
boolean stackIsEmpty (Stack stack) {
return (stack.topOfArray == -1 );
}
//remove items from stack. Will decrease stack capacity by 1.
int pop (Stack stack) {
if (stackIsEmpty(stack))
return Integer.MIN_VALUE;
return stack.array[stack.topOfArray--];
}
//add items to stack. Will increase stack capacity by 1.
void push (Stack stack, int data) {
if (stackIsFull(stack))
return;
stack.array[++stack.topOfArray] = data;
}
//movement between towers by initiation of two stacks defined as Tower1 and Tower2.
void moveBetweenTowers (Stack Tower1, Stack Tower2, char start, char next) {
int Tower1Top = pop(Tower1);
int Tower2Top = pop(Tower2);
//Tower1 is empty
if (Tower1Top == Integer.MIN_VALUE) {
push(Tower1, Tower2Top);
moveDisk (next, start, Tower2Top);
//Tower2 is empty
} else if (Tower2Top == Integer.MIN_VALUE) {
push(Tower2, Tower1Top);
moveDisk(start, next, Tower1Top);
//Occurence when disk of Tower1 is greater than disk of Tower2.
} else if (Tower1Top > Tower2Top) {
push(Tower1, Tower1Top);
push(Tower1, Tower2Top);
moveDisk(next, start, Tower2Top);
//occurence when disk of Tower2 is greater than disk of Tower1.
} else {
push(Tower2, Tower2Top);
push(Tower2, Tower1Top);
moveDisk(start, next, Tower1Top);
}
}
//disk movement defined
void moveDisk (char startTower, char nextTower, int Disk) {
System.out.println("Move disk " + Disk + " from " + startTower + " to " + nextTower);
}
void TowersOfHanoiIterative (int totalDisks, Stack Tower1, Stack stackEmpty, Stack Tower2) {
int n;
int maxMoves;
char start ='A', next = 'B', a ='C';
//if even move disk to empty Tower. Exchange labels of Towers.
if (totalDisks % 2 == 0) {
char temp = next;
next = start;
start = temp;
}
//For loop defined. Make movement of larger disks first
maxMoves = (int)(Math.pow(2, totalDisks) - 1);
for (n = totalDisks; n >= 1; n--)
push(Tower1, n);
for (n =1; n <=maxMoves; n++) {
if (n % 3 == 1)
moveBetweenTowers(Tower1,Tower2, start, next);
else if (n % 3 == 2)
moveBetweenTowers(Tower1, stackEmpty, start, a);
else if (n % 3 == 0 )
moveBetweenTowers(stackEmpty, Tower2, a, next);
}
}
public static void main (String args[]) throws IOException {
PrintWriter out = new PrintWriter (new FileWriter("IterativeTowers.txt"));
System.out.println("Enter number of disks: ");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
TowersOfHanoiIteration ob = new TowersOfHanoiIteration();
Stack Tower1, Tower2, stackEmpty;
Tower1 = ob.new Stack();
Tower2 = ob.new Stack();
stackEmpty = ob.new Stack();
int totalDisks;
ob.TowersOfHanoiIterative(totalDisks, Tower1, stackEmpty, Tower2);
long start = System.nanoTime();
long end = System.nanoTime();
out.println("Total Time: " + (end- start) + " nano seconds.");
System.out.println("Execution completed");
out.close();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started