V22.0101 ..... Homework 3 ..... Fall 2006
Chess Queens (instead of TicTacToe)

Assigned: Wed Oct 4
Due: Tue Oct 10 at midnight.

This is an exercise with 2D arrays that we will extend to a more interesting problem later in the semester. It is based on chess but involves only one kind of piece, a queen. We are allowed to put a queen on the board only if it does not share the same row, the same column, the same left diagonal, or the same right diagonal, as another queen that is already on the board.

Write a queens class that has the following private data fields (instance variables):

The class should have a constructor that assigns a value for N and constructs an N by N array board, initalizing it to have all elements set to false. Also write the following methods for the class: The main method should prompt the user for values of N, and for each choice, do the following
  1. construct an N by N queens object
  2. repeatedly prompt the user for integers I and J that specify a row and column to attempt to place a queen on the board, calling legal to determine whether such a move is legal. If it is legal, the main method should call setQueen to place the queen accordingly and display the new board configuration; otherwise, it should reject the request and ask for another choice of I and J
  3. this should continue until the user gives up trying to place a queen. The user should then be given the option of trying a new board (with a new value of N) or quitting.

If the user chooses N to be zero or negative, the program should reject it and insist on a positive value for N.

You can have fun seeing how many queens you can place on a board for N = 3, 4 and 5 (the answer for N = 1 and 2 is obvious). Later, we will find a systematic way to try to place N queens on an N by N board, but this is possible only for certain values of N. Don't try to do this yet.