Knight's tour game : Using C/C++ and graphics

Hey guys, this is the classic game many of you chess lovers may know this. It's very simple game, you have to move knight piece (horse) starting at any place but move such that it has to cover all 64 boxes in 64 moves without repeatation and valid move is only "L" shape move. This is the move of knight, unlike any other chess pieces, knight moves in strange way making "L" shape.

In this program, just click on the box you want to keep your knight piece such that it should meet above conditions. Here's the screen shot of the output.

Here's the code, enjoy coding...

Note : If the program is generating error in c please save the file with .cpp extension


/*
    Programmer : Ashok Kumar Shrestha (ak007)
    Program details : KNIGHT'S TOUR USING BGI GRAPHICS IN C/C++
*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdio.h>
#include<process.h>
#include<math.h>

void knight(int i,int j,int k);

union REGS i,o;
struct SREGS s;

int cursor[32]={/*
        0x0000,0x0000,0x0000,0x0000,
        0x8001,0xc003,0xf00f,0xfc3f,
        0xfc3f,0xf00f,0xc003,0x8001,
        0x0000,0x0000,0x0000,0x0000,
        0xffff,0x8001,0xffff,0x8001,
        0x4002,0x2004,0x1008,0x0240,
        0x0240,0x0810,0x2004,0x4002,
        0x8001,0xffff,0x8001,0xffff};
        */
        0xe1ff,0xe1ff,0xe1ff,0xe1ff,
        0xe1ff,0x0000,0x0000,0x0000,
        0x0000,0x0000,0x0000,0x0000,
        0x0000,0x0000,0x0000,0x0000,
        0x1e00,0x1200,0x1200,0x1200,
        0x13ff,0x1249,0x1249,0xf249,
        0x9001,0x9001,0x9001,0x8001,
        0x8001,0x8001,0xffff,0x0000,
        };

initmouse()
{
    i.x.ax=0;
    int86 (0x33,&i,&o);
    return(o.x.ax);
}

showmouseptr()
{
    i.x.ax=1;
    int86 (0x33,&i,&o);
    return 0;
}

changecursor(int *shape)
{
    i.x.ax=9;
    i.x.bx=0;
    i.x.cx=0;
    i.x.dx=(unsigned)shape;
    segread(&s);
    s.es=s.ds;
    int86x (0x33,&i,&i,&s);
    return 0;
}

void hidemouseptr()
{
    i.x.ax=2;
    int86(0x33,&i,&o);
}

 getmousepos(int *button,int *x,int *y)
{
    i.x.ax=3;
    int86(0x33,&i,&o);
    *button=o.x.bx;
    *x=o.x.cx;
    *y=o.x.dx;
    return 0;
}

restrictmouseptr(int x1,int y1,int x2,int y2)
{
    i.x.ax=7;
    i.x.cx=x1;
    i.x.dx=x2;
    int86(0x33,&i,&o);
    i.x.ax=8;
    i.x.cx=y1;
    i.x.dx=y2;
    int86(0x33,&i,&o);
    return 0;
}
int count_a[8][8];

int frst()
{
    int i,j;
    for(i=0;i<8;i++)
    {
        for(j=0;j<8;j++)
        {
            if(count_a[i][j]!=0)
                return 0;
        }
    }
    return 1;
}

int check(int pi,int pj,int i,int j)
{
    if((i==pi+1)&&(j==pj+2))
        return  1;
    else if((i==pi+2)&&(j==pj+1))
        return  1;
    else if((i==pi+2)&&(j==pj-1))
        return  1;
    else if((i==pi+1)&&(j==pj-2))
        return  1;
    else if((i==pi-1)&&(j==pj-2))
        return  1;
    else if((i==pi-2)&&(j==pj-1))
        return  1;
    else if((i==pi-2)&&(j==pj+1))
        return  1;
    else if((i==pi-1)&&(j==pj+2))
        return  1;
    return 0;
}

void knight(int x,int y)
{
    setcolor(0);
    setfillstyle(1,6);
    line(13+x,50+y,37+x,50+y);
    line(13+x,50+y,22+x,34+y);
    line(37+x,50+y,32+x,26+y);
    line(22+x,35+y,8+x,31+y);
    line(32+x,26+y,36+x,23+y);
    line(36+x,23+y,31+x,20+y);
    line(31+x,20+y,28+x,15+y);
    line(28+x,15+y,25+x,20+y);
    line(25+x,20+y,22+x,15+y);
    line(22+x,15+y,19+x,20+y);
    line(19+x,20+y,8+x,27+y);
    line(8+x,27+y,15+x,29+y);
    line(15+x,29+y,8+x,31+y);
    circle(25+x,25+y,1);
    floodfill(x+20,y+20,0);
    setcolor(7);
    line(13+x,50+y,22+x,34+y);
    line(28+x,15+y,25+x,20+y);
    line(22+x,15+y,19+x,20+y);
    line(19+x,20+y,8+x,27+y);
    line(15+x,29+y,8+x,31+y);
    setcolor(0);
    line(13+x,50+y,37+x,50+y);
}


int p[2];
int count(int c1,int c2)
{
    int i,j,k=0,x,y,cl;
    i=c1/79;    j=c2/59;
    if(frst())
    {

        count_a[i][j]=1;
        p[0]=i;    p[1]=j;
        return 1;
    }
    k=check(p[0],p[1],i,j);
    if((count_a[i][j]!=1)&&(k!=0))
    {
        if((p[1]%2==0&&p[0]%2==0)||(p[0]+p[1])%2==0)   //delete green box
                cl=15;
            else
                cl=1;
        setfillstyle(1,cl);
        bar(p[0]*79,p[1]*59,(p[0]+1)*79,(p[1]+1)*59);
        knight(p[0]*79+15,p[1]*59);

        count_a[i][j]=1;
        p[0]=i;    p[1]=j;
        return 1;
    }
    return 0;
}

int win_chek()
{
    int i,j;
    for(i=0;i<8;i++)
        for(j=0;j<8;j++)
        {
            if(count_a[i][j]==0)
                return 0;
        }
    return 1;
}

int win()
{
    if(win_chek())
    {
        cleardevice();
        hidemouseptr();
        settextstyle(4,0,5);
        setcolor(2);
        outtextxy(200,150,"CONGRATS!");
        outtextxy(220,250,"YOU WON");
        getch();
        return 1;
        //exit(0);
    }
    return 0;
}

void rank()
{
    char s[25];
    hidemouseptr();
    printf("\n\n\tNAME : ");
    scanf("%s",s);
    printf("\n\n\t%s completed knight's tour successfully.",s);
}

void box(int c1,int c2,int c=2)
{
    int d=4;
    if(count(c1,c2))
    {
        int c3,c4;
        c3=c1+79;    c4=c2+59;
        setcolor(0);
        setfillstyle(1,c);
            rectangle(c1,c2,c3,c4);
            rectangle(c1+d,c2+d,c3-d,c4-d);
        floodfill(c1+1,c2+1,0);
        knight(c1+15,c2);
        //win();
        if(win())
        {    rank();}
    }
    else
    {
        setcolor(0);
        setfillstyle(1,4);
            rectangle(c1,c2,c1+79,c2+59);
            rectangle(c1+d,c2+d,c1+79-d,c2+59-d);
        floodfill(c1+1,c2+1,0);

        setcolor(4);
        settextstyle(4,0,6);
        outtextxy(200,200,"Invalid Move");
        getch();
        exit(0);
    }
}

ma()
{
    int p1,p2,c1,c2,c3,c4;
    //changecursor(cursor);
    showmouseptr();
    int button,x,y,mx=getmaxx()/8,my=getmaxy()/8;
    button=0;
    getmousepos(&button,&x,&y);
    p1=x/mx;    p2=y/my;
    c1=p1*mx;    c2=p2*my;
    if(button&1==1)
    {       delay(150);
        hidemouseptr();
        box(c1,c2,2);
        showmouseptr();
        delay(150);
        return 1;

    }
    else if(button&2==2)
        return 1;
    return 0;
}

void loading()
{
    int d=3,i,x1=120,y1=350,x2=x1+400,y2=y1+15;
    char arr[10];
    cleardevice();
    moveto(260,50);
    setcolor(7);
    settextstyle(4,0,10);
    outtext("A");
    settextstyle(1,0,1);
    moveto(250,200);
    outtext("PRODUCTION");
    delay(600);

    setcolor(1);
    setfillstyle(1,3);
    rectangle(x1,y1,x2,y2);
    rectangle(x1-d,y1-d,x2+d,y2+d);
    floodfill(x1-1,y1-1,1);
    setcolor(7);
    settextstyle(8,0,1);
    outtextxy(x1,y1-30,"Loading...");
    setfillstyle(1,2);
    for(i=0;i<100;i++)
    {
        delay(100);
        bar(x1+1,y1+1,x1+i*4+3,y2-1);
        gotoxy(x2/8-2,y1/17+1);
        printf("%d %",i+1);
    }
    delay(700);
}

void main_page()
{
    loading();
    cleardevice();
    setcolor(2);
    settextstyle(10,0,3);
    outtextxy(50,50,"WELCOME TO KNIGHT'S TOUR");
    setcolor(3);
    settextstyle(9,0,1);
    outtextxy(260,150,"R U L E :");
    outtextxy(240,260,"HOW to play :");
    setcolor(5);
    settextstyle(6,0,2);
    outtextxy(100,200,"1.Move your knight in 64 box without repeatation.");

    outtextxy(100,300,"1.Position mouse pointer at appropriate box.");
    outtextxy(100,350,"2.Click left button to place knight.");

    outtextxy(220,450,"ENJOY The GAME . . . .");
    getch();
}

void main()
{
    int gd=DETECT,gm,c,cl=0,button,mx,my;
    int x,y,i,j,k,poly[8],l,m;
    initgraph(&gd,&gm,"..\\bgi");
    mx=getmaxx()/8;
    my=getmaxy()/8;
    main_page();
         for(i=0;i<8;i++)
         {
        for(j=0;j<8;j++)
        {
            if((j%2==0&&i%2==0)||(i+j)%2==0)
                cl=15;
            else
                cl=1;
            setcolor(cl);
            setfillstyle(1,cl);
            rectangle(i*mx,j*my,(i+1)*mx,(j+1)*my);
            floodfill(i*mx+25,j*my+25,cl);
        }
          }
          changecursor(cursor);
          restrictmouseptr(1,1,632-1,472-1);
          rectangle(0,0,(8*mx)+1,(8*my)+1);
          while(!kbhit())
          {
            ma();
          }

    getch();
    closegraph();
}

// End of the program










Comments

Popular posts from this blog

mTranslator

Circular Pattern Using C/C++ and Graphics

Sorting Hat: Privacy Policy