Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.48 KB | None | 0 0
  1. public class TowerOfHanoi
  2. {
  3.  
  4.         int[] ace;
  5.         int[] dos;
  6.         int[] thrice;
  7.         int first;
  8.         int second;
  9.         int third;
  10.        
  11.     /* Construct the Towers of Hanoi (3 towers) with aNumDisc
  12.      * on the first tower. Each tower can be identified by an
  13.      * integer number (0 for the first tower, 1 for the second
  14.      * tower, and 2 for the third tower). Each disc can be identified
  15.      * by an integer number starting from 0 (for the smallest disc)
  16.      * and (aNumDisc - 1) for the largest disc.
  17.      */
  18.     public TowerOfHanoi(int aNumDiscs)
  19.     {
  20.                 ace = new int[aNumDiscs];
  21.                 for(int i = 0; i < aNumDiscs; i++){
  22.                     ace[i] = aNumDiscs - 1 - i;
  23.                 }
  24.                 dos = new int[aNumDiscs];
  25.                 dos[0] = aNumDiscs;
  26.                 thrice = new int[aNumDiscs];
  27.                 thrice[0] = aNumDiscs;
  28.                 first = aNumDiscs;
  29.                 second = 0;
  30.                 third = 0;
  31.     }
  32.    
  33.     /* Returns an array of integer representing the order of
  34.      * discs on the tower (from bottom up). The bottom disc should
  35.      * be the first element in the array and the top disc should be
  36.      * the last element of the array. The size of the array MUST
  37.      * be the number of discs on the tower. For example, suppose
  38.      * the tower 0 contains the following discs 0,1,4,6,7,8 (from top
  39.      * to bottom). This method should return the array [8,7,6,4,1,0]
  40.      * (from first to last).
  41.      * @param tower the integer identify the tower number.
  42.      * @return an array of integer representing the order of discs.
  43.      */
  44.     public int[] getArrayOfDiscs(int tower)
  45.     {
  46.             int[] tempTower;
  47.         if(tower == 0){
  48.                     tempTower = new int[first];
  49.                     for(int i = 0; i < first; i++){
  50.                         tempTower[i] = ace[i];
  51.                     }
  52.                     return tempTower;
  53.                 }
  54.                 if(tower == 1){
  55.                     tempTower = new int[second];
  56.                     for(int i = 0; i < second; i++){
  57.                         tempTower[i] = dos[i];
  58.                     }
  59.                 return tempTower;    
  60.                 }
  61.                 if(tower == 2){
  62.                     tempTower = new int[third];
  63.                     for(int i = 0; i < third; i++){
  64.                         tempTower[i] = thrice[i];
  65.                     }
  66.                     return tempTower;
  67.                 }
  68.                 return ace;
  69.     }
  70.     public boolean moveTopDisc(int fromTower, int toTower)
  71.     {
  72.         if((fromTower == 0 && first == 0)||(fromTower == 1 && second == 0)
  73.                         || (fromTower == 2 && third == 0)){
  74.                     return false;
  75.                 }
  76.                 if(fromTower == 0){
  77.                     if(toTower == 1){
  78.                             if(second != 0&&ace[first-1]>dos[second-1]){
  79.                             return false;
  80.                         }
  81.                         else{
  82.                             dos[second]=ace[first-1];
  83.                             ace[first-1] = 0;
  84.                             first--;
  85.                             second++;
  86.                             return true;
  87.                         }
  88.                     }
  89.                     if(toTower == 2){
  90.                         if(third != 0&&ace[first-1] > thrice[third-1]){
  91.                             return false;
  92.                         }
  93.                         else{
  94.                             thrice[third] = ace[first-1];
  95.                             ace[first-1] = 0;
  96.                             first--;
  97.                             third++;
  98.                             return true;
  99.                         }
  100.                         }
  101.                     }
  102.                 if(fromTower == 1){
  103.                     if(toTower == 0){
  104.                         if(first != 0&&dos[second-1]>ace[first-1]){
  105.                             return false;
  106.                         }
  107.                         else{
  108.                             ace[first]=dos[second-1];
  109.                             dos[second-1] = 0;
  110.                             second--;
  111.                             first++;
  112.                             return true;
  113.                         }
  114.                     }
  115.                         if(toTower == 2){
  116.                         if(third!= 0&&dos[second-1] > thrice[third-1]){
  117.                             return false;
  118.                         }
  119.                         else{
  120.                             thrice[third] = dos[second-1];
  121.                             dos[second-1] = 0;
  122.                             second--;
  123.                             third++;
  124.                             return true;
  125.                         }
  126.                         }
  127.                    
  128.                     }
  129.                    
  130.                 if(fromTower == 2){
  131.                     if(toTower == 0){
  132.                         if(first !=0 && ace[first-1]>dos[second-1]){
  133.                             return false;
  134.                         }
  135.                         else{
  136.                             ace[first]=thrice[third-1];
  137.                             thrice[third-1] = 0;
  138.                             third--;
  139.                             first++;
  140.                             return true;
  141.                         }
  142.                     }
  143.                     if(toTower == 1){
  144.                         if(third !=0&&thrice[third-1] > dos[second-1]){
  145.                             return false;
  146.                         }
  147.                         else{
  148.                             dos[second] = thrice[third-1];
  149.                             thrice[third-1] = 0;
  150.                             third--;
  151.                             second++;
  152.                             return true;
  153.                         }
  154.                         }
  155.                     }
  156.                
  157.                     return false;
  158.     }
  159.    
  160.     /* Gets the total number of discs in this Towers of Hanoi
  161.      * @return the total number of discs in this Towers of Hanoi
  162.      */
  163.     public int getNumberOfDiscs()
  164.     {
  165.         return first+second+third;
  166.     }
  167.    
  168.     /* Gets the number of discs on a tower.
  169.      * @param tower the tower identifier (0, 1, or 2)
  170.      * @return the number of discs on the tower.
  171.      */
  172.     public int getNumberOfDiscs(int tower)
  173.     {
  174.         if(tower == 0){
  175.                     return first;
  176.                 }
  177.                 if(tower == 1){
  178.                     return second;
  179.                 }
  180.                 if(tower == 2){
  181.                     return third;
  182.                 }
  183.                 return 0;
  184.     }
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement