Tower of Hanoi

Source: Internet
Author: User


Soon after learning C, I made up a tower. It is troublesome. It is only for correction.

Tubro C for Win.

/* You want to use the Linked List table but cannot do it :')*/
# Include <conio. h>
# Include <graphics. h>
# Include <bios. h>

# Define N 7/* Number of squares, which can be customized and cannot exceed 8 */
# Define X (X-100)/200

Struct
{
Int y [N];/* y coordinate of each point on the pole */
Int fa;/* number of blocks on the token bar */
Int fb [N];/* width of the square at the current position on the logo bar */
} Stick [3];

Void drawintro (int x, int color)/* draw arrows */
{
Setcolor (color );
Line (x, 20, x, 40 );
Line (X-3, 40, x +); line (x +, x, 49); line (x, 49, X-3, 40 );
Setfillstyle (SOLID_FILL, color );
Floodfill (x, 45, color );
}
Void drawblock (int x, int y, int w, int color)/* draw a square */
{
Setcolor (color );
Rectangle (X-10 * w, Y-9, x + 10 * w, y + 9 );
Setfillstyle (SOLID_FILL, color );
Floodfill (x-5, y, color );
Floodfill (x + 5, y, color);/* This filling method is too disgusting */
}
Void _ line (int x, int y, int color)/* draw the line after square elimination */
{
Setcolor (color );
Line (x, Y-9, x, y + 9 );
}


Main ()
{
Int gd = DETECT, gm, I, x, key = 0, f = 0;
Initgraph (& gd, & gm ,"");
Setbkcolor (2 );
Setcolor (1 );
Line (100,241, 70 );
Line (300,400, 70 );
Line (500,400, 70 );
Settextstyle (0, 0, 4 );
Outtextxy (10,410, "Hanoi ");
Drawintro (x = 100,4 );
Stick [0]. fa = N;
Stick [1]. fa = stick [2]. fa = 0;
For (I = 0; I <N; ++ I)
{
Stick [0]. fb [I] = I + 1;
Stick [1]. fb [I] = stick [2]. fb [I] = 0;
Stick [0]. y [I] = stick [1]. y [I] = stick [2]. y [I] = 250 + 19 * I;
Drawblock (x, stick [0]. y [I], stick [0]. fb [I], 1 );
}
Setcolor (0 );
Line (x, 241, x, 259 + 19*(I-1 ));
Setcolor (1 );
Line (x, 260 + 19 * (I-1), x, 400 );
While (key! = 0x11b)
{
Key = (bioskey (0 ));
If (key = 0x4b00 & x! = 100)
{
Drawintro (x, 0 );
Drawintro (x-200,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.