This program is written in borland C and has simple graphic and it is tour of knight on 64 square on chess board without repeat position

[sourcecode language=’cpp’]
/*******************************************************
* MYCPLUS Sample Code – http://www.mycplus.com *
* *
* This code is made available as a service to our *
* visitors and is provided strictly for the *
* purpose of illustration. *
* *
* Please direct all inquiries to saqib at mycplus.com *
*******************************************************/

/*****************By : Farshad Mojtabai*************/
#include
#include
#include

#include
#include
#include
#include
#include

void LINE(int x1,int y1,int x2,int y2,int color);
void RECT(int x1,unsigned int y1,int x2,unsigned int y2,int color);
void Board();
void Knight(int x1,int y1);
int XYGraphic(int p);
void Tour();
void possible();
void exits(int l);
const int ver[]={-1,-2,-2,-1,1,2,2,1},
hor[]={2,1,-1,-2,-2,-1,1,2};

int row,col,npos;
int board[8][8],nextij[8][8][8],accessible[8][8];
void main()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int x, y, color;

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, “..\\bgi”);

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

Tour();

}
void RECT(int x1,unsigned int y1,int x2,unsigned int y2,int color)
{
for(int i=y1;i<=y2;i++) LINE(x1,i,x2,i,color); } void LINE(int x1,int y1,int x2,int y2,int color) { #define sign(x) ((x) > 0 ? 1:((x) == 0? 0:(-1)))
int dx,dy,dxabs,dyabs,i,px,py,sdx,sdy,x,y;

dx = x2 – x1 ;
dy = y2 – y1;

sdx = sign(dx);
sdy = sign(dy);

dxabs = abs(dx);
dyabs = abs(dy);

x = 0; y = 0;
px = x1;
py = y1;

if(dxabs >= dyabs)
{
for(i=0;i <=dxabs;i++) { y += dyabs; if(y >= dxabs)
{
y -= dxabs;
py += sdy;
}

putpixel(px,py,color);
px +=sdx;
}
}
else
{
for(i=0; i<=dyabs;i++) { x += dxabs; if(x >= dyabs)
{
x -= dyabs;
px += sdx;
}
putpixel(px,py,color);
py += sdy;
}
}
}
int XYGraphic(int p)
{
switch(p)
{
case 0: return 65;
case 1: return 115;
case 2: return 165;
case 3: return 215;
case 4: return 265;
case 5: return 315;
case 6: return 365;
case 7: return 415;
}
return 0 ;
}
void Knight(int x1,int y1)
{

setcolor(6);
for(int i=0;i<6;i++) circle(x1,y1,i); } void Board() { int x, y, color; outtextxy(65,20,"a"); outtextxy(115,20,"b"); outtextxy(165,20,"c"); outtextxy(215,20,"d"); outtextxy(265,20,"e"); outtextxy(315,20,"f"); outtextxy(365,20,"g"); outtextxy(415,20,"h"); outtextxy(20,65,"1"); outtextxy(20,115,"2"); outtextxy(20,165,"3"); outtextxy(20,215,"4"); outtextxy(20,265,"5"); outtextxy(20,315,"6"); outtextxy(20,365,"7"); outtextxy(20,415,"8"); for(int j=0;j<8;j++) for(int i=0; i<8;i++) { if((j%2 == 0)&&(i%2 ==0)) RECT(50*i+40,50*j+40,50*(i+1)+40,50*(j+1)+40,15); else if((j%2 != 0)&&(i%2 != 0)) RECT(50*i+40,50*j+40,50*(i+1)+40,50*(j+1)+40,15); else RECT(50*i+40,50*j+40,50*(i+1)+40,50*(j+1)+40,0); } } void Tour() { int count = 1,k,j; cout <<"position [from (0,0) to (7,7)]:"; cin >>row >>col;
cout << endl; board[row][col]=count; Board(); Knight(XYGraphic(row),XYGraphic(col)); delay(3000); while(count!=64) { //Board(); count++; possible(); exits(count); delay(3000); // Board(); if(kbhit()) exit(0); } /* for(int i=0;i<8;i++) for(j=0;j<8;j++) { Board(); Knight(XYGraphic(i),XYGraphic(j)); delay(3000); }*/ } void possible() { int npos; for(int r=0;r<=7;r++) { for(int c=0;c<=7;c++) { npos = 0; for(int i=0;i<=7;i++) { if(((r+ver[i] >=0) && (r

+ver[i] <=7))&&((c+hor[i] >=0) && (c+hor[i] <=7))&&(board[r+ver [i]][c+hor[i]] == 0)) { nextij[r][c][ npos] = i; npos++; } } accessible[r][c] = npos; } } } void exits(int l) { int min = accessible[row+ver[nextij[row][col][0]]][col+ hor[nextij[row][col][0]]]; int r = row+ver[nextij[row][col][0]],c=col+hor[nextij[row] [col][0]]; for(int i=1;i < accessible[row][col];i++) if(min >= accessible[row+ver[nextij[row][col][i

]]][col+hor[nextij[row][col][i]]])
{
min =accessible[row+ver[nextij[

row][col][i]]][col+hor[nextij[row][col][i]]];
r = row + ver[nextij[row][col][i]];
c = col + hor[nextij[row][col][i]];
}

//cout <<"\n min ="<<<" (" <<<','<<<") \n"; board[r][c]=l; Knight(XYGraphic(r),XYGraphic(c)); row = r; col = c; }[/sourcecode]

0 Shares
Share
+1
Tweet
Share
Stumble