Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Animation of Tower Of Hanoi using computer graphics in C/C++

  • Last Updated : 09 Aug, 2021

The task is to design the Tower of Hanoi using computer graphics in C/C++.

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:

We provide nothing but the best curated videos and practice problems for our students. Check out the C Foundation Course and master the C language from basic to advanced level. Wait no more, start learning today!
  • 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:

C




// C program for the above approach
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
  
// Function for moving the Green Disk
// to Third Tower On Top Of Blue Disk
void p7()
{
    getch();
    cleardevice();
    settextstyle(8, 0, 4);
    outtextxy(500, 50, "7th Phase");
    setfillstyle(SOLID_FILL, BLUE);
    rectangle(850, 500, 950, 550);
    floodfill(855, 545, 15);
    setfillstyle(SOLID_FILL, GREEN);
    rectangle(875, 450, 925, 500);
    floodfill(880, 495, 15);
    setfillstyle(SOLID_FILL, RED);
    rectangle(825, 600, 975, 550);
    floodfill(830, 555, 15);
  
    // Calling outline() function
    outline();
}
  
// Function to find the moving the Blue
// Disk To Third Tower On Top Of Red Disk
void p6()
{
    getch();
    cleardevice();
    settextstyle(8, 0, 4);
    outtextxy(500, 50, "6th Phase");
    setfillstyle(SOLID_FILL, BLUE);
    rectangle(850, 500, 950, 550);
    floodfill(855, 545, 15);
    setfillstyle(SOLID_FILL, GREEN);
    rectangle(275, 600, 325, 550);
    floodfill(280, 595, 15);
    setfillstyle(SOLID_FILL, RED);
    rectangle(825, 600, 975, 550);
    floodfill(830, 555, 15);
  
    // Calling outline() function
    outline();
}
  
// Function to find the moving Green Disk
// To the First Tower
void p5()
{
    getch();
    cleardevice();
    settextstyle(8, 0, 4);
    outtextxy(500, 50, "5th Phase");
    setfillstyle(SOLID_FILL, BLUE);
    rectangle(550, 550, 650, 600);
    floodfill(555, 595, 15);
    setfillstyle(SOLID_FILL, GREEN);
    rectangle(275, 600, 325, 550);
    floodfill(280, 595, 15);
    setfillstyle(SOLID_FILL, RED);
    rectangle(825, 600, 975, 550);
    floodfill(830, 555, 15);
  
    // Calling outline() function
    outline();
}
  
// Moving Red Disk To Third Tower
void p4()
{
    getch();
    cleardevice();
    settextstyle(8, 0, 4);
    outtextxy(500, 50, "4th Phase");
    setfillstyle(SOLID_FILL, BLUE);
    rectangle(550, 550, 650, 600);
    floodfill(555, 595, 15);
    setfillstyle(SOLID_FILL, GREEN);
    rectangle(575, 500, 625, 550);
    floodfill(580, 545, 15);
    setfillstyle(SOLID_FILL, RED);
    rectangle(825, 600, 975, 550);
    floodfill(830, 555, 15);
  
    // Calling outline() function
    outline();
}
  
// Function for moving the Green Disk
// To Second Tower On Top Of Blue Disk
void p3()
{
    getch();
    cleardevice();
    settextstyle(8, 0, 4);
    outtextxy(500, 50, "3rd Phase");
    setfillstyle(SOLID_FILL, BLUE);
    rectangle(550, 550, 650, 600);
    floodfill(555, 595, 15);
    setfillstyle(SOLID_FILL, GREEN);
    rectangle(575, 500, 625, 550);
    floodfill(580, 545, 15);
    setfillstyle(SOLID_FILL, RED);
    rectangle(225, 550, 375, 600);
    floodfill(230, 590, 15);
  
    // Calling outline() function
    outline();
}
  
// Function for moving the Blue Disk
// To Second Tower
void p2()
{
    getch();
    cleardevice();
    settextstyle(8, 0, 4);
    outtextxy(500, 50, "2nd Phase");
    setfillstyle(SOLID_FILL, BLUE);
    rectangle(550, 550, 650, 600);
    floodfill(555, 595, 15);
    setfillstyle(SOLID_FILL, GREEN);
    rectangle(875, 600, 925, 550);
    floodfill(880, 595, 15);
    setfillstyle(SOLID_FILL, RED);
    rectangle(225, 550, 375, 600);
    floodfill(230, 590, 15);
  
    // Calling outline() function
    outline();
}
  
// Function for moving the Green Disk
// To Third Tower
void p1()
{
    getch();
    cleardevice();
    settextstyle(8, 0, 4);
    outtextxy(500, 50, "1st Phase");
    setfillstyle(SOLID_FILL, GREEN);
    rectangle(875, 600, 925, 550);
    floodfill(880, 595, 15);
    setfillstyle(SOLID_FILL, RED);
    rectangle(225, 550, 375, 600);
    floodfill(230, 590, 15);
    setfillstyle(SOLID_FILL, BLUE);
    rectangle(250, 500, 350, 550);
    floodfill(255, 545, 15);
  
    // Calling outline() function
    outline();
}
  
// Function to start the animations
void start()
{
    // Starting Condition
    cleardevice();
    settextstyle(8, 0, 4);
    outtextxy(500, 50, "Begining State");
  
    // Red Coloring Of Disk
    setfillstyle(SOLID_FILL, RED);
    rectangle(225, 550, 375, 600);
    floodfill(230, 590, 15);
  
    // Blue Coloring Of Disk
    setfillstyle(SOLID_FILL, BLUE);
    rectangle(250, 500, 350, 550);
    floodfill(255, 545, 15);
  
    // Green Coloring Of Disk
    setfillstyle(SOLID_FILL, GREEN);
    rectangle(275, 450, 325, 500);
    floodfill(285, 495, 15);
  
    // calling outline() function
    outline();
}
  
// Function to print the outlines of
// the animations
void outline()
{
    // Main Base
    line(100, 600, 1100, 600);
  
    // 1st Line
    line(300, 600, 300, 300);
  
    // 2nd Line
    line(600, 600, 600, 300);
  
    // 3rd Line
    line(900, 600, 900, 300);
  
    // Printing Message
    settextstyle(8, 0, 2);
    outtextxy(290, 620, "(1)");
    outtextxy(590, 620, "(2)");
    outtextxy(890, 620, "(3)");
}
// Driver Code
void main()
{
    int gd = DETECT, gm;
  
    // Initialize of gdriver with
    // DETECT macros
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
  
    // Calling start() function
    start();
  
    // Calling p1() function
    p1();
  
    // Calling p2() function
    p2();
  
    // Calling p3() function
    p3();
  
    // Calling p4() function
    p4();
  
    // Calling p5() function
    p5();
  
    // Calling p6() function
    p6();
  
    // Calling p7() function
    p7();
  
    // Holding screen for a while
    getch();
  
    // Close the initialized gdriver
    closegraph();
}

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :