Guest User

Untitled

a guest
Nov 5th, 2013
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TowerOfHanoi666 {
  4.     static int moves = 0;
  5.     static boolean displayMoves = false;
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         System.out.println("Enter the Number of Discs : ");
  10.         Scanner scanner = new Scanner(System.in);
  11.         int iHeight = scanner.nextInt();
  12.         char source = 'S', auxiliary = 'D', destination = 'A'; // name poles or
  13.                                                                 // 'Needles'
  14.         System.out.println("Press 'v' or 'V' for a list of moves");
  15.         Scanner show = new Scanner(System.in);
  16.         String c = show.next();
  17.         displayMoves = c.equalsIgnoreCase("v");
  18.  
  19.         hanoi(iHeight, source, destination, auxiliary);
  20.         System.out.println(" Total Moves : " + moves);
  21.     }
  22.  
  23.     static void hanoi(int height, char source, char destination, char auxiliary) {
  24.         if (height >= 1) {
  25.             hanoi(height - 1, source, auxiliary, destination);
  26.             if (displayMoves) {
  27.                 System.out.println(" Move disc from needle " + source + " to "
  28.                         + destination);
  29.             }
  30.             moves++;
  31.             hanoi(height - 1, auxiliary, destination, source);
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment