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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | /*****************By : Farshad Mojtabai*************/ #include <stdio.h> #include <conio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <graphics.h> #include <dos.h> 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; } |