hi guys,
the problemm is that you have a horse on the chess board.You should write a function that calculate the horse's movement.I mean how many movement later you can go every point on the board.I wrote that program and wanted to share with you...
I am Turkish so I named functions in Turkish.So it can make the source misunderstable sorry for that
//---------------------------------------------------------------------------
the problemm is that you have a horse on the chess board.You should write a function that calculate the horse's movement.I mean how many movement later you can go every point on the board.I wrote that program and wanted to share with you...
I am Turkish so I named functions in Turkish.So it can make the source misunderstable sorry for that
//---------------------------------------------------------------------------
Code:
#include<stdio.h> #include<conio.h> int control(); void test(int,int); int secilecek(int , int ); int kucuk(int *); void ekranabas(); int hamle=0; typedef struct nokta { int a; int b; } nokta; nokta olasi[8]; int x,y,gec,q,indis,temp; int matris[8][8]={0}; int main() { x=0;y=0; ekranabas(); printf("\n\n\t\tTo watch horse's movement please press any key\n\t\t The starting point is a1"); getch(); test(x,y); getch(); return 0; } //--------------------------------------------------------------------------- void test(int x,int y) { for(int v=0;v<8;v++) { olasi[v].a=8; olasi[v].b=8; } if(x+1<8&&(x+1>-1)&&y+2<8&&y+2>-1&&(matris[x+1][y+2]==0)) { olasi[0].a=x+1; olasi[0].b=y+2; } if(x-1<8&&x-1>-1&&y+2<8&&y+2>-1&&(matris[x-1][y+2]==0)) { olasi[1].a=x-1; olasi[1].b=y+2; } if(x+1<8&&x+1>-1&&y-2<8&&y-2>-1&&(matris[x+1][y-2]==0)) { olasi[2].a=x+1; olasi[2].b=y-2; } if(x-1<8&&x-1>-1&&y-2<8&&y-2>-1&&(matris[x-1][y-2]==0)) { olasi[3].a=x-1; olasi[3].b=y-2; } if(x+2<8&&x+2>-1&&y-1<8&&y-1>-1&&(matris[x+2][y-1]==0)) { olasi[4].a=x+2; olasi[4].b=y-1; } if(x+2<8&&x+2>-1&&y+1<8&&y+1>-1&&(matris[x+2][y+1]==0)) { olasi[5].a=x+2; olasi[5].b=y+1; } if(x-2<8&&x-2>-1&&y-1<8&&y-1>-1&&(matris[x-2][y-1]==0)) { olasi[6].a=x-2; olasi[6].b=y-1; } if(x-2<8&&x-2>-1&&y+1<8&&y+1>-1&&(matris[x-2][y+1]==0)) { olasi[7].a=x-2; olasi[7].b=y+1; } int tut[8]; for(int t=0;t<8;t++) { if((olasi[t].a!=8)) tut[t]=secilecek(olasi[t].a,olasi[t].b); else tut[t]=9; } matris[x][y]=1; temp=tut[0]; for(int t=0;t<8;t++) if((temp>tut[t]||temp==tut[t])&&matris[olasi[t].a][olasi[t].b]==0) temp=tut[t]; for(q=0;q<8;q++) if(temp==tut[q]) break; x=olasi[q].a; y=olasi[q].b; ekranabas(); hamle++; for(int t=0;t<8;t++) tut[t]=9; if(!control()) return; test(x,y); } int secilecek(int x, int y) { int say=0; if(x+1<8&&(x+1>-1)&&y+2<8&&y+2>-1&&(matris[x+1][y+2]==0)) say++; if(x-1<8&&x-1>-1&&y+2<8&&y+2>-1&&(matris[x-1][y+2]==0)) say++; if(x+1<8&&x+1>-1&&y-2<8&&y-2>-1&&(matris[x+1][y-2]==0)) say++; if(x-1<8&&x-1>-1&&y-2<8&&y-2>-1&&(matris[x-1][y-2]==0)) say++; if(x+2<8&&x+2>-1&&y-1<8&&y-1>-1&&(matris[x+2][y-1]==0)) say++; if(x+2<8&&x+2>-1&&y+1<8&&y+1>-1&&(matris[x+2][y+1]==0)) say++; if(x-2<8&&x-2>-1&&y-1<8&&y-1>-1&&(matris[x-2][y+1]==0)) say++; if(x-2<8&&x-2>-1&&y+1<8&&y+1>-1&&(matris[x-2][y+1]==0)) say++; return say; } int kucuk(int *g) { return y; } void ekranabas() { clrscr(); printf("\t\t\t a b c d e f g h\n"); for(int i=0;i<8;i++) {printf("\t\t\t%d ",i+1); for(int j=0;j<8;j++) if((i+j)%2) { if(matris[i][j]==1) printf("xx "); else printf("\xdb\xdb\xdb"); } else if(matris[i][j]==1) printf("xx "); else printf(" "); printf("\n"); } printf("\n movement that made so far:%d",hamle); for(double g=0;g<9999999;g++); //getch(); return ; } int control() { for (int i=0;i<8;i++) for(int j=0;j<8;j++) if(matris[i][j]==0) return 1; return 0; }
Comment