towerofhanoi.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
/*-------------------------- INCLUDE FILES -------------------------------*/ #include<iostream.h> #include<graphics.h> #include<process.h> #include<fstream.h> #include<process.h> #include<conio.h> #include<stdio.h> #include<math.h> #include<io.h> #define ESC 27 /*------------------------ GLOBAL DECLERATIONS -----------------------------*/ ofstream fout ; int i,j,k; int X[10] ; int count ; /*------------------------ FUNCTION DECLERATIONS ----------------------------*/void InitializeScreen() ; void transfer(int,char,char,char) ; void StartSimulation(int) ; void Disk(int) ; /*----------------------------- MAIN DADDY -------------------------------*/void main() { int n; // To Keep The Graphical Solution Neat Only Upto 9 Disks Are Alloweddo { clrscr() ; cout<<endl<<"ENTER THE NO OF DISKS : "; cin>>n ; if(n>9 || n<1) { cout<<"\nILLEGAL VALUE FOR No. OF DISKS ENTER BETWEEN 1-10" ; getch() ; } } while(n>9 || n<1) ; /* Store The Solution In A File First Note: Could Have Used An Array Or Structure. */ fout.open("database.dat") ; // Recurrsevly Call The Transfer Function To Devise A Solution cout<<"\n\nS---O---L---U---T---I---O---N\n\n" ; cout<<"\n\tTotal No. Of Steps Required -> "<<(pow(2,n)-1)<<"\n\n" ; transfer(n,'L','R','C'); cout<<"\n\nPress Any Key To View The Solution Graphically ...." ; cout<<"\n\n\ (Hey! Would Somebody Tell Me Where To Find The Key Named 'ANY' On My Keyboard)" ; getch(); InitializeScreen() ; fout.close() ; // Display 'n' Disks Initially Disk(n) ; // Fasten Ur Seat Belts. Simulation Starts StartSimulation(n) ; getch(); } /**************************************** Function Which Finds The Solution *****************************************/void transfer(int n,char from,char to,char temp) { if(n>0) { transfer(n-1,from,temp,to); count++ ; if(count>24) { cout<<"\n\tPress Any Key To Continue...\n\n" ; getch() ; count=0 ; } cout<<"MOVE DISK "<<n<<" FROM "<<from<<" TO "<<to<<endl; fout<<n<<from<<to<<endl ; transfer(n-1,temp,to,from); } } /****************************************** Initialize Graphics And The Three Pegs *******************************************/void InitializeScreen() { int gDriver=DETECT,gMode ; initgraph(&gDriver,&gMode," ") ; setcolor(LIGHTGREEN) ; line(10,475,600,475) ; line(100,470,100,100) ; line(300,470,300,100) ; line(500,470,500,100) ; settextstyle(TRIPLEX_FONT,HORIZ_DIR,4) ; setcolor(YELLOW) ; outtextxy(90,50,"L") ; outtextxy(290,50,"C") ; outtextxy(490,50,"R") ; } /**************** DISPLAY DISKS *****************/void Disk(int n) { int x1,y1,x2,y2 ; int k ; setcolor(MAGENTA) ; for(i=1,k=n,x1=5,x2=x1+190,y1=470,y2=y1-20 ; i<=n ; i++,x1+=10,x2-=10,y1-=20,y2-=20,k--) { rectangle(x1,y1,x2,y2) ; X[k]=x1 ; } } /**************************************************************** SHOW SOLUTION STEP BY STEP GRAPHICALLY AS USER PRESSES A KEY *****************************************************************/void StartSimulation(int n) { char charac[3] ; char from,to ; char ch ; int disk ; int l,r,c ; int x1,y1 ; int length ; int height=20 ; l=n ; r=0 ; c=0 ; ifstream fin ; fin.open("database.dat") ; while(fin) { ch=getch() ; if(ch==ESC) exit(0) ; fin>>charac ; disk=(int)charac[0] ; disk-=48 ; from=charac[1] ; to=charac[2] ; x1=X[disk] ; gotoxy(2,2) ; cout<<"Move Disk "<<disk<<" From "<<from<<" TO "<<to ; if(from=='L' && to=='C') { length=2*(100-x1) ; y1=470-l*20 ; setcolor(BLACK) ; rectangle(x1,y1,x1+length,y1+height) ; l-- ; c++ ; x1=x1+200 ; X[disk]=X[disk]+200 ; y1=470-c*20 ; } if(from=='C' && to=='L') { length=2*(300-x1) ; y1=470-c*20 ; setcolor(BLACK) ; rectangle(x1,y1,x1+length,y1+height) ; c-- ; l++ ; x1=x1-200 ; X[disk]=X[disk]-200 ; y1=470-l*20 ; } if(from=='C' && to=='R') { length=2*(300-x1) ; y1=470-c*20 ; setcolor(BLACK) ; rectangle(x1,y1,x1+length,y1+height) ; r++ ; c-- ; x1=x1+200 ; X[disk]=X[disk]+200 ; y1=470-r*20 ; } if(from=='R' && to=='C') { length=2*(500-x1) ; y1=470-r*20 ; setcolor(BLACK) ; rectangle(x1,y1,x1+length,y1+height) ; r-- ; c++ ; x1=x1-200 ; X[disk]=X[disk]-200 ; y1=470-c*20 ; } if(from=='L' && to=='R') { length=2*(100-x1) ; y1=470-l*20 ; setcolor(BLACK) ; rectangle(x1,y1,x1+length,y1+height) ; l-- ; r++ ; x1=x1+400 ; X[disk]=X[disk]+400 ; y1=470-r*20 ; } if(from=='R' && to=='L') { length=2*(500-x1) ; y1=470-r*20 ; setcolor(BLACK) ; rectangle(x1,y1,x1+length,y1+height) ; r-- ; l++ ; x1=x1-400 ; X[disk]=X[disk]-400 ; y1=470-l*20 ; } setcolor(MAGENTA) ; rectangle(x1,y1,x1+length,y1+height) ; // TO REMOVE THE LAST USELESS RECTANGLEif(l==0 && c==0) break ; } fin.close() ; } |