- I've looked at EightQueens code before.
I can understand what's going on
but the code always seem so cryptic.
And it wasn't just single letter variable names
that bothered me.
It was trying to decipher exactly how the code worked.
Yes, that's one of the shortcomings of the C language.
So I began my EightQueens adventure by creating code
that operated on a character array, rows and columns.
First, to avoid the logic that comprises the bulk of a code,
I work on the main function
which examines the command line arguments.
So the default of eight squares is set
when no arguments is specified.
And if the first argument is a value,
then that value is converted into an integer.
Otherwise, they help messages displayed.
10 options for brief in board are checked.
And the output mode variable is set accordingly.
I also wrote the show_board function
which displays the board in brief output.
I didn't even bothered to code the brief output.
For this first approach, I just wanted to see the board.
In fact, the show_board function isn't
really called for a proper location in the code
simply because I wanted to see how the program ran.
Call it a form of visual debugging.
The board buffer is a character array
allocated here at line 63.
It's initialized to all spaces
where a space means an open square.
An asterisk represents a line of attack
and a Q is a queen.
I agree that it would be easier to use
a two-dimensional array here
as array notation is far more understandable.
But if you use in C, you need to accept
and work with pointers.
So the board buffer is a single chunk of memory
treated like a two-dimensional array with rows and columns.
The find_square routines scans the board,
looking for an open square.
It searches one row at a time.
And when a queen is found, the same routine is
then called recursively to scan the next row.
When the space isn't under attacked or occupied by a queen,
the set_queen function is then called.
It places the queen at a given row and column
plus it marks the lines of attack in the same grid.
I added the show_board function called
the end of this function to check my progress.
Also because the code, well, it didn't run
and I wanted to know why.
So the code runs but it finds only five solutions.
They aren't even real solutions
because I didn't write that part of the code yet.
To see what went wrong, I specified
the board size and the board option for output.
If I scroll back, I see that the first queen is placed
and her lines of attack are specified.
The second queen finds a spot and dutifully feels
the board with asterisks showing her attacks.
Same for the third and the fourth and the fifth queen.
But then I noticed something was wrong.
The board is full.
Because the queens are recursively placed,
the code would back up from the fifth row to the fourth row
and it attempt to change the location
of a queen and try again.
The problem is when the code backs up,
it doesn't remove the queen or her attacking asterisks.
I stood over how to deal with that.
It wouldn't be difficult to remove the queen
but determining which asterisks
belong to which queen would be a nightmare.
So I yelled at the cat and I took a walk.
Inspiration hit me again and I return to write
my second version of the nqueen solution
which I'll cover in the next movie.