EightQueens Example

The code for the EightQueens example evaluates whether the square board, which represents a chess board, is configured to keep the queens safe from the other queens on the board.

The main method begins by calling the solveNQueens method with a parameter of 8. The parameter of the method represents the number of queens on the board. The solveNQueens method determines whether a board can be created to accommodate the number of queens using the blankBoard() method and canSolve() method. The blankBoard method creates the char array that will represent and store the result of the canSolve() method. The canSolve() method creates the board using 'q' to represent queens and '.' to represent blank spaces. If the result of the canSolve() method is true, the system prints a statement saying the "queen problem" has been solved. If the result is false, the system prints a statement saying that is cannot be solved. The solveNQueens method is now complete.

The main method then creates an ArrayList named solutions. This ArrayList is set equal to the result of the method getAllNQueens() with a parameter of 8 queens. The getAllNQueens() method appears to be very similar to the solveNQueens() method. The getAllNQueens() method first creates a blank board then calls the solveAllNQueens(). The solveAllNQueens() method checks to see if the board is complete , if so, it is added to the ArrayList solutions. If it is not complete, it completes the board by adding a queen ('q'), checking to see if it is safe (using the queensAreSafe() method), adding a column if necessary and finally adding a blank space ('.'). Once solveAllNQueens() is completed, getAllNQueens() returns the now modified ArrayList solutions.

The main method is supposed to print the size of solutions, but that does not seem to run. The main method finally double checks to see if the queens are safe, and if so, it prints the ArrayList solutions. If they are not safe, it prints a message to fix the board.