Académique Documents
Professionnel Documents
Culture Documents
100. rectangle(x1,y1-
rectangle(98,200,113,posy1[1]) i,x2,y2-i);
1. #include <stdio.h> ; 101. setcolor(BLACK);
2. #include <conio.h> 51. 102. line(x1,y2-(i-
3. #include <graphics.h> rectangle(308,200,323,350); 1),x2,y2-(i-1));
4. 52. 103. setcolor(WHITE);
5. rectangle(518,200,533,350); 104.
6. int colors[7],n; 53. floodfill(100,205, if((posy1[diskpos[nd]]-
7. int sizesx1[7],sizesx2 getmaxcolor()); i)>=190)
[7],sizeax1[7],sizeax2[7],size 54. floodfill(310,205, 105. line(px1,y2-(i-
dx1[7],sizedx2[7]; getmaxcolor()); 1),px2,y2-(i-1));
8. int posy1[7],posy2[7]; 55. floodfill(520,205, 106. }
9. int disks[4]; getmaxcolor()); 107. cx1=x1;cx2=x2;
10. int diskpos[7]; 56. 108. cy1=y1-(i-1);
11. int cx1,cx2,cy1,cy2; 57. for(i=1;i<=nd;i++) 109. cy2=y2-(i-1);
12. void Initialize(int nd 58. { 110. }
) 59. 111.
13. { setfillstyle(1,colors[i]); 112. void MoveDown(int nd,i
14. int i; 60. nt d,int pos)
15. for(i=nd;i>=1;i--) rectangle(sizesx1[i],posy1[i], 113. {
16. { sizesx2[i],posy2[i]); 114. int i;
17. sizesx1[i]=30+ 61. 115. int x1,x2,y1,y2;
((nd-i)*10); floodfill(sizesx1[i] 116. int ey1;
18. sizesx2[i]=180- +1,posy1[i]+1, getmaxcolor()); 117. int px1,px2;
((nd-i)*10); 62. } 118. x1=cx1;x2=cx2;
19. sizeax1[i]=240+ 63. setcolor(RED); 119. y1=cy1;y2=cy2;
((nd-i)*10); 64. 120. ey1=posy1[pos];
20. sizeax2[i]=390- outtextxy(82,355,"SOURCE"); 121. if(d==1)
((nd-i)*10); 65. 122. {
21. sizedx1[i]=450+ outtextxy(285,355,"AUXILIARY") 123. px1=98;
((nd-i)*10); ; 124. px2=113;
22. sizedx2[i]=600- 66. 125. }
((nd-i)*10); outtextxy(485,355,"DESTINATION 126. if(d==2)
23. posy1[i]=339-((nd- "); 127. {
i)*10)-(nd-i); 67. } 128. px1=308;
24. posy2[i]=349-((nd- 68. 129. px2=323;
i)*10)-(nd-i); 69. void MoveUp(int nd,int 130. }
25. s) 131. if(d==3)
26. diskpos[i]=i; 70. { 132. {
27. } 71. int i; 133. px1=518;
28. 72. int x1,x2,y1,y2; 134. px2=533;
29. disks[1]=nd; 73. int px1,px2; 135. }
30. disks[2]=disks[3]=0; 74. if(s==1) 136. for(i=1;
31. 75. { (cy1+i)<=ey1;i++)
32. colors[6]=LIGHTRED;co 76. x1=sizesx1[nd]; 137. {
lors[5]=GREEN; 77. x2=sizesx2[nd]; 138. delay(5);
33. colors[4]=LIGHTBLUE;c 78. px1=98;px2=113; 139.
olors[3]=DARKGRAY; 79. } setcolor(colors[nd]);
34. colors[2]=MAGENTA;col 80. if(s==2) 140.
ors[1]=YELLOW; 81. { rectangle(x1,y1+i-1,x2,y2+i-
35. 82. x1=sizeax1[nd]; 1);
36. } 83. x2=sizeax2[nd]; 141. setcolor(WHITE);
37. 84. px1=308;px2=323; 142.
38. void DrawTowers(int nd 85. } rectangle(x1,y1+i,x2,y2+i);
) 86. if(s==3) 143. setcolor(BLACK);
39. { 87. { 144. line(x1,y1+(i-
40. int i; 88. x1=sizedx1[nd]; 1),x2,y1+(i-1));
41. 89. x2=sizedx2[nd]; 145. setcolor(WHITE);
42. 90. px1=518;px2=533; 146. if((cy1+i)>=200)
rectangle(20,350,190,365); 91. } 147. line(px1,y1+(i-
43. 92. 1),px2,y1+(i-1));
rectangle(230,350,400,365); y1=posy1[diskpos[nd]]; 148. }
44. 93. 149. }
rectangle(440,350,610,365); y2=posy2[diskpos[nd]]; 150.
45. 94. 151. void MoveForward(int n
setfillstyle(SOLID_FILL, for(i=1;i<=(posy1[diskpos[nd]] d,int d)
getmaxcolor()); -100);i++) 152. {
46. floodfill(22,352, 95. { 153. int i;
getmaxcolor()); 96. delay(5); 154. int x1,x2,y1,y2;
47. floodfill(232,352, 97. 155. int ex1;
getmaxcolor()); setcolor(colors[nd]); 156. x1=cx1;x2=cx2;
48. floodfill(442,352, 98. rectangle(x1,y1- 157. y1=cy1;y2=cy2;
getmaxcolor()); (i-1),x2,y2-(i-1)); 158. if(d==2)
49. 99. setcolor(WHITE); 159. ex1=sizeax1[nd];
160. if(d==3)
161. ex1=sizedx1[nd]; 219. { 270. errorcode =
162. 220. int i; graphresult();
163. for(i=1;i<=(ex1- 221. char peg[4] 271. if (errorcode !=
cx1);i++) [25]={"","SOURCE","AUXILIARY", grOk) /* an error occurred
164. { "DESTINATION"}; */
165. delay(5); 222. char msg[55]=""; 272. {
166. 223. char dn[2]; 273. printf("Graphics
setcolor(colors[nd]); 224. dn[0]=nd+48; error: %s\n",
167. rectangle(x1+i- 225. dn[1]='\0'; grapherrormsg(errorcode));
1,y1,x2+i-1,y2); 226. strcpy(msg,"Disk 274. printf("Press
168. setcolor(WHITE); "); any key to halt:");
169. 227. strcat(msg,dn); 275. getch();
rectangle(x1+i,y1,x2+i,y2); 228. strcat(msg," is 276. exit(1); /*
170. setcolor(BLACK); moving from "); terminate with an error code
171. line(x1+(i- 229. strcat(msg,peg[s]); */
1),y1,x1+(i-1),y2); 230. strcat(msg," to "); 277. }
172. } 231. strcat(msg,peg[d]); 278.
173. cx1=x1+(i-1); 232. 279.
174. cx2=x2+(i-1); 233. setcolor(BLACK); settextstyle(10,HORIZ_DIR,2);
175. cy1=y1;cy2=y2; 234. for(i=1;i<=50;i++) 280.
176. } 235. { outtextxy(180,20,"TOWER OF
177. 236. HANOI");
178. void MoveBackward(int line(29,399+i,600,399+i); 281.
nd,int d) 237. } outtextxy(175,50,"------------
179. { 238. setcolor(LIGHTRED); --");
180. int i; 239. 282.
181. int x1,x2,y1,y2; settextstyle(7,HORIZ_DIR,1); 283.
182. int ex1; 240. read:printf("\n\n\n\n\n\n\nEnt
183. x1=cx1;x2=cx2; outtextxy(30,400,msg); er the number of Disk (Minimum
184. y1=cy1;y2=cy2; 241. 1 & Maximum 6):");
185. if(d==1) 242. 284. scanf("%d",&nd);
186. ex1=sizesx1[nd]; setcolor(colors[nd]); 285. if(nd<1 || nd>6)
187. if(d==2) 243. 286. {
188. ex1=sizeax1[nd]; settextstyle(10,HORIZ_DIR,2); 287. printf("Number
189. 244. of Disks must be with in
190. for(i=1;(cx1- outtextxy(180,20,"TOWER OF range\n");
i)>=ex1;i++) HANOI"); 288. getch();
191. { 245. 289. goto read;
192. delay(5); outtextxy(175,50,"------------ 290. }
193. --"); 291. n=nd;
setcolor(colors[nd]); 246. } 292. cleardevice();
194. rectangle(x1- 247. 293.
i+1,y1,x2-i+1,y2); 248. void TowersOfHanoi(int settextstyle(10,HORIZ_DIR,2);
195. setcolor(WHITE); nd,int s,int d,int a) 294.
196. rectangle(x1- 249. { outtextxy(180,20,"TOWER OF
i,y1,x2-i,y2); 250. if(nd>0) HANOI");
197. setcolor(BLACK); 251. { 295.
198. line(x2-(i- 252. outtextxy(175,50,"------------
1),y1,x2-(i-1),y2); TowersOfHanoi(nd-1,s,a,d); --");
199. } 253. Display(nd,s,d); 296.
200. cx1=x1-(i-1); 254. settextstyle(DEFAULT_FONT,HORI
201. cx2=x2-(i-1); MoveDisk(nd,s,d); Z_DIR,1);
202. cy1=y1;cy2=y2; 255. 297. Initialize(nd);
203. TowersOfHanoi(nd-1,a,d,s); 298. DrawTowers(nd);
204. } 256. } 299.
205. 257. } TowersOfHanoi(nd,1,3,2);
206. void MoveDisk(int nd,i 258. 300.
nt s,int d) 259. int main(void) 301.
207. { 260. { 302. setcolor(BLACK);
208. MoveUp(nd,s); 261. int nd,i; 303. for(i=1;i<=50;i++)
209. if((d-s)>0) 262. /* request auto 304. {
210. MoveForward(nd,d); detection */ 305.
211. else 263. int gdriver = line(29,399+i,600,399+i);
212. DETECT, gmode, errorcode; 306. }
MoveBackward(nd,d); 264. char msg[80]; 307. getch();
213. MoveDown(nd,d,(n- 265. 308. return 0;
disks[d])); 266. /* initialize 309. }
214. diskpos[nd]=n- graphics and local variables
disks[d]; */
215. disks[s]--;disks[d] 267. initgraph(&gdriver,
++; &gmode, "d:\\tc\\bgi");
216. } 268.
217. 269. /* read result of
218. void Display(int nd,in initialization */
t s,int d)