Tower Of Hanoi: It is a mathematical problem where there are three towers and N numbers of disks. The problem is to move all the disks from the first tower to the third tower with the following rules:
Only one disk can be moved at a time and cannot move two or more than two disks at a time.
While moving the disk user have to remember that the smaller disk will always be on top of a bigger one.
That means the bigger one will be at the bottom of the tower & the smaller one will be at the top of the tower.
Function Used:
rectangle(l, t, r, b): A function from graphics.h header file which draw a rectangle from left(l) to right(r) & from top(t) to bottom(b).
line(a1, b1, a2, b2): A function from graphics.h header file which draws a line from the point (a1, b1) to the point (a2, b2).
setfillstyle( pattern, color): A function from graphics.h header file by which we can give a pattern of drawing & also a specific color.
floodfill( a, b, c): A function from graphics.h header file by which we can color in a specific bounded area with (a, b) as the center & c as the border color.
outtextxy(int x, int y, char *string): A function from graphics.h header file by which we can print any statements where, x, y are coordinates of the point and, the third argument contains the address of the string to be displayed.
settextstyle(int font, int direction, int font_size): A function from graphics.h header file by which we can make the style of the printable text where font argument specifies the font of the text, Direction can be HORIZ_DIR (Left to right) or VERT_DIR(Bottom to top).
Approach:
Here made an animation where 3 disk Tower Of Hanoi Problem is implemented. The full process will complete in 7 phases.
Total nine functions are defined , start(), p1(), p2(), p3(), p4(), p5(), p6(), p7(), outline().
First, call start() function. There print a message 'Beginning State'. Then implement a total of three disks using the rectangle() function. Then color it. The lower bigger one is colored with Red, the middle one is colored with Blue & the smaller top one is colored with Green. All coloring is done by setfillstyle() and floodfill() functions. At last call outline().
In outline() function implement the towers using line() function and print the number of towers also.
Then call p1() function. cleardevice() function will clear the screen. Here, move the smaller Green disk to the third tower. Then call outline() function. Also, print the message '1st Phase'.
Then call p2() function. cleardevice() function will clear the screen. Here, move the smaller Blue disk to the second tower. Then will also call the outline() function. Also, print the message '2nd Phase'.
Then will call the p3() function. cleardevice() function will clear the screen. Here, move the smaller Green disk to the second tower on the top of the Blue disk. Then will also call the outline() function. Also, print the message '3rd Phase'.
Then will call the p4() function. cleardevice() function will clear the screen. Here, move the bigger Red disk to the third tower. Then will also call the outline() function. Also, print the message '4th Phase'.
Then will call the p5() function. cleardevice() function will clear the screen. Here, move the smaller Green disk to the first tower. Then also call outline() function. Also, print the message '5th Phase'.
Then call p6() function. cleardevice() function will clear the screen. Here, move the smaller Blue disk to the third tower on the top of Red disk. Then will also call the outline() function. Also, print the message '6th Phase'.
Then will call the p7() function. cleardevice() function will clear the screen. Here, move the smaller Green disk to the third tower on the top of the Blue disk. Then will also call the outline() function. Also, print the message '7th Phase'. Hence animation is completed.
Below is the implementation of the above approach: