In: Computer Science
I have a recursive Tower of Hanoi program and would like to get the index numbers to be in correct sequence: 1,2,3,4,5,6,7. How can I do this?
Main.java
import java.io.*;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
/**There is a stack of N disks on the first of three poles (call them A, B and C) and your job is to move the disks from pole A to pole C without ever putting a larger disk on top of a smaller disk.*/
System.out.println("\nW7L1 Tower of Hanoi");
System.out.println("Program by Quang Pham\n");
char startingrod = ' ';
char endingrod = ' ';
Scanner sc = new Scanner(System.in);
System.out.println("Enter the starting rod letter (A, B, or C).");
startingrod = sc.next().charAt(0);
System.out.println("Enter the ending rod letter (A, B, C).");
endingrod = sc.next().charAt(0);
if (startingrod == endingrod)
{
System.out.println("Sorry. Starting rod and ending rod cannot be the same.");
System.out.println("Enter ending rod letter (A, B, C)");
endingrod = sc.next().charAt(0);
}
else if ((startingrod=='A' && endingrod=='C')||(startingrod=='C' && endingrod=='A'));
{
char other = (char)('A' + 'B' + 'C' - startingrod - endingrod) ;
System.out.println("OK. Starting with disks 1, 2, 3, on rod " + startingrod + "." + "\n");
System.out.println("Moves are as follows:");
int count = playHanoi (3,startingrod,other,endingrod);
System.out.println();
System.out.printf("All disks 1, 2, 3 are on rod %c.\n", endingrod);
System.out.println("All done.");
System.out.println("Took a total of " + count + " moves.");
sc.close();
System.out.println();
}
}
private static int playHanoi( int n, char from , char other, char to)
{
int index = 1;
int count = 1;
if (n == 0)
return 0;
else if(n == 1)
{
System.out.printf("%d.", index);
System.out.printf(" Move disk "+ n +" from rod %c to rod %c \n", from, to);
index++;
return count;
}
else
{
count+= playHanoi(n - 1, from, to, other);
System.out.printf("%d.", index);
System.out.printf(" Move disk " + n + " from rod %c to rod %c \n", from, to);
index++;
count+= playHanoi(n - 1, other, from, to);
index++;
return count;
}
}
}
The program is at: https://repl.it/@quangnvpham1/W7L1-Recursive-Tower-of-Hanoi-Program#Main.java .
Any help with this problem is greatly appreciated. Yours truly, Quang Pham
I run your code and get your point . Please attach output always if you do have problem in that .
As you mentioned you want to get the index numbers in correct sequence :-
One alternative for that is make your index global and static so that value of it remains unchanged or i can say constant while moving back in recursion. For code you can see below:-
import java.io.*;
import java.util.List;
import java.util.Scanner;
public class Main {
static int index=1; // Firstly i make the index variable global and initializing it with 1 as value.
public static void main(String[] args){
/**There is a stack of N disks on the first of three poles (call them A, B and C) and your job is to move the disks from pole A to pole C without ever putting a larger disk on top of a smaller disk.*/
System.out.println("\nW7L1 Tower of Hanoi");
System.out.println("Program by Quang Pham\n");
char startingrod = ' ';
char endingrod = ' ';
Scanner sc = new Scanner(System.in);
System.out.println("Enter the starting rod letter (A, B, or C).");
startingrod = sc.next().charAt(0);
System.out.println("Enter the ending rod letter (A, B, C).");
endingrod = sc.next().charAt(0);
if (startingrod == endingrod)
{
System.out.println("Sorry. Starting rod and ending rod cannot be the same.");
System.out.println("Enter ending rod letter (A, B, C)");
endingrod = sc.next().charAt(0);
}
else if ((startingrod=='A' && endingrod=='C')||(startingrod=='C' && endingrod=='A'));
{
char other = (char)('A' + 'B' + 'C' - startingrod - endingrod) ;
System.out.println("OK. Starting with disks 1, 2, 3, on rod " + startingrod + "." + "\n");
System.out.println("Moves are as follows:");
int count = playHanoi (3,startingrod,other,endingrod);
System.out.println();
System.out.printf("All disks 1, 2, 3 are on rod %c.\n", endingrod);
System.out.println("All done.");
System.out.println("Took a total of " + count + " moves.");
sc.close();
System.out.println();
}
}
private static int playHanoi( int n, char from , char other, char to)
{
// int index = 1;
int count = 1;
if (n == 0)
return 0;
else if(n == 1)
{
System.out.printf("%d.", index);
System.out.printf(" Move disk "+ n +" from rod %c to rod %c \n", from, to);
index++;
return count;
}
else
{
count+= playHanoi(n - 1, from, to, other);
System.out.printf("%d.", index);
System.out.printf(" Move disk " + n + " from rod %c to rod %c \n", from, to);
index++;
count+= playHanoi(n - 1, other, from, to);
index--; // I just decrease the count of index by 1 as it will increase two times in the recursion due to //which it skips index 4 always.
index++;
return count;
}
}
}
While running the code just remove the comments and the code will work fine.