package game;

import exception.PlacementFailedException;

/**
 * This class creates a 2d array of squares and keeps track of the piece
 * locations, win conditions, and text display.
 */
public abstract class CharBoard {
	/**
	 * board a 2d array of characters that keep track of the pieces on the board
	 */
	private Square[][] board;

	/**
	 * This constructor takes the size of the board and Initializes the board.
	 * 
	 * @param size
	 */
	public CharBoard(int size) {
		board = new Square[size][size];

		initializeBoard();
	}

	/**
	 * This method sets the board the the position that it should initially be
	 * for the game that is running. The default version is turning every square
	 * into an empty square.
	 */
	public void initializeBoard() {
		for (int j = 0; j < board.length; j++)
			for (int i = 0; i < board[j].length; i++)
				board[j][i].setChar(' ');
	}

	/**
	 * This method attempts to place the piece on the coordinate. If the
	 * placement fails, then it throws an exception.
	 * 
	 * @return if the piece was successfully placed or not
	 */
	public void attemptPlace(PieceType piece, Coordinate c) {
		if (canPlace(piece, c)) {
			place(piece, c);
		} else {
			throw new PlacementFailedException("The piece cannot be placed on square \"" + c.toAlphaNumerical() + '\"');
		}
	}

	/**
	 * This method places a piece on the square the coordinates corresponds to
	 * without verification.
	 * 
	 * @param piece
	 *            the piece that will be placed
	 * @param c
	 *            the coordinates of the square the piece will be placed on
	 */
	public void place(PieceType piece, Coordinate c) {
		board[c.getX()][c.getY()].setChar(piece.getChar());
	}

	/**
	 * This method verifies that a piece can be placed on this square.
	 * 
	 * @param piece
	 *            the piece that will be checked
	 * @param c
	 *            the coordinates the piece will be checked on
	 * @return if the piece can be placed on the coordinate
	 */
	public abstract boolean canPlace(PieceType piece, Coordinate c);

	/**
	 * This method tells the user if the square has no piece on it.
	 * 
	 * @param c
	 *            the coordinate
	 * @return if the coordinate is empty
	 */
	public boolean isEmpty(Coordinate c) {
		return board[c.getX()][c.getY()].getChar() == ' ';
	}

	/**
	 * For every column, put a number. For every row, put a letter. Place the
	 * single letter representation of the piece inside the grid.
	 * 
	 * @return a text created board with all the square names and pieces on the
	 *         squares shown.
	 */
	public String toString() {
		String out = " ";
		for (int i = 1; i <= board.length; i++)
			out += " " + (i % 10 == 0 ? i / 10 : i % 10);
		out += "\n";

		char letter = 'A';
		for (int j = 0; j < board[0].length; j++) {
			out += "" + letter++ + " " + board[j][0];
			for (int i = 1; i < board.length; i++) {
				out += "|" + board[j][i];
			}
			if (j < board[0].length - 1) {
				out += "\n  -";
				for (int i = 1; i < board.length; i++)
					out += "--";
				out += "\n";
			}
		}
		out += "\n";

		return out;
	}

	/**
	 * This method detects if the game has ended and returns a notification a
	 * character that corresponds to the state of the game.
	 */
	public abstract char gameEnd();

	/**
	 * This method detects if the square at a coordinate exists.
	 * 
	 * @return if the square exists
	 */
	public boolean squareExists(Coordinate out) {
		return out.getX() >= 0 && out.getX() < board[0].length && out.getY() >= 0 && out.getY() < board.length;
	}

	/**
	 * This method returns the board that the game is placed on.
	 * 
	 * @return the board
	 */
	public Square[][] getBoard() {
		return board;
	}
}
@@ -3741,22 +3741,20 @@
 %09public 
-Squ
+ch
 ar
-e
 %5B%5D%5B%5D get
@@ -3766,28 +3766,213 @@
 () %7B%0D%0A%09%09
-return board
+char%5B%5D%5B%5D out = new char%5Bboard.length%5D%5Bboard%5B0%5D.length%5D;%0D%0A%09%09for (int j = 0; j %3C board.length; j++)%0D%0A%09%09%09for (int i = 0; i %3C board%5Bj%5D.length; i++)%0D%0A%09%09%09%09out%5Bj%5D%5Bi%5D = board%5Bj%5D%5Bi%5D.getChar();%0D%0A%09%09return out
 ;%0D%0A%09%7D%0D%0A%7D

fix getBoard method