In: Computer Science
My recursive Tower of Hanoi program does not render the correct index numbers. Also, it does not prompt user to enter the starting rod letter and the ending rod letter. Could you help me fix it?
Main.java
import java.io.*;
import java.util.List;
import java.util.Scanner;
public class Main {
int index = 1;
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 B without
ever putting a larger disk on top of a smaller disk.*/
System.out.println();
System.out.println("Tower of Hanoi");
System.out.println("Program by Quang Pham");
System.out.println();
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);
}
if (startingrod == 'A' && endingrod =='C')
{
System.out.println("OK. Starting with discs 1, 2, 3, on rod A \n");
System.out.println("Moves are:");
int count = playHanoi (3,"A","B","C");
System.out.println();
System.out.println("All done.");
System.out.println("Took a total of " + count + " moves.");
System.out.println();
}
}
private static int playHanoi( int n, String from , String other, String to)
{ int count = 1;
//int index = 1;
if (n == 0)
return 0;
else if(n == 1)
{
System.out.printf("%d." + " Move disk "+ n +" from rod %s to rod %s \n", count, from, to);
//index++;
return count;
}
else{
count+= playHanoi(n-1, from, to, other);
System.out.printf("%d." + " Move disk " + n + " from rod %s to rod %s \n ", count, from, to);
count+=playHanoi(n-1, other, from, to);
}
//
return count;
}
}
The directions are:
Tower of Hanoi Program by YOUR NAME Enter Starting Rod (A, B, or C): A Enter Ending Rod (A, B, or C): A Sorry. starting and ending rod cannot be the same. Enter Ending Rod ((A, B, or C): C OK Starting with discs 1, 2, and 3 on rod A Moves are as follows: 1. Move disc 1 from rod A to rod C 2. Move disc 2 from rod A to rod B 3. Move disc 1 from rod C to rod B 4. Move disc. 3 from rod A to rod C 5. Move disk 1 from rod B to rod A 6. Move disk 2 from rod B to rod C 7. Move disk 1 from rod A to rod C All done. Took a total of 7 moves.
Any help with this program is greatly appreciated. Yours truly, Quang Pham
Here is the correction in your code.
import java.io.*;
import java.util.List;
import java.util.Scanner;
public class Main {
// int index = 1;
static int count = 1;
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 B without ever putting a larger disk on top of a smaller disk.*/
System.out.println("\nTower 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 discs 1, 2, 3, on rod " + startingrod + "\n");
System.out.println("Moves are:");
playHanoi (3,startingrod,other,endingrod);
//index++;
System.out.println();
System.out.println("All done.");
System.out.println("Took a total of " + (count - 1) + " moves.");
sc.close();
System.out.println();
}
}
private static void playHanoi( int n, char from , char other, char to)
{
// int count = 1;
if (n == 0)
return;
else if(n == 1)
{
System.out.printf("%d. " + "Move disk "+ n +" from rod %c to rod %c \n", /*index*/ count, from, to);
//index++;
count++;
// return count;
}
else
{
playHanoi(n - 1, from, to, other);
System.out.printf("%d. " + "Move disk " + n + " from rod %c to rod %c \n", /*index*/ count, from, to);
count++;
playHanoi(n - 1, other, from, to);
//index++;
// return count;
}
return;
}
}
The only case you handled in the previous code is when starting rod is A and ending index is C.