/* ========================================================= * Course: * C Programming * Exercise 2 * * Date: 25-nov-2009 * * ========================================================= * 1. 8 queens assignment * 2. 8 queens solution print * 3. Check query * ========================================================= */ #include int main() { int ch[8][8],p,j=0,i=0,tfalse=50,b,z=1,n,s,d,abc=0; //initialize variables for the first program int i2,j2,temp2=0,ch2[8]={0},N2=1,flag=0,flag2=0,temp3=0,b2; //initialize variables for the 2nd program int ch3[8][8],j3=0,i3=0,tool3,b3=1,kj3,ki3,te3=0,check3=0; //initialize variables for the third program for(i=0;i<8;i++){ for(j=0;j<8;j++){ ch[i][j]=0; ch3[i][j]=0;}} while(abc!=4) { printf("1. 8 queens assignment\n2. 8 queens solution print\n3. Check query\n4. Exit program\nEnter your choice (1-4): "); scanf("%d" ,&abc); while ((abc>4)||(abc<1)){ printf("Enter your choice <1-4>: "); scanf("%d" ,&abc); } printf("\n"); switch (abc){ case 1: /************here strat the first program****************/ for (p=0;p<8;p++){ printf ("Enter location of queen no. %d : ",p); scanf ("%d,%d",&i,&j); if ((i>7)||(i<0)||(j>7)||(j<0)){ printf("Out of boundaries!\n"); p=p-1; } if (ch[i][j]==3){ printf ("Already taken!\n"); p=p-1; } if ((ch[i][j]==0)&&((i<8)&&(i>=0)&&(j<8)&&(j>=0))){ ch[i][j]=3; } } for(i=0;i<8;i++){ for(j=0;j<7;j++){ for (b=1;z<8;b++){ if ((ch[i][j]==3)&&(ch[i][b+j]==3)) { tfalse=0; } z++; } } z=1; } for(i=0;i<8;i++){ for(j=0;j<7;j++){ for (b=1;z<8;b++){ if (ch[i][j]==3&&ch[i+b][j]==3){ tfalse=0; } z++; } } z=1; } for(i=0;i<8;i++){ for(j=0;j<7;j++){ for (b=1,n=1,s=1,d=1;z<8;b++,n++,s++,d++){ if ((b+j)>7) break; if ((n+i)>7) break; if ((ch[i][j]==3&&ch[i+n][b+j]==3)){ tfalse=0; } z++; } z=1; } } for(i=0;i<8;i++){ for(j=0;j<8;j++){ for (s=1,d=1;z<8;b++,n++,s++,d++){ if ((i+s)>7) break; if ((j-d)<0) break; if ((ch[i][j]==3&&ch[i+s][j-d]==3)){ tfalse=0; } z++; } z=1; } } if (!tfalse) {printf("Impossible assignment!\n\n");} if (tfalse) {printf("Possible assignment!\n\n");} for(i=0;i<8;i++){ //here the program ends and now initialize the variables for(j=0;j<8;j++){ ch[i][j]=0; } } j=0;i=0;tfalse=50;z=1; break; case 2: /************here strat the 2nd program****************/ printf("Enter solution number: "); scanf("%d",&N2); while ((N2>92)||(N2<1)){ printf("Enter solution number: "); scanf("%d",&N2); } while (temp3!=N2){ for (i2=0;i2<8;i2++){ for (j2=0;j27){ ch2[i2-1]=0; ch2[i2-2]++; flag2=1; } j2=-1; flag2=1; } } if (ch2[j2]==ch2[i2]){ //check the Column if(ch2[i2]+1<8){ ch2[i2]++; for(b2=1;b2<8;b2++){ if(i2+b2<8) ch2[i2+b2]=0; } flag=1; } else { ch2[i2]=0; for(b2=1;b2<8;b2++){ if(i2+b2<8) ch2[i2+b2]=0; } ch2[i2-1]++; flag=1; if (ch2[i2-1]>7){ ch2[i2-1]=0; ch2[i2-2]++; flag2=1; } j2=-1; flag2=1; } } if ((ch2[i2]==ch2[j2]+i2-j2)&&(j2+1<8)){ //check the upper Diagonal if(ch2[i2]+1<8){ ch2[i2]++; for(b2=1;b2<8;b2++){ if(i2+b2<8) ch2[i2+b2]=0; } flag=1; } else { ch2[i2]=0; for(b2=1;b2<8;b2++){ if(i2+b2<8) ch2[i2+b2]=0; } ch2[i2-1]++; flag=1; if (ch2[i2-1]>7){ ch2[i2-1]=0; ch2[i2-2]++; flag2=1; } j2=-1; } } if (flag){ j2=-1; i2=0; flag=0; } } if (ch2[7]!=0) temp2=1; if (flag2){ i2=-1; flag2=0; } } temp3++; if (temp3==N2){ for(i2=0;i2<8;i2++){ printf("%d,%d ",i2,ch2[i2]); } printf("\n\n"); } if(ch2[7]==7){ ch2[7]=0; ch2[6]++; } else ch2[7]++; } for (b2=0;b2<8;b2++){ //here the program ends and now initialize the variables ch2[b2]=0; } temp2=0;N2=1;flag=0;flag2=0;temp3=0; break; case 3: /************here strat the third program****************/ do{ printf ("Enter row,column,tool: "); scanf ("%d,%d,%d",&i3,&j3,&tool3); if ((ch3[i3][j3]!=0)&&((i3<8)&&(i3>=0))&&((j3<8)&&(j3>=0))&&(tool3>0)&&(tool3<6)){ printf ("Already taken!\n"); } if (tool3==1){ki3=i3; kj3=j3;} //remmber the king posion if ((ch3[i3][j3]==0)&&(i3<8&&(i3>=0)&&((j3<8)&&(j3>=0))&&((tool3<6)&&(tool3>0)))) ch3[i3][j3]=tool3; //רק אם המקום לא תפוס תוסיף למספר } while ((i3<8&&(i3>=0)&&((j3<8)&&(j3>=0))&&((tool3<6)&&(tool3>0)))); for(i3=0;i3<8;i3++){ for(j3=0;j3<8;j3++){ if (ch3[i3][j3]==3){ //algoritem for the "charich" for(b3=1;(j3+b3)=0)&&(ch3[i3][j3-b3]!=1));b3++){ if((ch3[i3][j3-b3]==0)){te3=te3+1;} } if ((j3-kj3-1==te3)&&(ki3==i3)){ check3=1; } te3=0; for(b3=1;(i3+b3)=0)&&(ch3[i3-b3][j3]!=1));b3++){ if((ch3[i3-b3][j3]==0)){te3=te3+1;} } if ((i3-ki3-1==te3)&&(kj3==j3)){ check3=1; } } if (ch3[i3][j3]==2){ //algoritem for the queen for(b3=1;(j3+b3)=0)&&(ch3[i3][j3-b3]!=1));b3++){ if((ch3[i3][j3-b3]==0)){te3=te3+1;} } if ((j3-kj3-1==te3)&&(ki3==i3)){ check3=1; } te3=0; for(b3=1;(i3+b3)=0;b3++){ if((ch3[i3-b3-ki3][j3]==0)){te3=te3+1;} } if ((i3-ki3-1==te3)&&(kj3==j3)){ check3=1; } te3=0; for(b3=1;(j3+b3)0;b3++){ if((ch3[i3-b3][j3-b3]==0)){te3=te3+1;} } if ((j3-kj3-1==te3)&&(ki3==i3-te3-1)){ check3=1; } te3=0; for(b3=1;(i3+b3)=0)&&(ch3[i3-b3][j3+b3]!=1));b3++){ if((ch3[i3-b3][j3+b3]==0)){te3=te3+1;} } if ((i3-ki3-1==te3)&&(kj3==j3+1+te3)){ check3=1; } } if (ch3[i3][j3]==5){ //algoritem for the runner te3=0; for(b3=1;(j3+b3)0;b3++){ if((ch3[i3-b3][j3-b3]==0)){te3=te3+1;} } if ((j3-kj3-1==te3)&&(ki3==i3-te3-1)){ check3=1; } te3=0; for(b3=1;(i3+b3)0;b3++){ if((ch3[i3-b3][j3+b3]==0)){te3=te3+1;} } if ((i3-ki3-1==te3)&&(kj3==j3+1+te3)){ check3=1; } } if (ch3[i3][j3]==4){ //algoritem for the horus if ((ch3[i3+1][j3+2]==1)){ check3=1; } if ((ch3[i3+1][j3-2]==1)&&((j3-2)>=0)){ check3=1; } if ((ch3[i3-1][j3+2]==1)&&((i3-1)>=0)){ check3=1; } if (((ch3[i3-1][j3-2]==1)&&((i3-1)>=0))&&((j3-2)>=0)){ check3=1; } if ((ch3[i3+2][j3+1]==1)){ check3=1; } if ((ch3[i3+2][j3-1]==1)&&((j3-1)>=0)){ check3=1; } if ((ch3[i3-2][j3+1]==1)&&((i3-2)>=0)){ check3=1; } if ((ch3[i3-2][j3-1]==1)&&((i3-2)>=0)){ check3=1; } } } } if (check3) printf("check!\n\n"); else printf("no check!\n\n"); for(i3=0;i3<8;i3++){ //here the program ends and now initialize the variables for(j3=0;j3<8;j3++){ ch3[i3][j3]=0;}} j3=0;i3=0;b3=1;kj3=0;ki3=0;te3=0;check3=0; } //end the switch } }