# Thread: "uniqe" practice for 8 qeens problem

1. Did you know your queen's program churned out a faulty result?

Quzah.

2. Adak and all, i finish my code, you can see that it's a monster code!
adak I need to redeem your promise "*don't* want to do your assignment for you, we want to *help* you, do your assignment.".. so help me it's working but it's monster!

Code:
```#include<stdio.h>
void main(){
int program;
int i,chess[8][8]={0},temp_j,temp_k,z,w,k,j,flag = 0,l;
int arr2[8]={0};
char c;

do{
printf("1.  8 queens assignment\n");
printf("2.  8 queens solution print\n");
printf("3.  Check query\n");
printf("4.  Exit program\n");
scanf("%d",&program);
switch(program){
//--------------------------------action number 1---------------------
case(1):
do{
printf("\nEnter location of queen no. %d : ",i);
scanf("%d%c%d",&temp_j,&c,&temp_k);
if(temp_j<0||temp_j>7||temp_k<0||temp_k>7){
printf("\nOut of bundaries!\n");
printf("Enter location of queen no. %d : ",i);
continue;
}
if(chess[temp_j][temp_k] == 1){
continue;
}
chess[temp_j][temp_k] = 1;
i++;
}while(i<8);

for (i=0;i<8;i++){           //*--------------------*
if (flag)break;          //*passing on the array*
for(j=0;j<8;j++){       //*--------------------*
if (flag)break;
if (chess[i][j] == 1){ //checking for queen on square

for (k=i+1;k<8;k++){//checking for queen on the same row
if (chess[k][j] == 1){
printf("Impossible assignment!\n");
flag = 1;
break;
}
}
if (flag)break;

for (k=j+1;k<8;k++){//checking for queen on on the same column
if (chess[i][k] == 1){
printf("Impossible assignment!\n");
flag = 1;
break;
}
}
if (flag)break;

w=i+1;//checking for queen on on the right diagonal
z=j+1;
while(((z<8)&&(z>=0))&&((w>=0)&&(w<8))){
if (chess[w][z] == 1){
printf("Impossible assignment!\n");
flag = 1;
break;
}
w++;
z++;
}
if (flag)break;

w=i+1;//checking for queen on on the left diagonal
z=j-1;
while(((z>=0)&&(z<8))&&((w<8)&&(w>=0))){
if (chess[w][z] == 1){
printf("Impossible assignment!\n");
flag = 1;
break;
}
w++;
z--;
}
if (flag)break;

}
}
}

if (flag==0)
printf("Possible assignment!\n");

//initialization variables
flag=0;
for (i=0;i<8;i++){
for(j=0;j<8;j++)
chess[i][j]=0;
}
break;
//--------------------------------------------Action number 2
case(2):
do{
printf("\nEnter solution number: ");
scanf("%d",&num);

}
while (num<1 || num>92);
n=1;
flag2=0;
p=0;
while (1){
for (i=p; i<8; i++){
if (flag2){
j=(queens[i]+1);
} else {
j=0;
}
if (flag3){
j=tempj;
flag3=0;
}

for (; j<8; j++){
queens[i]=j;
w=i-1;

flag=1;
if (w>=0){
flag=1;
for (k=w;k>=0;k--){

if (j==queens[k]){

flag=0;
break;
}

flag=0;
break;
}
}
}
if (flag){
break;
}
}
if (!flag){
i-=2;
flag2=1;
}
else {
flag2=0;
}
}
if (num!=n){
n++;

p=7;
tempj = (queens[7]+1);
if((queens[7]+1)%8==0){
tempj = queens[6]+1;
p=6;}
flag3=1;
}else break;
}

for (i=0; i<8; i++){
printf("%d,%d ",i,queens[i]);

}
printf("\n\n");
break;

//--------------------------------------------Action number 3
case(3):
for (i=0; i<8; i++){
for (j=0;j<8;j++){
chess[i][j]=0;
}
}
do{
printf("Enter row,column,tool: ");
scanf("%d%c%d%c%d",&row,&c,&col,&c,&tool);
if (row>=0 && row<=7 && col >=0 && col <=7 && tool>=1 && tool <=5){
if (chess[row][col]){
}
else
{
chess[row][col]=tool;
}
} else {
break;
}
} while(1);

flag = 0;

for (i=0; i<8; i++){
for (j=0; j<8; j++){
if (chess[i][j] == 1){
flag=1;
break;
}
}
if (flag){
break;
}
}

flag2=0;
flag3=0;
king_col = j;
king_row = i;

// Checking on the right of the king's column
for (i=king_col+1; i<8; i++){
switch (chess[king_row][i]){
case 2:
case 3:
flag2=1;
break;
case 0: break;
default: flag3=1; break;
}
if (flag2 || flag3){
break;
}
}

flag3=0;

if (!flag2){
// Checking on the left of the king's column
for (i=king_col-1; i>=0; i--){
switch (chess[king_row][i]){
case 2:
case 3:
flag2=1;
break;
case 0: break;
default: flag3=1; break;
}
if (flag2 || flag3){
break;
}
}

flag3=0;

if (!flag2){
// checking above the king ( same column )
for (i=king_row-1; i>=0; i--){
switch (chess[i][king_col]){
case 2:
case 3:
flag2=1;
break;
case 0: break;
default: flag3=1; break;
}
if (flag2 || flag3){
break;
}
}

flag3=0;
if (!flag2){
// checking below the king ( same column )
for (i=king_row+1; i<8; i++){
switch (chess[i][king_col]){
case 2:
case 3:
flag2=1;
break;
case 0: break;
default: flag3=1; break;
}
if (flag2 || flag3){
break;
}
}

flag3=0;
if (!flag2){
i=king_row-1;
j=king_col+1;
while (i>=0 && j<8){
switch (chess[i][j]){
case 2:
case 5:
flag2=1;
break;
case 0: break;
default: flag3=1; break;
}
if (flag2 || flag3){
break;
}

i--; j++;
}

flag3=0;
if (!flag2){
i=king_row-1;
j=king_col-1;
while (i>=0 && j>=0){
switch (chess[i][j]){
case 2:
case 5:
flag2=1;
break;
case 0: break;
default: flag3=1; break;
}
if (flag2 || flag3){
break;
}

i--; j--;
}

flag3=0;
if (!flag2){
i=king_row+1;
j=king_col-1;
while (i<8 && j>=0){
switch (chess[i][j]){
case 2:
case 5:
flag2=1;
break;
case 0: break;
default: flag3=1; break;
}
if (flag2 || flag3){
break;
}

i++; j--;
}

flag3=0;
if (!flag2){
i=king_row+1;
j=king_col+1;
while (i<8 && j<8){
switch (chess[i][j]){
case 2:
case 5:
flag2=1;
break;
case 0: break;
default: flag3=1; break;
}
if (flag2 || flag3){
break;
}

i++; j++;
}

flag3=0;
if (!flag2){
// First knight case
i=king_row-2;
j=king_col+1;

if (i>=0 && i<8 && j>=0 && j<8){
if (chess[i][j]==4){
flag2=1;
}
}

if (!flag2){
// Second knight case
i=king_row-2;
j=king_col-1;

if (i>=0 && i<8 && j>=0 && j<8){
if (chess[i][j]==4){
flag2=1;
}
}

if (!flag2){
// Third knight case
i=king_row-1;
j=king_col-2;

if (i>=0 && i<8 && j>=0 && j<8){
if (chess[i][j]==4){
flag2=1;
}
}

if (!flag2){
// Fourth knight case
i=king_row-1;
j=king_col+2;

if (i>=0 && i<8 && j>=0 && j<8){
if (chess[i][j]==4){
flag2=1;
}
}

if (!flag2){
// Fifth knight case
i=king_row+1;
j=king_col-2;

if (i>=0 && i<8 && j>=0 && j<8){
if (chess[i][j]==4){
flag2=1;
}
}

if (!flag2){
// Sixth knight case
i=king_row+2;
j=king_col-1;

if (i>=0 && i<8 && j>=0 && j<8){
if (chess[i][j]==4){
flag2=1;
}
}

if (!flag2){
// Seventh knight case
i=king_row+2;
j=king_col+1;

if (i>=0 && i<8 && j>=0 && j<8){
if (chess[i][j]==4){
flag2=1;
}
}

if (!flag2){
// Eighth knight case
i=king_row+1;
j=king_col+2;

if (i>=0 && i<8 && j>=0 && j<8){
if (chess[i][j]==4){
flag2=1;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}

if (flag2){
printf("check!\n");
} else {
printf("no check!\n");
}

break;
//--------------------------------------------Wrong choice - back to main menu.
default:
printf("\n");
break;
}
}while (program!=4);

}```

3. Congratulations & Holy ........ - 20 levels of indentation!!

And no other function allowed but main()

I'm not on a computer that will run it yet, but I look forward to running it later today.

Quzah, that was a representation, to show the way the board used the border logic, not output from the program.

4. Thanks!
yhea 20 ........ing indentation it's to long, I'm sure that there is a way to make all programs be shorter even junt using main()

but we start making progs wirh functions only in the next assiment:\

5. Megazord, I'm happy to help with your program, but I'm not prepared to fight your Monster program.

You didn't mention the restriction of not using any function except main(), and that you were going to use a 2D array, despite the assignment asking for just a 1D array.

My program I wrote to help show you some logic, is zip! out the window. No sane person would choose (on their own), to write code for an assignment like this, in one function. I do remember how we made do before functions were available, but that was a long time ago, and in a BASIC galaxy far, far, away.

A few thoughts:

1) use 2-4 spaces instead of tabs for each level of indentation. As is, it's a PITA to study. It also "breaks the tables" on our forum.

This should be an option in your editor, BTW.

2) use descriptive names for variables: flag & flag2, flag3, leave a lot to be desired for naming.

3) add some comments to your programs. Not for the obvious bits, but for things like "king" in this code. I was fumbling there.

I don't believe you can make this much smaller, and still leave it as clear as it is, because you don't have the power of functions. Without comments and descriptive variable names, using goto's to sections of code would be tedious to make work, and no smaller.

The thing that makes this difficult is that you have two modes of doing the same work. In the "clear the board" mode, you are removing ALL the squares the queen attacks. In the "checking" mode, you do the same thing, but now you only CHECK the squares which are less than (lower in index for a 1D array), the square you're currently on. Then on the squares that are higher in index, you remove them IF the lower numbers have gone through their check loop OK, with no conflicts.

It's not a trivial problem, and I'm a surprised that you came up with this.

6. I fixed my program for the length and for more uniqe variables,

maybe if I will specify my problems, you can help me more:

1. On part 1, can I unite some of the checks?
2. On part 2 of the program, can I make it more efficient?
3. can I replace the "checks for threat by a knight" 8 if's on part 3, to something shorter? like 'Switch' or somthing?

Code:
```#include<stdio.h>
void main(){
char c;
int choice,i,j,z,w,k,flag,num,n;
int chess[8][8]={0},solu[8]={0},row,column,tool;
//------------------temp Declaer----------
int p,flag2, flag3,tempj,tangent,tangent1;
int temp_tangent,temp_tangent1;

//----------------------------------------

do{
printf("1.  8 queens assignment\n");
printf("2.  8 queens solution print\n");
printf("3.  Check query\n");
printf("4.  Exit choice\n");
scanf("%d",&choice);
printf("\n");
switch(choice){
//*-----------------------------------------------------*
//*8 queens assignment - This program checks if the user*
//*Queen positioning are valid positioning.             *
//*-----------------------------------------------------*
case(1):
i=0;
do{
printf("Enter location of queen no. %d : ",i);
scanf("%d%c%d",&row,&c,&column);
//printf("\n");
if(row<0||row>=8||column<0||column>=8){
printf("Out of bundaries!\n");
continue;
}
if(chess[row][column]==1){
continue;
}
chess[row][column]=1;
i++;
}while(i<8);

for (i=0;i<8;i++){           //*--------------------*
if (flag)break;          //*passing on the array*
for(j=0;j<8;j++){       //*--------------------*
if (flag)break;
if (chess[i][j] == 1){ //checks for queen on square

for (k=i+1;k<8;k++){//checks for queen on the same row
if (chess[k][j] == 1){
flag = 1;
break;
}
}
if (flag)break;

for (k=j+1;k<8;k++){//checks for queen on the same column
if (chess[i][k] == 1){
flag = 1;
break;
}
}
if (flag)break;

w=i+1;//checks for queen on the right diagonal
z=j+1;
while(((z<8)&&(z>=0))&&((w>=0)&&(w<8))){
if (chess[w][z] == 1){
flag = 1;
break;
}
w++;
z++;
}
if (flag)break;

w=i+1;//checks for queen on the left diagonal
z=j-1;
while(((z>=0)&&(z<8))&&((w<8)&&(w>=0))){
if (chess[w][z] == 1){
flag = 1;
break;}
w++;
z--;
}
if (flag)break;
}
}
}

if(flag)
printf("Impossible assignment!\n\n");
else
printf("Possible assignment!\n\n ");
break;
//*-----------------------------------------------------*
//*8 Queens solution print - This program will print    *
//*a valid Queen positioning board, according to user   *
//*choice.                                              *
//*-----------------------------------------------------*
case(2):

do{        //Input for solution number
printf("Enter solution number: ");
scanf("%d",&num);
}while (num<1 || num>92);

n=1;
flag3=0;
flag2=0;
p=0;

for(;;){
for (i=p; i<8; i++){
if (flag2){          //*--------------------------------*
j=(solu[i]+1);   //*                                *
} else {             //*                                *
j=0;             //*                                *
}                    //*choosing a column to start from *
if (flag3){          //*                                *
j=tempj;         //*                                *
flag3=0;         //*                                *
}                    //*--------------------------------*

for (;j<8; j++){
solu[i]=j;
w=i-1;
tangent1 = (j+i);
tangent = (j-i);
flag=1;
if (w>=0){
//going to i-1(--), and checks if valid
for (k=w;k>=0;k--){
temp_tangent = (solu[k]-k);
temp_tangent1 = (solu[k]+k);

//checks for same column
if (j==solu[k]){
flag=0;
break;}

//checks for same tangent
if ((temp_tangent==tangent)||(temp_tangent1==tangent1)){
flag=0;
break;}
}
}

if (flag)break;
}

if (!flag){
i-=2;
flag2=1;
}
else
flag2=0;
}
if (num!=n){
n++;
p=7;
tempj = (solu[7]+1);
if((solu[7]+1)%8==0){
tempj = solu[6]+1;
p=6;
}
flag3=1;
}
else break;
}

//printing the solution(Index will be rows, soul[] will be column)
for (i=0; i<8; i++)
printf("%d,%d ",i,solu[i]);
printf("\n");
break;

//*-----------------------------------------------------*
//*Check query -                                        *
//*-----------------------------------------------------*
case(3):
//initialization variables
for (i=0; i<8; i++){
for (j=0;j<8;j++){
chess[i][j]=0;
}
}

do{
printf("Enter row,column,tool: ");
scanf("%d%c%d%c%d",&row,&c,&column,&c,&tool);
if((row<0)||(row>7)||(column<0)||(column>7)||(tool<0)||(tool>5))break;
if(chess[row][column])
{
continue;
}
chess[row][column]=tool;
}while((row>=0)&&(row<8)&&(column>=0)&&(column<8)&&(tool>0)&&(tool<=5));

flag=0;
for(i=0;i<8;i++){                //*--------------------*
if(flag)break;               //*passing on the array*
for(j=0;j<8;j++){            //*--------------------*
if (chess[i][j]==1){

//checks for threat by a knight
if(chess[i+1][j+2]==4&&i+1>0&&i+1<8&&j+2>0&&j+2<8){
flag=1;
break;}
if(chess[i+2][j+1]==4&&i+2>0&&i+2<8&&j+1>0&&j+1<8){
flag=1;
break;}
if(chess[i+2][j-1]==4&&i+2>0&&i+2<8&&j-1>0&&j-1<8){
flag=1;
break;}
if(chess[i-1][j-2]==4&&i-1>0&&i-1<8&&j-2>0&&j-2<8){
flag=1;
break;}
if(chess[i-2][j-1]==4&&i-2>0&&i-2<8&&j-1>0&&j-1<8){
flag=1;
break;}
if(chess[i-2][j+1]==4&&i-2>0&&i-2<8&&j+1>0&&j+1<8){
flag=1;
break;}
if(chess[i-1][j+2]==4&&i-1>0&&i-1<8&&j+2>0&&j+2<8){
flag=1;
break;}
if(chess[i+1][j-2]==4&&i+1>0&&i+1<8&&j-2>0&&j-2<8){
flag=1;
break;}

//checks for threat on the diagonals:
//checks for threat on the right down diagonal
w=i+1;
z=j+1;
while(w>0&&w<8&&z>0&&z<8){
if(chess[w][z]==3||chess[w][z]==4) break;
if(chess[w][z]==2||chess[w][z]==5){
flag=1;
break;}
w++;
z++;
}
//checks for threat on the left down diagonal
if(flag)break;
w=i-1;
z=j+1;
while(w>0&&w<8&&z>0&&z<8){
if(chess[w][z]==3||chess[w][z]==4) break;
if(chess[w][z]==2||chess[w][z]==5){
flag=1;
break;}
w--;
z++;
}

//checks for threat on the left up diagonal
if(flag)break;
w=i-1;
z=j-1;
while(w>0&&w<8&&z>0&&z<8){
if(chess[w][z]==3||chess[w][z]==4) break;
if(chess[w][z]==2||chess[w][z]==5){
flag=1;
break;}
w--;
z--;
}

//checks for threat on the right up diagonal
w=i+1;
z=j-1;
while(w>0&&w<8&&z>0&&z<8){
if(chess[w][z]==3||chess[w][z]==4) break;
if(chess[w][z]==2||chess[w][z]==5){
flag=1;
break;}
w++;
z--;
}

//checks for threat on the right row.
for(w=i;w<8;w++){
if(chess[w][j]==5||chess[w][z]==4) break;
if(chess[w][j]==2||chess[w][z]==3){
flag=1;
break;}
}
//checks for threat on the left row.
for(w=i;w>=0;w--){
if(chess[w][j]==5||chess[w][z]==4) break;
if(chess[w][j]==2||chess[w][z]==3){
flag=1;
break;}
}
//checks for threat on the column down.
for(z=j;z<8;z++){
if(chess[i][z]==5||chess[w][z]==4) break;
if(chess[i][z]==2||chess[w][z]==3)
{
flag=1;
break;
}
}
//checks for threat on the column up.
for(z=j;z>=0;z--){
if(chess[i][z]==5||chess[w][z]==4) break;
if(chess[i][z]==2||chess[w][z]==3){
flag=1;
break;}
}
}
}
}
if(flag)
printf("check!\n");
else
printf("no check!\n");

break;
//-------------------Wrong choice - back to main menu------------------------
default:
printf("\n");
break;
}
}while (choice!=4);

}```

7. I don't see why you check for threats from a knight, at all. 8 queens problem is all about queens and no other chess pieces.

This is quite compact code, considering that it has no function calls. The flags help a lot.

A switch statement wouldn't help much.

Congrats on getting it trimmed down this much.