Code and discussion for the 8 Queens problem.

Here's the code for the object-oriented solution for the 8 queens problem (queen.cpp), and the output it produces (queen.out). The source code is the code provided by the author of our class text, Dr. Timothy Budd, with one small change as we discussed in class (the while loop in findSolution() has been changed to an if statement since the loop never iterates).

Here is the same code except that trace statements have been added so that you can see all the interactions between the queen objects (queen-verbose.cpp). Here's the output it produces (queen-verbose.out).

Here's another version of the solution (q2.cpp). In this case, the second if statement in findSolution( ) is replaced with a return statement -- this causes the code to become tail-recursive. Notice the difference in the output generated (q2.out).

And finally, here's a version of the code that finds all possible solutions rather than just the first solution (all-solutions.cpp). As we discussed in class, we simply keep asking queen 8 to advance to its next safe position, until it can no longer advance. In testing this code, a problem with the queen's advance( ) behavior was found -- it did not work correctly when queen 1 ran out of rows. Look at the changes made to the advance( ) method. Here is the output this version produces (all-solutions.out).

Note: all C++ source files above compile and run successfully on both our Unix server and on PC's with MSVC++.