Class ChessBoard

java.lang.Object
  |
  +--ChessBoard

public class ChessBoard
extends java.lang.Object
implements java.lang.Cloneable

A class used to represent a chess board, used to solve the n-queens problem. Methods are provided to place queens on the board, make a copy of the board, and return a String representation of the board. All indexing into the board start at 0. That is for an 8x8 board, valid row or column numbers are 0 - 7.


Field Summary
static char BLOCKED
          A char used to represent a square to which some queen can move
static char FREE
          A char representation of a free square
static char QUEEN
          A char representation of a square containing a queen
 
Constructor Summary
ChessBoard(ChessBoard other)
          Creates an exact duplicate of the given board.
ChessBoard(int size)
          Creates a new, intially empty board of the specified size
 
Method Summary
 java.lang.Object clone()
          Returns an exact copy of this ChessBoard.
 boolean placeQueen(int row, int column)
          Places a queen at the specified board position, marking all squares the newly placed queen can attack with the BLOCKED constant.
 int size()
          Returns the number of rows and columns of this ChessBoard
 java.lang.String toString()
          Returns a String representation of this ChessBoard
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

FREE

public static final char FREE
A char representation of a free square

QUEEN

public static final char QUEEN
A char representation of a square containing a queen

BLOCKED

public static final char BLOCKED
A char used to represent a square to which some queen can move
Constructor Detail

ChessBoard

public ChessBoard(int size)
Creates a new, intially empty board of the specified size
Parameters:
size - the number of rows and columns on this ChessBoard

ChessBoard

public ChessBoard(ChessBoard other)
Creates an exact duplicate of the given board.
Parameters:
other - the board to be copied
Method Detail

clone

public java.lang.Object clone()
Returns an exact copy of this ChessBoard. Note that you have to perform a cast to convert the return type to a ChessBoard
Returns:
an exact copy of this ChessBoard
Overrides:
clone in class java.lang.Object

placeQueen

public boolean placeQueen(int row,
                          int column)
Places a queen at the specified board position, marking all squares the newly placed queen can attack with the BLOCKED constant. Note that all indexing should be in range [0, board.size() - 1]. If it is not possible to place a queen at the given square because it is either occupied by another queen or can be taken by another queen, false is returned. If the placement was successful, true is returned.
Parameters:
row - the row number at which to place the queen
column - the column number at which to place the queen
Returns:
true if the queen was placed, false otherwise

toString

public java.lang.String toString()
Returns a String representation of this ChessBoard
Returns:
a String representation of this ChessBoard
Overrides:
toString in class java.lang.Object

size

public int size()
Returns the number of rows and columns of this ChessBoard
Returns:
the number of rows and columns of this ChessBoard