Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Cant undestand dis code

  1. Jun 4, 2006 #1
    #include <stdio.h>
    #include <conio.h>
    #include <graphics.h>


    int colors[7],n;
    int sizesx1[7],sizesx2[7],sizeax1[7],sizeax2[7],sizedx1[7],sizedx2[7];
    int posy1[7],posy2[7];
    int disks[4];
    int diskpos[7];
    int cx1,cx2,cy1,cy2;
    void Initialize(int nd)
    {
    int i;
    for(i=nd;i>=1;i--)
    {
    sizesx1=30+((nd-i)*10);
    sizesx2=180-((nd-i)*10);
    sizeax1=240+((nd-i)*10);
    sizeax2=390-((nd-i)*10);
    sizedx1=450+((nd-i)*10);
    sizedx2=600-((nd-i)*10);
    posy1=339-((nd-i)*10)-(nd-i);
    posy2=349-((nd-i)*10)-(nd-i);

    diskpos=i;
    }

    disks[1]=nd;
    disks[2]=disks[3]=0;

    colors[6]=LIGHTRED;colors[5]=GREEN;
    colors[4]=LIGHTBLUE;colors[3]=DARKGRAY;
    colors[2]=MAGENTA;colors[1]=YELLOW;

    }

    void DrawTowers(int nd)
    {
    int i;

    rectangle(20,350,190,365);
    rectangle(230,350,400,365);
    rectangle(440,350,610,365);
    setfillstyle(SOLID_FILL, getmaxcolor());
    floodfill(22,352, getmaxcolor());
    floodfill(232,352, getmaxcolor());
    floodfill(442,352, getmaxcolor());

    rectangle(98,200,113,posy1[1]);
    rectangle(308,200,323,350);
    rectangle(518,200,533,350);
    floodfill(100,205, getmaxcolor());
    floodfill(310,205, getmaxcolor());
    floodfill(520,205, getmaxcolor());

    for(i=1;i<=nd;i++)
    {
    setfillstyle(1,colors);
    rectangle(sizesx1,posy1,sizesx2,posy2);
    floodfill(sizesx1+1,posy1+1, getmaxcolor());
    }
    setcolor(RED);
    outtextxy(82,355,"SOURCE");
    outtextxy(285,355,"AUXILIARY");
    outtextxy(485,355,"DESTINATION");
    }

    void MoveUp(int nd,int s)
    {
    int i;
    int x1,x2,y1,y2;
    int px1,px2;
    if(s==1)
    {
    x1=sizesx1[nd];
    x2=sizesx2[nd];
    px1=98;px2=113;
    }
    if(s==2)
    {
    x1=sizeax1[nd];
    x2=sizeax2[nd];
    px1=308;px2=323;
    }
    if(s==3)
    {
    x1=sizedx1[nd];
    x2=sizedx2[nd];
    px1=518;px2=533;
    }
    y1=posy1[diskpos[nd]];
    y2=posy2[diskpos[nd]];
    for(i=1;i<=(posy1[diskpos[nd]]-100);i++)
    {
    delay(5);
    setcolor(colors[nd]);
    rectangle(x1,y1-(i-1),x2,y2-(i-1));
    setcolor(WHITE);
    rectangle(x1,y1-i,x2,y2-i);
    setcolor(BLACK);
    line(x1,y2-(i-1),x2,y2-(i-1));
    setcolor(WHITE);
    if((posy1[diskpos[nd]]-i)>=190)
    line(px1,y2-(i-1),px2,y2-(i-1));
    }
    cx1=x1;cx2=x2;
    cy1=y1-(i-1);
    cy2=y2-(i-1);
    }

    void MoveDown(int nd,int d,int pos)
    {
    int i;
    int x1,x2,y1,y2;
    int ey1;
    int px1,px2;
    x1=cx1;x2=cx2;
    y1=cy1;y2=cy2;
    ey1=posy1[pos];
    if(d==1)
    {
    px1=98;
    px2=113;
    }
    if(d==2)
    {
    px1=308;
    px2=323;
    }
    if(d==3)
    {
    px1=518;
    px2=533;
    }
    for(i=1;(cy1+i)<=ey1;i++)
    {
    delay(5);
    setcolor(colors[nd]);
    rectangle(x1,y1+i-1,x2,y2+i-1);
    setcolor(WHITE);
    rectangle(x1,y1+i,x2,y2+i);
    setcolor(BLACK);
    line(x1,y1+(i-1),x2,y1+(i-1));
    setcolor(WHITE);
    if((cy1+i)>=200)
    line(px1,y1+(i-1),px2,y1+(i-1));
    }
    }

    void MoveForward(int nd,int d)
    {
    int i;
    int x1,x2,y1,y2;
    int ex1;
    x1=cx1;x2=cx2;
    y1=cy1;y2=cy2;
    if(d==2)
    ex1=sizeax1[nd];
    if(d==3)
    ex1=sizedx1[nd];

    for(i=1;i<=(ex1-cx1);i++)
    {
    delay(5);
    setcolor(colors[nd]);
    rectangle(x1+i-1,y1,x2+i-1,y2);
    setcolor(WHITE);
    rectangle(x1+i,y1,x2+i,y2);
    setcolor(BLACK);
    line(x1+(i-1),y1,x1+(i-1),y2);
    }
    cx1=x1+(i-1);
    cx2=x2+(i-1);
    cy1=y1;cy2=y2;
    }

    void MoveBackward(int nd,int d)
    {
    int i;
    int x1,x2,y1,y2;
    int ex1;
    x1=cx1;x2=cx2;
    y1=cy1;y2=cy2;
    if(d==1)
    ex1=sizesx1[nd];
    if(d==2)
    ex1=sizeax1[nd];

    for(i=1;(cx1-i)>=ex1;i++)
    {
    delay(5);
    setcolor(colors[nd]);
    rectangle(x1-i+1,y1,x2-i+1,y2);
    setcolor(WHITE);
    rectangle(x1-i,y1,x2-i,y2);
    setcolor(BLACK);
    line(x2-(i-1),y1,x2-(i-1),y2);
    }
    cx1=x1-(i-1);
    cx2=x2-(i-1);
    cy1=y1;cy2=y2;

    }

    void MoveDisk(int nd,int s,int d)
    {
    MoveUp(nd,s);
    if((d-s)>0)
    MoveForward(nd,d);
    else
    MoveBackward(nd,d);
    MoveDown(nd,d,(n-disks[d]));
    diskpos[nd]=n-disks[d];
    disks--;disks[d]++;
    }

    void Display(int nd,int s,int d)
    {
    int i;
    char peg[4][25]={"","SOURCE","AUXILIARY","DESTINATION"};
    char msg[55]="";
    char dn[2];
    dn[0]=nd+48;
    dn[1]='\0';
    strcpy(msg,"Disk ");
    strcat(msg,dn);
    strcat(msg," is moving from ");
    strcat(msg,peg);
    strcat(msg," to ");
    strcat(msg,peg[d]);

    setcolor(BLACK);
    for(i=1;i<=50;i++)
    {
    line(29,399+i,600,399+i);
    }
    setcolor(LIGHTRED);
    settextstyle(7,HORIZ_DIR,1);
    outtextxy(30,400,msg);

    setcolor(colors[nd]);
    settextstyle(10,HORIZ_DIR,2);
    outtextxy(180,20,"TOWER OF HANOI");
    outtextxy(175,50,"--------------");
    }

    void TowersOfHanoi(int nd,int s,int d,int a)
    {
    if(nd>0)
    {
    TowersOfHanoi(nd-1,s,a,d);
    Display(nd,s,d);
    MoveDisk(nd,s,d);
    TowersOfHanoi(nd-1,a,d,s);
    }
    }

    int main(void)
    {
    int nd,i;
    /* request auto detection */
    int gdriver = DETECT, gmode, errorcode;
    char msg[80];

    /* initialize graphics and local variables */
    initgraph(&gdriver, &gmode, "d:\\tc\\bgi");

    /* read result of initialization */
    errorcode = graphresult();
    if (errorcode != grOk) /* an error occurred */
    {
    printf("Graphics error: %s\n", grapherrormsg(errorcode));
    printf("Press any key to halt:");
    getch();
    exit(1); /* terminate with an error code */
    }

    settextstyle(10,HORIZ_DIR,2);
    outtextxy(180,20,"TOWER OF HANOI");
    outtextxy(175,50,"--------------");

    read:printf("\n\n\n\n\n\n\nEnter the number of Disk (Minimum 1 & Maximum 6):");
    scanf("%d",&nd);
    if(nd<1 || nd>6)
    {
    printf("Number of Disks must be with in range\n");
    getch();
    goto read;
    }
    n=nd;
    cleardevice();
    settextstyle(10,HORIZ_DIR,2);
    outtextxy(180,20,"TOWER OF HANOI");
    outtextxy(175,50,"--------------");
    settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
    Initialize(nd);
    DrawTowers(nd);
    TowersOfHanoi(nd,1,3,2);


    setcolor(BLACK);
    for(i=1;i<=50;i++)
    {
    line(29,399+i,600,399+i);
    }
    getch();
    return 0;
    }i cant undestand dis code for tower ov hanoi..need urgent help...today!!
     
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you help with the solution or looking for help too?
Draft saved Draft deleted



Similar Discussions: Cant undestand dis code
  1. Error in code (Replies: 4)

  2. Examples of code (Replies: 2)

  3. Code Blocks (Replies: 2)

  4. The Source Code! (Replies: 15)

Loading...