package game;

import exception.PlacementFailedException;

public abstract class Board {
	private Square[][] board;

	public Board(int size) {
		board = new Square[size][size];

		initializeBoard();
	}

	public void initializeBoard() {
		for (int row = 0; row < board.length; row++)
			for (int col = 0; col < board[row].length; col++)
				board[row][col] = new Square(' ');
	}

	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() + '\"');
		}
	}

	public void place(PieceType piece, Coordinate c) {
		board[c.getX()][c.getY()].setChar(piece.getChar());
	}

	public abstract boolean canPlace(PieceType piece, Coordinate c);

	public boolean isEmpty(Coordinate c) {
		return board[c.getX()][c.getY()].getChar() == ' ';
	}

	public String toString() {
		String out = " ";
		out += labelEachColumn() + "\n";
		out += printAllRows() + "\n";

		return out;
	}

	protected String labelEachColumn() {
		String out = "";
		for (int i = 1; i <= board.length; i++)
			out += " " + labelColumn(i);

		return out;
	}

	protected String labelColumn(int i) {
		return "" + (i % 10 == 0 ? i / 10 : i % 10);
	}

	protected String printAllRows() {
		String out = "";
		for (int j = 0; j < board[0].length; j++) {
			out += printRowLabel(j) + " ";
			out += printRow(j) + "\n";
			out += printRowDivider(!isBottomRow(j)) + "\n";
		}
		return out;
	}

	protected String printRowLabel(int row) {
		return "" + (char) ('A' + row);
	}

	protected String printRow(int j) {
		String out = "" + board[j][0].getChar();
		for (int i = 1; i < board.length; i++) {
			out += "|" + board[j][i].getChar();
		}
		return out;
	}

	protected String printRowDivider(boolean shouldPrint) {
		if (shouldPrint) {
			String out = "  -";
			for (int i = 1; i < board.length; i++)
				out += "--";
			return out;
		} else {
			return "";
		}
	}

	protected boolean isBottomRow(int row) {
		return board.length - 1 == row;
	}

	public abstract char gameEnd();

	public boolean squareExists(Coordinate out) {
		return out.getX() >= 0 && out.getX() < board[0].length && out.getY() >= 0 && out.getY() < board.length;
	}

	public char[][] getCharBoard() {
		char[][] out = new char[board.length][board[0].length];
		for (int j = 0; j < board.length; j++)
			for (int i = 0; i < board[j].length; i++)
				out[j][i] = board[j][i].getChar();
		return out;
	}
}
@@ -114,16 +114,44 @@
  board;%0D
+%0A%09private Move currentMove;%0D
 %0A%0D%0A%09publ
@@ -411,32 +411,153 @@
 re(' ');%0D%0A%09%7D%0D%0A%0D%0A
+%09public void setMove(Move move) %7B%0D%0A%09%09currentMove = move;%0D%0A%09%7D%0D%0A%0D%0A%09public Move getMove() %7B%0D%0A%09%09return currentMove;%0D%0A%09%7D%0D%0A%0D%0A%0D%0A
 %09public void att
@@ -638,24 +638,16 @@
 %09%09place(
-piece, c
 );%0D%0A%09%09%7D 
@@ -801,68 +801,172 @@
 ace(
-PieceType piece, Coordinate c) %7B%0D%0A%09%09board%5Bc
+) %7B%0D%0A%09%09int row = currentMove.getCoordinate().getX();%0D%0A%09%09int col = currentMove.getCoordinate()
 .get
-X()%5D%5Bc.getY()
+Y();%0D%0A%09%09PieceType piece = currentMove.getPiece();%0D%0A%09%09board%5Brow%5D%5Bcol
 %5D.se

add currentMove