Moving Top Disc Tower Of Hanoi Using Recursion Java -


i have weird issue whenever program attempts solve tower of hanoi puzzle. whenever tries solve it, move first 2 discs on end pole (the pole way right), move remainder of discs starting pole. example, if have tower of hanoi 10 discs, move first several discs start pole, top 2 make end pole. rest of discs end on first pole. when this, gives me index out of bounds error. not sure going wrong it, , appreciated. in advance.

public class towerofhanoi { private int[] towerone;   private int[] towertwo;   private int[] towerthree;   private int discone;   private int disctwo;   private int discthree;      /* construct towers of hanoi (3 towers) anumdisc  * on first tower. each tower can identified  * integer number (0 first tower, 1 second  * tower, , 2 third tower). each disc can identified  * integer number starting 0 (for smallest disc)  * , (anumdisc - 1) largest disc.  */ public towerofhanoi(int anumdiscs) {     towerone = new int[anumdiscs];         for(int = 0; < anumdiscs; i++){             towerone[i] = anumdiscs - 1 - i;         }         towertwo = new int[anumdiscs];         towertwo[0] = anumdiscs;         towerthree = new int[anumdiscs];         towerthree[0] = anumdiscs;         discone = anumdiscs;         disctwo = 0;         discthree = 0;   }  /* returns array of integer representing order of  * discs on tower (from bottom up). bottom disc should  * first element in array , top disc should  * last element of array. size of array must  * number of discs on tower. example, suppose  * tower 0 contains following discs 0,1,4,6,7,8 (from top  * bottom). method should return array [8,7,6,4,1,0]  * (from first last).   * @param tower integer identify tower number.  * @return array of integer representing order of discs.  */ public int[] getarrayofdiscs(int tower) {     int[] temptower;         if(tower == 0){             temptower = new int[discone];              for(int = 0; < discone; i++){                     temptower[i] = towerone[i];              }              return temptower;         }         if(tower == 1){             temptower = new int[disctwo];             for(int = 0; < disctwo; i++){                 temptower[i] = towertwo[i];             }             return temptower;             }         if(tower == 2){             temptower = new int[discthree];              for(int = 0; < discthree; i++){                     temptower[i] = towerthree[i];              }              return temptower;         }         return towerone;     }  /* gets total number of discs in towers of hanoi  * @return total number of discs in towers of hanoi  */ public int getnumberofdiscs() {     return discone+disctwo+discthree;  }  /* gets number of discs on tower.  * @param tower tower identifier (0, 1, or 2)  * @return number of discs on tower.  */ public int getnumberofdiscs(int tower) {     if(tower == 0){             return discone;         }         if(tower == 1){              return disctwo;         }         if(tower == 2){              return discthree;         }         return 0; }  /* moves top disc fromtower totower. note  * operation has follow rule of tower of hanoi  * puzzle. first fromtower must have @ least 1 disc , second  * top disc of totower must not smaller top disc  * of fromtower.  * @param fromtower source tower  * @param totower destination tower  * @return true if move top disc  *         fromtower totower.  */ public boolean movetopdisc(int fromtower, int totower) {         if((fromtower == 0 && discone == 0)||(fromtower == 1 && disctwo == 0) || (fromtower == 2 && discthree == 0)){                 return false;             }             if(fromtower == 0){                 if(totower == 1){                         if(disctwo != 0&&towerone[discone-1]>towertwo[disctwo-1]){                         return false;                     }                     else{                         towertwo[disctwo]=towerone[discone-1];                         towerone[discone-1] = 0;                         discone--;                         disctwo++;                         return true;                     }                 }                 if(totower == 2){                     if(discthree != 0&&towerone[discone-1] > towerthree[discthree-1]){                         return false;                     }                     else{                         towerthree[discthree] = towerone[discone-1];                         towerone[discone-1] = 0;                         discone--;                         discthree++;                         return true;                     }                     }                 }             if(fromtower == 1){                 if(totower == 0){                     if(discone != 0&&towertwo[disctwo-1]>towerone[discone-1]){                         return false;                     }                     else{                         towerone[discone]=towertwo[disctwo-1];                         towertwo[disctwo-1] = 0;                         disctwo--;                         discone++;                         return true;                     }                 }                     if(totower == 2){                     if(discthree!= 0&&towertwo[disctwo-1] > towerthree[discthree-1]){                         return false;                     }                     else{                         towerthree[discthree] = towertwo[disctwo-1];                         towertwo[disctwo-1] = 0;                         disctwo--;                         discthree++;                         return true;                     }                     }                  }              if(fromtower == 2){                 if(totower == 0){                     if(discone !=0 && towerone[discone-1]>towertwo[disctwo-1]){                         return false;                     }                     else{                         towerone[discone]=towerthree[discthree-1];                         towerthree[discthree-1] = 0;                         discthree--;                         discone++;                         return true;                     }                 }                 if(totower == 1){                     if(discthree !=0&&towerthree[discthree-1] > towertwo[disctwo-1]){                         return false;                     }                     else{                         towertwo[disctwo] = towerthree[discthree-1];                         towerthree[discthree-1] = 0;                         discthree--;                         disctwo++;                         return true;                     }                     }                 }                  return false; } } 

this class using run program above.

import javax.swing.jframe;  public class thsolverframe { public static void main(string[] args) throws interruptedexception {     int numberofdiscs = 10;     towerofhanoi towers = new towerofhanoi(numberofdiscs);     thcomponent thc = new thcomponent(towers);       jframe frame = new jframe();     frame.settitle("tower of hanoi");     frame.setsize(500,500);     frame.setdefaultcloseoperation(jframe.exit_on_close);      frame.add(thc);      frame.setvisible(true);      thread.sleep(5000);      solvetower(towers, thc, numberofdiscs, 0, 1, 2);      system.out.println("done!!!"); }  public static void solvetower(towerofhanoi towers, thcomponent thc, int numberofdiscs, int startpole, int temppole, int endpole) throws interruptedexception {     if(numberofdiscs == 1) {         towers.movetopdisc(startpole, endpole);         thc.repaint();         thread.sleep(100);     }      else {         solvetower(towers, thc, numberofdiscs - 1, startpole, endpole, temppole);         towers.movetopdisc(startpole, endpole);         thc.repaint();         thread.sleep(100);         solvetower(towers, thc, numberofdiscs - 1, temppole, startpole, endpole);      } } } 

i tracked down 2 lines in movetopdisk() method. first 1 this:

if(fromtower == 2){             if(totower == 0){                 if(discone !=0 && towerone[discone-1]>towertwo[disctwo-1]){ <---- here 

the third if statement here trying access towertwo when should using towerthree , discthree changed this:

    if (fromtower == 2) {         if (totower == 0) {             if (discone != 0 && towerone[discone - 1] > towerthree[discthree - 1]) { 

the way before, code trying pull disc tower without discs on , causing error. after running again, found such typo within same area. :

if(totower == 1){                 if(discthree !=0&&towerthree[discthree-1] > towertwo[disctwo-1]){ 

this second if statement targeting discthree, when should using disctwo.

if(totower == 1){                 if(disctwo !=0&&towerthree[discthree-1] > towertwo[disctwo-1]){ 

after these changes, code ran me, error free. issue had after unable solve puzzle! algorithm not able solve puzzle above 3 discs. tried 3, 4, 5, , 10 , solved 3. 4 , 5, program stopped, not in winning configuration , when tried 10, managed shuffle first 3 discs around , never come solution (i let run solid 5 minutes in case).

tl;dr suggestions careful of copy/pasting, mindful of whether using zero-index or not, , should take @ algorithm again see if can solve puzzle. myself have not written hanoi puzzle, i'm not familiar how implement in code. see have idea though. being solve puzzle n discs, first have solve n-1 discs. luck on rest of work!


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -