N-QUEENS
PROJECT
By Alyce Brady • Edited
by Sandino Vargas-Pérez
PROBLEM DESCRIPTION
In this lab you will implement the N Queens Problem, placing N queens on an
N x N board (like a chess board) in such a way that no queen can attack
another queen. The N x N board will be represented by a
BoundedGrid.
Exercise Set
-
Download the starting code files for the N Queens Lab:
- NQueensApp.java
(contains the
mainmethod) - Queen.java (represents a queen on the board)
- NQueens.java (the partially-written class where you will implement the solution to the N Queens Problem)
- Two image files, GoldCrown.gif and SilverCrown.gif (images of crowns).
- The
grid.jar
Java archive (
jar) file, which contains a library of classes that can be used to model a two-dimensional grid, including:-
BoundedGrid(class that represents the two-dimensional grid)‡ -
Location(class that represents the row and column positions of a location in the grid)‡ -
ColorBlock(class whose objects represent blocks of color put in a grid)
Throughout this lab you may want to have access to the class documentation for the
Grid,GridObject, andLocationclasses. This documentation can be found at www.cs.kzoo.edu/GridPkg/GridPkgClassDocumentation/. -
- NQueensApp.java
(contains the
- Compile and run the program. You should see an 8 x 8 "chess board" with
no queens.
Before compiling your program, you will have to make sure your project knows about the grid.jar library. If you are using BlueJ, you can create a
+libsfolder under the folder containing your source files and put thejarfile there, or you can specify its location in the Libraries tab of the Preferences or Properties dialog box (under BlueJ->Preferences, Tools->Preferences, or File->Properties, depending on the version of BlueJ you are using). Then within BlueJ, open your folder as a Non-BlueJ Project and compile it. -
Complete the
numQueensandremoveQueenmethods, without adding any additional instance variables. To test theremoveQueenmethod, modify theplaceQueenmethod to add a queen to any arbitrary column (your choice) of the correct row for that queen, display the environment, and then remove the queen and redisplay the environment. Modify thesolvemethod to place one queen. Run the program. You should see one queen (or color block) appear and then disappear from the environment.If you want to use an image to represent a queen rather than a color block, put the image in the same folder as your Java source files and name it
queen.gif,queen.jpg, orqueen.png, as appropriate. -
Modify the
placeQueenmethod to recursively place all the queens in arbitrary columns (or the same arbitrary column). Think about where you should place the recursive call if you want to see the queens appear in each row, one-by-one, and then disappear in reverse order. Make sure you remember to include a base case. Do you need to modify thesolvemethod to place all the queens? If so, do it. -
Fully implement the
placeQueenmethod so that it checks column by column to find an OK location to place this queen, until the queen has been successfully placed in a column and all the queens after her have been successfully placed also. Since thelocationIsOKmethod always returnstrue, the queens should fill in the first column. -
Modify the
locationIsOKmethod to returnfalseif any queens have already be placed in the same column as the location parameter. When you have this working you should see the queens fill in the diagonal from location (0, 0) to location (n-1, n-1). -
Modify the
locationIsOKmethod again to also returnfalseif any queens have already been placed on the board in locations that are on the diagonal from the location parameter.
Location class comes
directly from the AP® Marine Biology Simulation case study;
BoundedGrid was inspired by the MBS BoundedEnv
class. AP is a registered trademark of the College Entrance Examination
Board. The Marine Biology Simulation case study was copyrighted in 2002 by
the College Entrance Examination Board. It is available on the web from the
College Board web site (www.collegeboard.com
and apcentral.collegeboard.com).
Copyright Alyce Brady, 2002.
Submit your completed program through Kit, under Project 3: N-Queens Problem.
Have fun! And if you have any questions remember I am available during office hours and the Collaboration Center folks are here for you as well!
← back to the schedule