import java.awt.*;

public class ChessBoard
{
    /**
     * The size of a chess board.
     */

    public static final int SIZE = 8;

    /**
     * Codes for colors of pieces
     */

    public static final int WHITE = 0;
    public static final int BLACK = 1;

    /**
     * Makes a new chess board with pieces in their standard starting
     * locations.
     */

    public ChessBoard()
    {
	// *** 1 *** declare an instance variable that is a 2-D array of chars
	// called pieces

	// *** 2 *** create the pieces array as an 8x8 array of chars

	// *** 3 *** put 'R' in the upper left corner of the array,
	//         'r' in the lower left, and 'b' in the third columns of
	//         the bottom row

	// *** 4 *** initialize all of the 2nd and 7th rows to 'P' and 'p'

	// *** 5 *** initialize all of the 3rd through 6th rows to ' '

	// my code to set up the other pieces -- don't use setRow above
	setRow(0, " HBQKBHR");
	setRow(SIZE - 1, "rh qkbh ");
    }

    /**
     * Copies pieces (givenby character codes) from the given string into
     * the given row.
     *
     * @param r the row to put pieces in
     * @param s the string containing the pieces
     */

    public void setRow(int r, String s)
    {
	for (int c = 0; c < s.length(); c++)
	    pieces[r][c] = s.charAt(c);
    }

    /**
     * Returns true exacly when the given square is empty.
     *
     * @param r the row index of the square
     * @param c the column index of the square
     * @return true iff no piece occupies the square
     */

    public boolean isEmpty(int r, int c)
    {
	// *** 6 *** return true if there's a ' ' in row r, column c,
	//           false otherwise

	return false;
    }

    /**
     * Updates this board to reflect the result of moving the piece
     * at the given start location to the end location.
     * 
     *
     * @param startRow the row index of the piece to move
     * @param startCol the column index of the piece to move
     * @param endRow the row index of the square to move it to
     * @param endCol the column index of the square to move it to
     */

    public void makeMove(int startRow, int startCol, int endRow, int endCol)
    {
	// *** 7 *** copy from start position to end position and put ' '
	//           in start position
    }

    /**
     * Returns the color of the piece occupying the given square, provided
     * that there is a piece at that location.  The return value is
     * meaningless if there is no piece at the given location.
     *
     * @param r the row index of the square
     * @param c the column index of the square
     * @return the color of the piece on that square
     */

    public int getPieceColor(int r, int c)
    {
	if (pieces[r][c] >= 'a' && pieces[r][c] <= 'z')
	    return WHITE;
	else
	    return BLACK;
    }

    /**
     * Returns an upper case character representing the piece at the given
     * location on the board.
     *
     * @param r the row index of the square
     * @param c the column index of the square
     * @return the character representing the piece at the given location
     */

    public char getPiece(int r, int c)
    {
	return Character.toUpperCase(pieces[r][c]);
    }

    /**
     * Returns true exactly when it is legal to move the piece from
     * the first location to the second.
     *
     * @param startRow the row index of the piece to move
     * @param startCol the column index of the piece to move
     * @param endRow the row index of the square to move it to
     * @param endCol the column index of the square to move it to
     * @return true iff it legal to move the piece at the starting location
     * to the ending location
     */

    public boolean isLegalMove(int startRow, int startCol, int endRow, int endCol)
    {
	// can't capture your own piece
	if (!isEmpty(endRow, endCol)
	    && getPieceColor(startRow, startCol) == getPieceColor(endRow, endCol))
	    return false;

	switch (pieces[startRow][startCol])
	    {
	    case 'P':
		return isLegalBlackPawnMove(startRow, startCol, endRow, endCol);
	    case 'p':
		return isLegalWhitePawnMove(startRow, startCol, endRow, endCol);
	    case 'b':
	    case 'B':
		return isLegalBishopMove(startRow, startCol, endRow, endCol);

	    case 'H':
	    case 'h':
		return isLegalKnightMove(startRow, startCol, endRow, endCol);

	    case 'R':
	    case 'r':
		return isLegalRookMove(startRow, startCol, endRow, endCol);
		
	    case 'Q':
	    case 'q':
		return isLegalQueenMove(startRow, startCol, endRow, endCol);

	    case 'K':
	    case 'k':
		return isLegalKingMove(startRow, startCol, endRow, endCol);

	    default:
		return true;
	    }
    }

    /**
     * Returns true exactly when it is legal to move a black pawn from
     * the given start position to the end position.
     *
     * @param startRow the row index of the piece to move
     * @param startCol the column index of the piece to move
     * @param endRow the row index of the square to move it to
     * @param endCol the column index of the square to move it to
     * @return true iff it legal to move the piece at the starting location
     * to the ending location
     */
    private boolean isLegalBlackPawnMove(int startRow, int startCol, int endRow, int endCol)
    {
	// *** 8 *** return true in the following three cases, false otherwise
	//           1) move is down one and end location is empty
	//           2) move is straight down from 2nd row to 4th and end empty
	//           3) move is diagonally down one and end not empty and white

	return true;
    }

    /**
     * Returns true exactly when it is legal to move a white pawn from
     * the given start position to the end position.
     *
     * @param startRow the row index of the piece to move
     * @param startCol the column index of the piece to move
     * @param endRow the row index of the square to move it to
     * @param endCol the column index of the square to move it to
     * @return true iff it legal to move the piece at the starting location
     * to the ending location
     */
    private boolean isLegalWhitePawnMove(int startRow, int startCol, int endRow, int endCol)
    {
	// *** 9 *** write code similar to isLegalBlackPawnMove

	return true;
    }

    /**
     * Returns true exactly when it is legal to move a rook from
     * the given start position to the end position.
     *
     * @param startRow the row index of the piece to move
     * @param startCol the column index of the piece to move
     * @param endRow the row index of the square to move it to
     * @param endCol the column index of the square to move it to
     * @return true iff it legal to move the piece at the starting location
     * to the ending location
     */

    public boolean isLegalRookMove(int startRow, int startCol, int endRow, int endCol)
    {
	// *** 10 *** if moving in same row or same col and all spaces
	//            between are empty, return true, otherwise return false

	return true;
    }

    /**
     * Returns true exactly when it is legal to move a bishop from
     * the given start position to the end position.
     *
     * @param startRow the row index of the piece to move
     * @param startCol the column index of the piece to move
     * @param endRow the row index of the square to move it to
     * @param endCol the column index of the square to move it to
     * @return true iff it legal to move the piece at the starting location
     * to the ending location
     */

    public boolean isLegalBishopMove(int startRow, int startCol, int endRow, int endCol)
    {
	// *** 11 *** if moving diagonally and all pieces between start and
	//            end are empty then return true, otherwise return false

	return true;
    }

    /**
     * Returns true exactly when it is legal to move a knight from
     * the given start position to the end position.
     *
     * @param startRow the row index of the piece to move
     * @param startCol the column index of the piece to move
     * @param endRow the row index of the square to move it to
     * @param endCol the column index of the square to move it to
     * @return true iff it legal to move the piece at the starting location
     * to the ending location
     */

    public boolean isLegalKnightMove(int startRow, int startCol, int endRow, int endCol)
    {
	if ((Math.abs(endRow - startRow) == 2 && Math.abs(endCol - startCol) == 1)
	    || (Math.abs(endRow - startRow) == 1 && Math.abs(endCol - startCol) == 2))
	    return true;
	else
	    return false;
    }

    /**
     * Returns the sign of the parameter.  Sign is -1 if <CODE>x < 0</CODE>,
     * 0 if <CODE>x == 0</CODE> and 1 if <CODE>x > 0</CODE>.
     *
     * @param x the number whose sign will be returned
     * @return the sign of <CODE>x</CODE>
     */

    private int sign(int x)
    {
	if (x < 0)
	    return -1;
	else if (x == 0)
	    return 0;
	else
	    return 1;
    }

    /**
     * Returns true exactly when it is legal to move a queen from
     * the given start position to the end position.
     *
     * @param startRow the row index of the piece to move
     * @param startCol the column index of the piece to move
     * @param endRow the row index of the square to move it to
     * @param endCol the column index of the square to move it to
     * @return true iff it legal to move the piece at the starting location
     * to the ending location
     */

    public boolean isLegalQueenMove(int startRow, int startCol, int endRow, int endCol)
    {
	// *** 12 *** check for legal queen move
	
	return true;
    }

    /**
     * Returns true exactly when it is legal to move a king from
     * the given start position to the end position.
     *
     * @param startRow the row index of the piece to move
     * @param startCol the column index of the piece to move
     * @param endRow the row index of the square to move it to
     * @param endCol the column index of the square to move it to
     * @return true iff it legal to move the piece at the starting location
     * to the ending location
     */

    public boolean isLegalKingMove(int startRow, int startCol, int endRow, int endCol)
    {
	// *** 13 *** check for legal king move
	
	return true;
    }

    /**
     * Returns the height of this board in squares.
     *
     * @return the height of thisboard in squares
     */

    public int getHeight()
    {
	return SIZE;
    }

    /**
     * Returns the width of this board in squares.
     *
     * @return the width of this board in squares
     */

    public int getWidth()
    {
	return SIZE;
    }
    

}

