I am trying to develop an 8 queens program, and currently it is working however it is printing 87222211 which is obviously wrong, I am trying to print the row of the queen in order from column0-column7 of an array. I am new to C++ and was wondering if anyone could see my mistake? Thanks for taking a look. Here is my code.
Code:
int a[8];
void display ();
int TestLegal (int currentColumn, int currentRow)
{
for (int column = 0; column < currentColumn; ++column) //check row conflict
if (a[column] == a[currentColumn])
return (0); // return FALSE if both queens are on the same row
for (int column = 0; column < currentColumn; ++column) //check diagonal conflict
if (abs(a[column] - column) == (abs(a[currentColumn] - currentColumn)))
return (0); // return FALSE if both queens are on the same diagonal.
return currentRow;
}
int placeQueen (int column) {
if (column == 8) //Check if all columns have a legal value
{
display();
return (-1);
}
for (int row = 1; row <= 8; ++row) // Try all of the legal values for the column
{
if (TestLegal (column, row))
{
a[column] = row;
if (placeQueen (column + 1) < 0)
return -1;
}
}
return (0);
}
Comment