I have a towers of hanoi puzzle program that is getting closer to being done. My problem right now is trying to get my input from the user to work properly.
If they type 'v' or 'V', then the steps to solve the puzzle will be displayed (Hence the output would be 'Move Disc from S to D' and so forth) . Else, if the user does not type 'v' or 'V', then the program proceeds with in solving the puzzle, displaying the Total moves but not displaying the steps.
The problem I am having is the options aren't working like they are supposed to.
Now the only thing wrong is when the user input's 'v' or 'V', the moves are not displayed correctly. Output:
Enter the min number of discs : 2 Press 'v' or 'V' for a list of moves v Move disc from needle S to A Total Moves : 3
How can I accomplish having the moves displayed if the user types 'v' or 'V', and if the user types some other than this the output just displays the 'Total Moves'?
Here is my code:
import java.util.*; import java.util.Scanner; public class TowerOfHanoi4 { static int moves=0; public static void main(String[] args) { System.out.println("Enter the min number of discs : "); Scanner scanner = new Scanner(System.in); int iHtMn = scanner.nextInt(); //iHeightMin char source='S', auxiliary='D', destination='A'; //name poles or 'Needles' System.out.println("Press 'v' or 'V' for a list of moves"); Scanner show = new Scanner(System.in); String c = show.next(); // char lstep='v', lsteps='V'; // grab option v or V if (c.equalsIgnoreCase("v")){ //if option is not v or V, execute code and only display total moves hanoi(iHtMn, source, destination, auxiliary); //else, user typed v or V and moves are displayed System.out.println(" Move disc from needle "+source+" to "+destination); System.out.println(" Total Moves : "+moves); } else { hanoi(iHtMn, source, destination, auxiliary); System.out.println(" Total Moves : "+moves); } } public static void hanoi(int htmn,char source,char destination,char auxiliary) { if (htmn >=1) { hanoi(htmn-1, source, auxiliary, destination); // move n-1 disks from source to auxilary // System.out.println(" Move disc from needle "+source+" to "+destination); // move nth disk to destination moves++; hanoi(htmn-1, auxiliary, destination, source);//move n-1 disks from auxiliary to Destination } } }