Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class TowerOfHanoi666 {
- static int moves = 0;
- static boolean displayMoves = false;
- public static void main(String[] args) {
- System.out.println("Enter the Number of Discs : ");
- Scanner scanner = new Scanner(System.in);
- int iHeight = scanner.nextInt();
- 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();
- displayMoves = c.equalsIgnoreCase("v");
- hanoi(iHeight, source, destination, auxiliary);
- System.out.println(" Total Moves : " + moves);
- }
- static void hanoi(int height, char source, char destination, char auxiliary) {
- if (height >= 1) {
- hanoi(height - 1, source, auxiliary, destination);
- if (displayMoves) {
- System.out.println(" Move disc from needle " + source + " to "
- + destination);
- }
- moves++;
- hanoi(height - 1, auxiliary, destination, source);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment